Transform in Angular Image Editor

31 Aug 202619 minutes to read

The Image Editor provides a range of transformation options for manipulating both the image and its annotations. These options include rotation, flipping, zooming, and panning. These transformations offer flexibility in adjusting the image and enhancing its visual appearance.

Rotate an image

The Image Editor allows you to rotate the image and its annotations by a specific number of degrees clockwise or counterclockwise using the rotate method. This method takes a single parameter: the angle of rotation in degrees. A positive value will rotate the image clockwise, while a negative value will rotate it counterclockwise.

Note: It is recommended to pass values in multiples of 90° (e.g., 90, 180, -90) for proper rotation alignment.

Here is an example of rotating an image in a button click event.

import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { ImageEditorModule } from '@syncfusion/ej2-angular-image-editor'
import { enableRipple } from '@syncfusion/ej2-base'



import { Component,ViewChild } from '@angular/core';
import { Browser } from '@syncfusion/ej2-base';
import { ImageEditorComponent } from '@syncfusion/ej2-angular-image-editor';

@Component({
imports: [
        
        ImageEditorModule
    ],


standalone: true,
    selector: 'app-root',
    template: `<div class="e-section-control">
              <!-- To render Image Editor. -->
              <div id="wrapperDiv" style="width:550px;height:350px;">
                <ejs-imageeditor #imageEditor (created)="created()" [toolbar]="toolbar"></ejs-imageeditor>
              </div>
              <button class="e-btn e-primary" (click)="rotateImage()">Rotate</button>
              </div>`
})

export class AppComponent {
    @ViewChild('imageEditor')
    public imageEditorObj?: ImageEditorComponent;
    public toolbar: string[] = [];
      public created(): void {
      if (Browser.isDevice) {
        this.imageEditorObj?.open('./flower.png');        
      } 
      else {
        this.imageEditorObj?.open('./bridge.png');
      }
    }
    rotateImage(): void {
        this.imageEditorObj?.rotate(90);
    }
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Flip an image

The Image Editor provides the flip method, which allows you to flip both the image and its annotations either horizontally or vertically. This method takes a single parameter of type ImageEditorDirection, which specifies the direction in which the flip operation should be applied.

The Direction parameter accepts two values: Horizontal and Vertical. When you choose Horizontal, the image and annotations will be flipped along the horizontal axis, resulting in a mirror effect. On the other hand, selecting Vertical will flip them along the vertical axis, producing a vertical mirror effect.

Here is an example of flipping an image in a button click event.

import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { ImageEditorModule } from '@syncfusion/ej2-angular-image-editor'
import { enableRipple } from '@syncfusion/ej2-base'



import { Component,ViewChild } from '@angular/core';
import { Browser } from '@syncfusion/ej2-base';
import { Direction, ImageEditorComponent } from '@syncfusion/ej2-angular-image-editor';

@Component({
imports: [
        
        ImageEditorModule
    ],


standalone: true,
    selector: 'app-root',
    template: `<div class="e-section-control">
              <!-- To render Image Editor. -->
              <div id="wrapperDiv" style="width:550px;height:350px;">
                <ejs-imageeditor #imageEditor (created)="created()" [toolbar]="toolbar"></ejs-imageeditor>
              </div>
              <button class="e-btn e-primary" (click)="flipHorizontal()">Flip Horizontal</button>
              </div>`
})

export class AppComponent {
    @ViewChild('imageEditor')
    public imageEditorObj?: ImageEditorComponent;
    public toolbar: string[] = [];
      public created(): void {
      if (Browser.isDevice) {
        this.imageEditorObj?.open('./flower.png');        
      } 
      else {
        this.imageEditorObj?.open('./bridge.png');
      }
    }
    flipHorizontal(): void {
        this.imageEditorObj?.flip(Direction.Horizontal); // Horizontal flip
    }
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Straighten an image

The straightening feature in an Image Editor allows users to adjust an image by rotating it clockwise or counterclockwise. The rotating degree value should be within the range of -45 to +45 degrees for accurate straightening. Positive values indicate clockwise rotation, while negative values indicate counterclockwise rotation. The Image Editor control includes a straightenImage method, which allows you to adjust the degree of an image. This method takes one parameter that defines how the straightening should be carried out:

