Class ImageComponent

Represents a raster image.

Example: Adding images:

import { Editor, ImageComponent, Mat33 } from 'js-draw';
const editor = new Editor(document.body);

//
// Adding an image
//
const myHtmlImage = new Image();
myHtmlImage.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAACCAYAAAB/qH1jAAAAKklEQVQIW2Ns022zZGRgfPnz8s8HDQwN/xgZgKBDu0PuL8tf5d8/fz8FAOiDD1H2gfpGAAAAAElFTkSuQmCC';

const rotated45Degrees = Mat33.zRotation(Math.PI / 4); // A 45 degree = pi/4 radian rotation
const scaledByFactorOf100 = Mat33.scaling2D(100);
// Scale **and** rotate
const transform = rotated45Degrees.rightMul(scaledByFactorOf100);

const imageComponent = await ImageComponent.fromImage(myHtmlImage, transform);
await editor.dispatch(editor.image.addElement(imageComponent));

//
// Make a new image from the editor itself (with editor.toDataURL)
//
const toolbar = editor.addToolbar();
toolbar.addActionButton('From editor', async () => {
	const dataUrl = editor.toDataURL();
	const htmlImage = new Image();
	htmlImage.src = dataUrl;

	const imageComponent = await ImageComponent.fromImage(htmlImage, Mat33.identity);
	await editor.addAndCenterComponents([ imageComponent ]);
});

Hierarchy (view full)

Constructors

Properties

contentBBox: Rect2

The bounding box of this component. getBBox, by default, returns contentBBox. This must be set by components.

If this changes, EditorImage.queueRerenderOf should be called for this object (provided that this object has been added to the editor.)

Note: This value is ignored if getSizingMode returns FillScreen or FillImage.

lastChangedTime: number

The timestamp (milliseconds) at which the component was last changed (i.e. created/translated).

Methods

  • Parameters

    Returns boolean

    true if this component intersects rect -- it is entirely contained within the rectangle or one of the rectangle's edges intersects this component.

    The default implementation assumes that this.getExactBBox() returns a tight bounding box -- that any horiziontal/vertical line that intersects this' boounding box also intersects a point in this component. If this is not the case, components must override this function.

  • Renders this component onto the given canvas.

    If visibleRect is given, it should be the region of canvas that is visible -- rendering anything outside of visibleRect should have no visible effect on the resultant image.

    For optimal performance, implementers should call canvas.startObject and canvas.endObject before and after rendering.

    Parameters

    Returns void

  • Convert the component to an object that can be passed to JSON.stringify.

    Do not rely on the output of this function to take a particular form — this function's output can change form without a major version increase.

    Returns {
        data:
            | string
            | number
            | any[]
            | Record<string, any>;
        id: string;
        loadSaveData: LoadSaveDataTable;
        name: string;
        zIndex: number;
    }

    • data:
          | string
          | number
          | any[]
          | Record<string, any>
    • id: string
    • loadSaveData: LoadSaveDataTable
    • name: string
    • zIndex: number
  • Return null iff this object cannot be safely serialized/deserialized.

    Returns {
        height: number;
        label: undefined | string;
        src: string;
        transform: Mat33Array;
        width: number;
    }

    • height: number
    • label: undefined | string
    • src: string
    • transform: Mat33Array
    • width: number

      Store the width and height for bounding box computations while the image is loading.

OpenSource licenses