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.
Quick Start
Section titled “Quick Start”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
Constructor
Section titled “Constructor”new Graphic(path, width, height)
Section titled “new Graphic(path, width, height)”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. |
Properties
Section titled “Properties”| Property | Description |
|---|---|
image |
The underlying browser HTMLImageElement. |
width |
The default drawing width. |
height |
The default drawing height. |
render(context, x, y, width, height)
Section titled “render(context, x, y, width, 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 × 64graphic.render(context, 24, 104, 64, 32); // Scaled to 64 × 32Use 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.