  • degree: Specifies the amount of rotation for straightening the image, within the range of -45 to +45 degrees. Positive values indicate clockwise rotation, while negative values indicate counterclockwise rotation.

Here is an example of straightening the image.

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ImageEditorModule, ImageEditorComponent } from '@syncfusion/ej2-angular-image-editor';
import { Browser, enableRipple } from '@syncfusion/ej2-base';
import { Component, ViewChild } from '@angular/core';

@Component({
  imports: [ImageEditorModule],
  standalone: true,
  selector: 'app-root',
  template: `<!-- To render Image Editor. -->
              <div id="wrapperDiv" style="width:550px;height:350px;">
                <ejs-imageeditor #imageEditor (created)="created()" [toolbar]="toolbar"></ejs-imageeditor>
                 <button class="e-btn e-primary" (click)="straightenClick()">Straighten</button>
              </div>`
})

export class AppComponent {
  @ViewChild('imageEditor')
  public imageEditorObj?: ImageEditorComponent;
  public toolbar: string[] = [];
  public created(): void {
    if (Browser.isDevice) {
      this.imageEditorObj?.open('./flower.png');
    }
    else {
      this.imageEditorObj?.open('./bridge.png');
    }
  }
  straightenClick(): void {
    this.imageEditorObj?.straightenImage(45);
  }
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Zoom an image in or out

The Image Editor allows you to magnify an image using the zoom method. This method allows you to zoom in and out of the image and provides a more detailed view of the image’s hidden areas. This method takes two parameters to perform zooming.

  • zoomFactor - Specifies a value to control the level of magnification applied to the image.
  • zoomPoint - Specifies the x and y coordinates of a point as ImageEditorPoint on the image to perform zooming. If zoomPoint is omitted, the zoom is performed relative to the center of the image.

Minimum and maximum zoom level

The minZoomFactor property allows you to specify the minimum level of zoom that is allowed for an image. By setting this property, you can prevent the image from being zoomed out beyond a certain point, ensuring that it remains visible and usable even at the smallest zoom level.

By default, the minZoomFactor value is set to 0.1, meaning that the image can be zoomed out up to 10 times its original size.

The maxZoomFactor property is a useful feature in the Image Editor that allows you to define the maximum level of zoom permitted for an image. This property sets a limit on how much the image can be magnified, preventing excessive zooming that may result in a loss of image quality or visibility.

By default, the maxZoomFactor value is set to 10, meaning that the image can be zoomed in up to 10 times its original size. This ensures that the zooming functionality remains within reasonable bounds and maintains the integrity of the image.

Here’s an example of zooming in and out in the Image Editor by setting the minZoomFactor and maxZoomFactor properties within the zoomSettings options.

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ImageEditorModule, ImageEditorComponent, ZoomSettingsModel } from '@syncfusion/ej2-angular-image-editor';
import { Browser, enableRipple } from '@syncfusion/ej2-base';
import { Component, ViewChild } from '@angular/core';

@Component({
    imports: [ImageEditorModule],
    standalone: true,
    selector: 'app-root',
    template: `<div class="e-section-control">
                <!-- To render Image Editor. -->
              <div id="wrapperDiv" style="width:550px;height:350px;">
                <ejs-imageeditor #imageEditor (created)="created()" [toolbar]="toolbar" [zoomSettings]="zoomSettings"></ejs-imageeditor>
              </div>
              <button class="e-btn e-primary" (click)="zoomInClick()">Zoom In</button>
              <button class="e-btn e-primary" (click)="zoomOutClick()">Zoom Out</button>
              </div>`
})

export class AppComponent {
    @ViewChild('imageEditor')
    public imageEditorObj?: ImageEditorComponent;
    public toolbar: string[] = [];
    public zoomLevel: number = 1;
    public zoomSettings: ZoomSettingsModel = { maxZoomFactor: 30, minZoomFactor: 0.1 };
    public created(): void {
        if (Browser.isDevice) {
            this.imageEditorObj?.open('./flower.png');
        }
        else {
            this.imageEditorObj?.open('./bridge.png');
        }
    }
    zoomInClick(): void {
        if (this.zoomLevel < 1) {
            this.zoomLevel += 0.1;
        }
        else {
            this.zoomLevel += 1;
        }
        const value: any = this.imageEditorObj?.zoomSettings.maxZoomFactor;
        if (this.zoomLevel > value) {
            this.zoomLevel = value;
        }
        this.imageEditorObj?.zoom(this.zoomLevel) // zoom in
    }
    zoomOutClick(): void {
        if (this.zoomLevel <= 1) {
            this.zoomLevel -= 0.1;
        }
        else {
            this.zoomLevel -= 1;
        }
        const value: any = this.imageEditorObj?.zoomSettings.minZoomFactor;
        if (this.zoomLevel < value) {
            this.zoomLevel = value;
        }
        this.imageEditorObj?.zoom(this.zoomLevel) // zoom out
    }
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Panning an image

The Image Editor allows you to pan an image when the image exceeds the canvas size or selection range. When zooming in on an image or applying a selection for cropping, it is common for the image to exceed the size of the canvas or exceed the selection range. So, panning is used to view the entire image, by clicking on the canvas and dragging it in the direction you want to move.

In the following example, you can enable panning using the pan method in the button click event.

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ImageEditorModule, ImageEditorComponent } from '@syncfusion/ej2-angular-image-editor';
import { Browser, enableRipple } from '@syncfusion/ej2-base';
import { Component, ViewChild } from '@angular/core';

@Component({
  imports: [ImageEditorModule],
  standalone: true,
  selector: 'app-root',
  template: `<div class="e-section-control">
              <!-- To render Image Editor. -->
              <div id="wrapperDiv" style="width:550px;height:350px;">
                <ejs-imageeditor #imageEditor (created)="created()" [toolbar]="toolbar"></ejs-imageeditor>
              </div>
              <button class="e-btn e-primary" (click)="panClick()">Pan</button>
              </div>`
})

export class AppComponent {
  @ViewChild('imageEditor')
  public imageEditorObj?: ImageEditorComponent;
  public toolbar: string[] = [];
  public zoomLevel: number = 6;
  public created(): void {
    if (Browser.isDevice) {
      this.imageEditorObj?.open('./flower.png');
    }
    else {
      this.imageEditorObj?.open('./bridge.png');
    }
  }
  panClick(): void {
    this.imageEditorObj?.zoom(this.zoomLevel) // zoom in
    this.imageEditorObj?.pan(true);
  }
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Panning event

The panning event is activated when the user begins dragging the image within the canvas. This event provides an opportunity to perform specific actions, like adjusting the position of an image, in response to the gesture of panning. This event uses PanEventArgs to handle the panning action when the user starts dragging the image.

The parameters available in the PanEventArgs event are:

