Class StaticCanvas<EventSpec>

Static canvas class

demo

before:render

after:render

canvas:cleared

object:added

object:removed

Type Parameters

  • EventSpec extends StaticCanvasEvents = StaticCanvasEvents

Hierarchy

  • StaticCanvas_base
    • StaticCanvas

Implements

  • StaticCanvasOptions

Constructors

Properties

Accessors

Methods

Constructors

  • Type Parameters

    • EventSpec extends StaticCanvasEvents = StaticCanvasEvents

    Parameters

    • Optionalel: string | HTMLCanvasElement
    • Optionaloptions: TOptions<StaticCanvasOptions>

    Returns StaticCanvas<EventSpec>

Properties

__cleanupTask?: {
    kill: ((reason?: any) => void);
    (): void;
}
_objects: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[]
_offset: {
    left: number;
    top: number;
}
allowTouchScrolling: boolean

move to Canvas

backgroundColor: string | TFiller

Background color of canvas instance.


backgroundImage?: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>

Background image of canvas instance. since 2.4.0 image caching is active, please when putting an image as background, add to the canvas property a reference to the canvas it is on. Otherwise the image cannot detect the zoom vale. As an alternative you can disable image objectCaching


backgroundVpt: boolean

if set to false background image is not affected by viewport transform

1.6.3

we should really find a different way to do this


clipPath?: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>

a fabricObject that, without stroke define a clipping area with their shape. filled in black the clipPath object gets used when the canvas has rendered, and the context is placed in the top left corner of the canvas. clipPath will clip away controls, if you do not want this to happen use controlsAboveOverlay = true

controlsAboveOverlay: boolean

move to Canvas

destroyed?: boolean

If true the Canvas is in the process or has been disposed/destroyed. No more rendering operation will be executed on this canvas.

disposed?: boolean

Started the process of disposing but not done yet. WIll likely complete the render cycle already scheduled but stopping adding more.

elements: StaticCanvasDOMManager
enableRetinaScaling: boolean

When true, canvas is scaled by devicePixelRatio for better rendering on retina screens


hasLostContext: boolean
height: number

Height in virtual/logical pixels of the canvas. The canvas can be taller than width if retina scaling is active

imageSmoothingEnabled: boolean

Indicates whether this canvas will use image smoothing, this is on by default in browsers


includeDefaultValues: boolean

Indicates whether toObject/toDatalessObject should include default values if set to false, takes precedence over the object value.


nextRenderHandle: number
overlayColor: string | TFiller

Overlay color of canvas instance.

1.3.9


overlayImage?: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>

Overlay image of canvas instance. since 2.4.0 image caching is active, please when putting an image as overlay, add to the canvas property a reference to the canvas it is on. Otherwise the image cannot detect the zoom vale. As an alternative you can disable image objectCaching


overlayVpt: boolean

if set to false overlay image is not affected by viewport transform

1.6.3

we should really find a different way to do this


renderOnAddRemove: boolean

Indicates whether StaticCanvas#add, StaticCanvas#insertAt and StaticCanvas#remove, StaticCanvas#moveTo, StaticCanvas#clear and many more, should also re-render canvas. Disabling this option will not give a performance boost when adding/removing a lot of objects to/from canvas at once since the renders are queued and executed one per frame. Disabling is suggested anyway and managing the renders of the app manually is not a big effort ( canvas.requestRenderAll() ) Left default to true to do not break documentation and old app, fiddles.


skipControlsDrawing: boolean

When true control drawing is skipped. This boolean is used to avoid toDataURL to export controls. Usage of this boolean to build up other flows and features is not supported

false
skipOffscreen: boolean

Based on vptCoords and object.aCoords, skip rendering of objects that are not included in current viewport. May greatly help in applications with crowded canvas and use of zoom/pan If One of the corner of the bounding box of the object is on the canvas the objects get rendered.

true
svgViewportTransformation: boolean

When true, getSvgTransform() will apply the StaticCanvas.viewportTransform to the SVG transformation. When true, a zoomed canvas will then produce zoomed SVG output.


viewportTransform: TMat2D

The transformation (a Canvas 2D API transform matrix) which focuses the viewport

canvas.viewportTransform = [1, 0, 0, 1, 0, 0];
canvas.viewportTransform = [0.7, 0, 0, 0.7, 50, 50];

