Images
Custom width/height
If you want to maintain proportions of an image, use the scale
property (the scale is a multiple, with 1
as the baseline).
Using an image element
You can also use an image DOM element as the value for the source
property.
The load
event
In jCanvas, images are drawn asynchronously because they must load before they can be drawn. In some cases, this isn’t an issue because the image loads fast enough, however this isn’t always the case.
To address this, jCanvas allows you to run a callback function once the image has loaded, using the load
event.
Note that the load
event fires when the image is initially drawn, but also when it is redrawn. Therefore, dragging the image (for example) will cause the load
event to fire continuously (as you are dragging).
Cropping
Including the sx
, sy
, sWidth
, or sHeight
properties allows you to choose which section of the image to crop.
sx:
Defines the x-position of the crop regionsy:
Defines the y-position of the crop regionsWidth:
Defines the width of the crop regionsHeight:
Defines the height of the crop region
By default, the sx
and sy
properties defines the center of the crop region (for consistency). To change this, set the cropFromCenter
property to false
.
Now, the sx
and sy
properties define the top-left corner of the crop region, as you might expect.
Images and Layers
Beginning with version 13.04.05, image layers will always load completely before successive layers are drawn, as long as you use addLayer()
in conjunction with drawLayers()
:
Notes
Internet Explorer 9 has a bug which prevents images from drawing on the canvas if they have not loaded. To fix this, use an existing <img>
DOM element as the value for the source
property.