  • PanEventArgs.startPoint - The x and y coordinates as ImageEditorPoint for the start point.
  • PanEventArgs.endpoint - The x and y coordinates as ImageEditorPoint for the end point.
  • PanEventArgs.cancel - Specifies a boolean value to cancel the panning action.

Zooming event

The zooming event is triggered when performing zooming on the image. This event can be used to perform certain actions, such as updating the position of the image. This event is passed an object that contains information about the zooming event, such as the amount of zooming performed. This event uses ZoomEventArgs to handle the zooming action in the image.

The parameters available in the zooming event are:

  • ZoomEventArgs.zoomPoint - The x and y coordinates as ImageEditorPoint for the zoom point.
  • ZoomEventArgs.previousZoomFactor - The previous zoom factor applied in the image editor.
  • ZoomEventArgs.currentZoomFactor - The current zoom factor to be applied in the image editor.
  • ZoomEventArgs.cancel - Specifies a boolean value to cancel the zooming action.
  • ZoomEventArgs.zoomTrigger - The type of zooming performed in the image editor (e.g., ZoomIn, ZoomOut, Reset).

Rotating event

The rotating event is triggered when performing rotating on the image. This event is passed an object that contains information about the rotating event, such as the amount of rotation performed. This event uses RotateEventArgs to handle the rotating action in the image.

The parameters available in the rotating event are:

  • RotateEventArgs.previousDegree - The degree of rotation before the recent rotation action was applied in the Image Editor.
  • RotateEventArgs.currentDegree - The current degree of rotation after the rotation action has been performed in the Image Editor.
  • RotateEventArgs.cancel - Specifies a boolean value to cancel the rotating action.

Flipping event

The flipping event is triggered when performing flipping on the image. This event is passed an object that contains information about the flipping event, such as the amount of flip performed. This event uses FlipEventArgs to handle the flipping action in the image.

The parameters available in the flipping event are:

  • FlipEventArgs.direction - The flip direction as ImageEditorDirection to be applied in the image editor.
  • FlipEventArgs.cancel - Specifies a boolean value to cancel the flip action.