vptCoords: TCornerPoint

The viewport bounding box in scene plane coordinates, see calcViewportBoundaries

width: number

Width in virtual/logical pixels of the canvas. The canvas can be larger than width if retina scaling is active

ownDefaults: TOptions<StaticCanvasOptions>

Accessors

  • get contextContainer(): CanvasRenderingContext2D
  • Returns CanvasRenderingContext2D

  • get lowerCanvasEl(): HTMLCanvasElement
  • A reference to the canvas actual HTMLCanvasElement. Can be use to read the raw pixels, but never write or manipulate

    Returns HTMLCanvasElement

Methods

  • Parameters

    • obj: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>

    Returns void

  • Parameters

    • obj: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>

    Returns void

  • Returns void

  • Parameters

    • key: string
    • value: any

    Returns void

  • Protected

    Internal use only

    Parameters

    • dimensions: Partial<TSize | CSSDimensions>
    • Optional__namedParameters: TCanvasSizeOptions

    Returns void

  • Protected

    Sets object's properties from options, for initialization only

    Parameters

    • Optionaloptions: any

      Options object

    Returns void

  • Pan viewport so as to place point at top left corner of canvas

    Parameters

    • point: Point

      to move to

    Returns void

  • Parameters

    • Rest...objects: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[]

    Returns number

  • Parameters

    • object: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>
    • Optionalintersecting: boolean

    Returns boolean

  • Parameters

    • object: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>

    Returns boolean

  • Calculates canvas element offset relative to the document This method is also attached as "resize" event handler of window

    Returns {
        left: number;
        top: number;
    }

    • left: number
    • top: number
  • Calculate the position of the 4 corner of canvas with current viewportTransform. helps to determinate when an object is in the current rendering viewport

    Returns TCornerPoint

  • Returns void

  • Centers object vertically and horizontally in the canvas

    Parameters

    • object: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>

      Object to center vertically and horizontally

    Returns void

  • Centers object horizontally in the canvas

    Parameters

    • object: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>

    Returns void

  • Centers object vertically in the canvas

    Parameters

    • object: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>

      Object to center vertically

    Returns void

  • Clears all contexts (background, main, top) of an instance

    Returns void

  • Clears specified context of canvas element

    Parameters

    • ctx: CanvasRenderingContext2D

      Context to clear

    Returns void

  • Clones canvas instance

    Parameters

    • Optionalproperties: string[]

      Array of properties to include in the cloned canvas and children

    Returns Promise<StaticCanvas<EventSpec>>

  • Clones canvas instance without cloning existing data. This essentially copies canvas dimensions since loadFromJSON does not affect canvas size.

    Returns this

  • Parameters

    • __namedParameters: TBBox
    • Optional__namedParameters: {
          includeIntersecting?: boolean;
      }
      • OptionalincludeIntersecting?: boolean

    Returns InteractiveFabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[]

  • Returns number

  • Parameters

    • object: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>
    • Optionaldeep: boolean

    Returns boolean

  • Parameters

    • options: TSVGExportOptions

    Returns string

  • Creates markup containing SVG font faces, font URLs for font faces must be collected by developers and are not extracted from the DOM by fabricjs

    Returns string

  • Creates markup containing SVG referenced elements like patterns, gradients etc.

    Returns string

  • Waits until rendering has settled to destroy the canvas

    Returns Promise<boolean>

    a promise resolving to true once the canvas has been destroyed or to false if the canvas has was already destroyed

    if aborted by a consequent call

  • Paint the cached clipPath on the lowerCanvasEl

    Parameters

    • ctx: CanvasRenderingContext2D

      Context to render on

    • clipPath: TCachedFabricObject

    Returns void

  • Parameters

    • _ctx: CanvasRenderingContext2D

    Returns void

  • Parameters

    • object: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>
    • idx: number
    • Optionalintersecting: boolean

    Returns number

  • Parameters

    • object: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>
    • idx: number
    • Optionalintersecting: boolean

    Returns number

  • Fires event with an optional options object

    Type Parameters

    • K extends keyof CanvasEvents

    Parameters

    • eventName: K

      Event name to fire

    • Optionaloptions: CanvasEvents[K]

      Options object

    Returns void

  • Parameters

    • callback: ((object: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>, index: number, array: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[]) => any)
        • (object, index, array): any
        • Parameters

          • object: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>
          • index: number
          • array: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[]

          Returns any

    Returns void

  • Basic getter

    Parameters

    • property: string

      Property name

    Returns any

    value of a property

  • Returns coordinates of a center of canvas. Returned value is an object with top and left properties

    Returns {
        left: number;
        top: number;
    }

    object with "top" and "left" number values

    • left: number
    • top: number

    migrate to getCenterPoint

  • Returns coordinates of a center of canvas.

    Returns Point

  • Returns context of canvas where objects are drawn

    Returns CanvasRenderingContext2D

  • Returns <canvas> element corresponding to this instance

    Returns HTMLCanvasElement

  • Returns canvas height (in px)

    Returns number

  • Parameters

    • Rest...types: string[]

    Returns FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[]

  • Calculate the point in canvas that correspond to the center of actual viewport.

    Returns Point

    vpCenter, viewport center

  • Returns canvas width (in px)

    Returns number

  • Returns canvas zoom level

    Returns number

  • Parameters

    • Optionalel: string | HTMLCanvasElement

    Returns void

  • Parameters

    • index: number
    • Rest...objects: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[]

    Returns number

  • Returns boolean

  • Parameters

    • index: number

    Returns FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>

  • Populates canvas with data from the specified JSON. JSON format must conform to the one of fabric.Canvas#toJSON

    IMPORTANT: It is recommended to abort loading tasks before calling this method to prevent race conditions and unnecessary networking

    Parameters

    • json: string | Record<string, any>

      JSON string or object

    • Optionalreviver: (<T>(serializedObj: Record<string, any>, instance: T) => void)

      Method for further parsing of JSON elements, called after each fabric object created.

        • <T>(serializedObj, instance): void
        • Type Parameters

          • T extends
                | TFiller
                | FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>
                | FabricObject<Partial<ObjectProps>, SerializedObjectProps, ObjectEvents>
                | BaseFilter<string, object>
                | Shadow

          Parameters

          • serializedObj: Record<string, any>
          • instance: T

          Returns void

    • Optionaloptions: Abortable

      options

    Returns Promise<StaticCanvas<EventSpec>>

    instance

    demo

    canvas.loadFromJSON(json).then((canvas) => canvas.requestRenderAll());
    
    canvas.loadFromJSON(json, function(o, object) {
    // `o` = json object
    // `object` = fabric.Object instance
    // ... do some stuff ...
    }).then((canvas) => {
    ... canvas is restored, add your code.
    });
  • Parameters

    • object: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>
    • index: number

    Returns boolean

  • Unsubscribe all event listeners for eventname. Do not use this pattern. You could kill internal fabricJS events. We know we should have protected events for internal flows, but we don't have yet

    Type Parameters

    • K extends keyof CanvasEvents

    Parameters

    • eventName: K

      event name (eg. 'after:render')

    Returns void

  • unsubscribe an event listener

    Type Parameters

    • K extends keyof CanvasEvents

    Parameters

    • eventName: K

      event name (eg. 'after:render')

    • handler: TEventCallback<any>

      event listener to unsubscribe

    Returns void

  • unsubscribe event listeners

    Parameters

    • handlers: EventRegistryObject<CanvasEvents>

      handlers key/value pairs (eg. {'after:render': handler, 'selection:cleared': handler})

    Returns void

  • unsubscribe all event listeners

    Returns void

  • Observes specified event

    Type Parameters

    • K extends keyof CanvasEvents
    • E extends
          | TPointerEventInfo<TPointerEvent>
          | TPointerEventInfo<TPointerEvent> & {
              currentSubTargets: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[];
              currentTarget?: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
              isClick: boolean;
          }
          | TPointerEventInfo<WheelEvent>
          | TPointerEventInfo<TPointerEvent> & InEvent
          | TPointerEventInfo<TPointerEvent> & OutEvent
          | {
              path: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
          }
          | {
              path: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
          }
          | {
              drawables: {
                  backgroundImage?: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
                  overlayImage?: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
              };
              path: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
              subTargets: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[];
              targets: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[];
          }
          | {
              target: IText<Partial<ITextProps>, SerializedITextProps, ITextEvents>;
          }
          | {
              target: IText<Partial<ITextProps>, SerializedITextProps, ITextEvents>;
          }
          | {
              target: IText<Partial<ITextProps>, SerializedITextProps, ITextEvents>;
          } & Partial<TEvent<TPointerEvent>>
          | {
              target: IText<Partial<ITextProps>, SerializedITextProps, ITextEvents>;
          }
          | {
              ctx: CanvasRenderingContext2D;
          }
          | {
              ctx: CanvasRenderingContext2D;
          }
          | LayoutBeforeEvent & {
              target: Group;
          }
          | LayoutAfterEvent & {
              target: Group;
          }
          | {
              target: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
          }
          | {
              target: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
          }
          | DragEventData
          | DragEventData & InEvent
          | DragEventData & OutEvent
          | TEventWithTarget<DragEvent>
          | DropEventData
          | SimpleEventHandler<Event>
          | TEvent<TPointerEvent> & {
              transform: Transform;
          }
          | BasicTransformEvent<TPointerEvent> & {
              target: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
          }
          | BasicTransformEvent<TPointerEvent> & {
              target: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
          }
          | BasicTransformEvent<TPointerEvent> & {
              target: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
          }
          | BasicTransformEvent<TPointerEvent> & {
              target: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
          }
          | BasicTransformEvent<TPointerEvent> & {
              target: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
          }
          | BasicTransformEvent<TPointerEvent> & {
              target: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
          }
          | BasicTransformEvent<TPointerEvent> & {
              target: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
          } & ModifyPathEvent
          | ModifiedEvent<TPointerEvent>
          | Partial<TEvent<TPointerEvent>> & {
              selected: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[];
          }
          | Partial<TEvent<TPointerEvent>> & {
              deselected: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[];
              selected: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[];
          }
          | Partial<TEvent<TPointerEvent>> & {
              deselected: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[];
          }
          | Partial<TEvent<TPointerEvent>> & {
              deselected: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[];
          }

    Parameters

    • eventName: K

      Event name (eg. 'after:render')

    • handler: TEventCallback<E>

      Function that receives a notification when an event of the specified type occurs

    Returns VoidFunction

    disposer

    on

  • Parameters

    • handlers: EventRegistryObject<CanvasEvents>

    Returns VoidFunction

  • Observes specified event once

    Type Parameters

    • K extends keyof CanvasEvents
    • E extends
          | TPointerEventInfo<TPointerEvent>
          | TPointerEventInfo<TPointerEvent> & {
              currentSubTargets: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[];
              currentTarget?: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
              isClick: boolean;
          }
          | TPointerEventInfo<WheelEvent>
          | TPointerEventInfo<TPointerEvent> & InEvent
          | TPointerEventInfo<TPointerEvent> & OutEvent
          | {
              path: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
          }
          | {
              path: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
          }
          | {
              drawables: {
                  backgroundImage?: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
                  overlayImage?: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
              };
              path: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
              subTargets: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[];
              targets: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[];
          }
          | {
              target: IText<Partial<ITextProps>, SerializedITextProps, ITextEvents>;
          }
          | {
              target: IText<Partial<ITextProps>, SerializedITextProps, ITextEvents>;
          }
          | {
              target: IText<Partial<ITextProps>, SerializedITextProps, ITextEvents>;
          } & Partial<TEvent<TPointerEvent>>
          | {
              target: IText<Partial<ITextProps>, SerializedITextProps, ITextEvents>;
          }
          | {
              ctx: CanvasRenderingContext2D;
          }
          | {
              ctx: CanvasRenderingContext2D;
          }
          | LayoutBeforeEvent & {
              target: Group;
          }
          | LayoutAfterEvent & {
              target: Group;
          }
          | {
              target: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
          }
          | {
              target: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
          }
          | DragEventData
          | DragEventData & InEvent
          | DragEventData & OutEvent
          | TEventWithTarget<DragEvent>
          | DropEventData
          | SimpleEventHandler<Event>
          | TEvent<TPointerEvent> & {
              transform: Transform;
          }
          | BasicTransformEvent<TPointerEvent> & {
              target: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
          }
          | BasicTransformEvent<TPointerEvent> & {
              target: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
          }
          | BasicTransformEvent<TPointerEvent> & {
              target: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
          }
          | BasicTransformEvent<TPointerEvent> & {
              target: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
          }
          | BasicTransformEvent<TPointerEvent> & {
              target: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
          }
          | BasicTransformEvent<TPointerEvent> & {
              target: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
          }
          | BasicTransformEvent<TPointerEvent> & {
              target: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
          } & ModifyPathEvent
          | ModifiedEvent<TPointerEvent>
          | Partial<TEvent<TPointerEvent>> & {
              selected: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[];
          }
          | Partial<TEvent<TPointerEvent>> & {
              deselected: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[];
              selected: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[];
          }
          | Partial<TEvent<TPointerEvent>> & {
              deselected: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[];
          }
          | Partial<TEvent<TPointerEvent>> & {
              deselected: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[];
          }

    Parameters

    • eventName: K

      Event name (eg. 'after:render')

    • handler: TEventCallback<E>

      Function that receives a notification when an event of the specified type occurs

    Returns VoidFunction

    disposer

    once

  • Parameters

    • handlers: EventRegistryObject<CanvasEvents>

    Returns VoidFunction

  • Pans viewpoint relatively

    Parameters

    • point: Point

      (position vector) to move by

    Returns void

  • Parameters

    • Rest...objects: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[]

    Returns FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[]

  • Renders the canvas

    Returns void

  • Renders background, objects, overlay and controls.

    Parameters

    • ctx: CanvasRenderingContext2D
    • objects: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>[]

      to render

    Returns void

  • Append a renderAll request to next animation frame. unless one is already in progress, in that case nothing is done a boolean flag will avoid appending more.

    Returns void

  • Parameters

    • object: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>
    • Optionalintersecting: boolean

    Returns boolean

  • Parameters

    • object: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>

    Returns boolean

  • Sets property to a given value. When changing position/dimension -related properties (left, top, scale, angle, etc.) set does not update position of object's borders/controls. If you need to update those, call setCoords().

    Parameters

    • key: string | Record<string, any>

      Property name or object (if object, iterate over the object properties)

    • Optionalvalue: any

      Property value (if function, the value is passed into it and its return value is used as a new one)

    Returns this

  • Sets dimensions (width, height) of this canvas instance. when options.cssOnly flag active you should also supply the unit of measure (px/%/em)

    Parameters

    • dimensions: Partial<CSSDimensions>

      Object with width/height properties

    • Optionaloptions: {
          backstoreOnly?: false;
          cssOnly?: true;
      }

      Options object

      • OptionalbackstoreOnly?: false

        Set the given dimensions only as canvas backstore dimensions

      • OptionalcssOnly?: true

        Set the given dimensions only as css dimensions

    Returns void

  • Parameters

    • dimensions: Partial<TSize>
    • Optionaloptions: {
          backstoreOnly?: true;
          cssOnly?: false;
      }
      • OptionalbackstoreOnly?: true
      • OptionalcssOnly?: false

    Returns void

  • Parameters

    • dimensions: Partial<TSize>
    • Optionaloptions: never

    Returns void

  • s Sets height of this canvas instance

    Parameters

    • value: number

      Value to set height to

    • Optionaloptions: {
          backstoreOnly?: true;
          cssOnly?: false;
      }

      Options object

      • OptionalbackstoreOnly?: true

        Set the given dimensions only as canvas backstore dimensions

      • OptionalcssOnly?: false

        Set the given dimensions only as css dimensions

    Returns void

    will be removed in 7.0

  • Parameters

    • value: string | number
    • Optionaloptions: {
          backstoreOnly?: false;
          cssOnly?: true;
      }
      • OptionalbackstoreOnly?: false
      • OptionalcssOnly?: true

    Returns void

  • Sets viewport transformation of this canvas instance

    Parameters

    • vpt: TMat2D

      a Canvas 2D API transform matrix

    Returns void

  • Sets width of this canvas instance

    Parameters

    • value: number

      Value to set width to

    • Optionaloptions: {
          backstoreOnly?: true;
          cssOnly?: false;
      }

      Options object

      • OptionalbackstoreOnly?: true

        Set the given dimensions only as canvas backstore dimensions

      • OptionalcssOnly?: false

        Set the given dimensions only as css dimensions

    Returns void

    will be removed in 7.0

  • Parameters

    • value: string | number
    • Optionaloptions: {
          backstoreOnly?: false;
          cssOnly?: true;
      }
      • OptionalbackstoreOnly?: false
      • OptionalcssOnly?: true

    Returns void

  • Sets zoom level of this canvas instance

    Parameters

    • value: number

      to set zoom to, less than 1 zooms out

    Returns void

  • Returns number

  • Create a new HTMLCanvas element painted with the current canvas content. No need to resize the actual one or repaint it. Will transfer object ownership to a new canvas, paint it, and set everything back. This is an intermediary step used to get to a dataUrl but also it is useful to create quick image copies of a canvas without passing for the dataUrl string

    Parameters

    • Optionalmultiplier: number

      a zoom factor.

    • Optionaloptions: TToCanvasElementOptions

      Cropping informations

    Returns HTMLCanvasElement

  • Returns dataless JSON representation of canvas

    Parameters

    • OptionalpropertiesToInclude: string[]

      Any properties that you might want to additionally include in the output

    Returns any

    json string

  • Returns dataless object representation of canvas

    Parameters

    • OptionalpropertiesToInclude: string[]

      Any properties that you might want to additionally include in the output

    Returns any

    object representation of an instance

  • Exports canvas element to a dataurl image. Note that when multiplier is used, cropping is scaled appropriately

    Parameters

    • Optionaloptions: TDataUrlOptions

      Options object

    Returns string

    Returns a data: URL containing a representation of the object in the format specified by options.format

    demo

    var dataURL = canvas.toDataURL({
    format: 'jpeg',
    quality: 0.8
    });
    var dataURL = canvas.toDataURL({
    format: 'png',
    left: 100,
    top: 100,
    width: 200,
    height: 200
    });
    var dataURL = canvas.toDataURL({
    format: 'png',
    multiplier: 2
    });
    var myObject;
    var dataURL = canvas.toDataURL({
    filter: (object) => object.isContainedWithinObject(myObject) || object.intersectsWithObject(myObject)
    });
  • Toggles specified property from true to false or from false to true

    Parameters

    • property: string

      Property to toggle

    Returns this

  • Returns Object representation of canvas this alias is provided because if you call JSON.stringify on an instance, the toJSON object will be invoked if it exists. Having a toJSON method means you can do JSON.stringify(myCanvas)

    Returns any

    JSON compatible object

    demo

    var json = canvas.toJSON();
    
    var json = canvas.toJSON(['lockMovementX', 'lockMovementY', 'lockRotation', 'lockScalingX', 'lockScalingY']);
    
    var json = canvas.toJSON();
    
  • Returns object representation of canvas

    Parameters

    • OptionalpropertiesToInclude: string[]

      Any properties that you might want to additionally include in the output

    Returns any

    object representation of an instance

  • Returns a string representation of an instance

    Returns string

    string representation of an instance

  • Returns SVG representation of canvas

    Parameters

    • Optionaloptions: TSVGExportOptions

      Options object for SVG output

    • Optionalreviver: TSVGReviver

      Method for further parsing of svg elements, called after each fabric object converted into svg representation.

    Returns string

    SVG string

    demo

    var svg = canvas.toSVG();
    
    var svg = canvas.toSVG({suppressPreamble: true});
    
    var svg = canvas.toSVG({
    viewBox: {
    x: 100,
    y: 100,
    width: 200,
    height: 300
    }
    });
    var svg = canvas.toSVG({encoding: 'ISO-8859-1'});
    
    var svg = canvas.toSVG(null, function(svg) {
    return svg.replace('stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; ', '');
    });
  • Centers object vertically and horizontally in the viewport

    Parameters

    • object: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>

      Object to center vertically and horizontally

    Returns void

  • Centers object horizontally in the viewport, object.top is unchanged

    Parameters

    • object: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>

      Object to center vertically and horizontally

    Returns void

  • Centers object Vertically in the viewport, object.top is unchanged

    Parameters

    • object: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>

      Object to center vertically and horizontally

    Returns void

  • Sets zoom level of this canvas instance, the zoom centered around point meaning that following zoom to point with the same point will have the visual effect of the zoom originating from that point. The point won't move. It has nothing to do with canvas center or visual center of the viewport.

    Parameters

    • point: Point

      to zoom with respect to

    • value: number

      to set zoom to, less than 1 zooms out

    Returns void

  • Returns Record<string, any>