Skip to content

Graphic

Graphic wraps one browser image with its logical dimensions and can draw the entire image to a canvas. Images creates and stores graphics from named image definitions; Sprite uses a graphic when it needs only part of an image.

const logo = new Graphic('/images/logo.png', 128, 64);
logo.render(context, 24, 24);
logo.render(context, 24, 104, 64, 32);
flowchart LR
	A[Graphic path width height] --> B[Create browser Image]
	B --> C[Set image source]
	D[render context x y width height] --> E[Draw complete image]
	C --> E
new Graphic(path, width, height);

Creates an Image with the supplied logical width and height, then starts loading path by assigning it to image.src.

Parameter Description
path URL of the image file.
width Default destination width in canvas coordinates.
height Default destination height in canvas coordinates.
Property Description
image The underlying browser HTMLImageElement.
width The default drawing width.
height The default drawing height.
graphic.render(context, x, y, width, height);

Draws the entire image at x, y. Omit width and height to use the dimensions supplied to the constructor.

graphic.render(context, 24, 24); // 128 × 64
graphic.render(context, 24, 104, 64, 32); // Scaled to 64 × 32

Use Images.render() when drawing a named image at its default size. Use Sprite to draw one source rectangle from a sheet rather than the entire image.