Canvas: Textures & Sprite animation
Sprites
Last updated
Sprites
Last updated
A sprite sheet is a simple bitmap or block images to a more sophisticated animated image as shown in the example below. Sprites often help in improving the performance of the application in many ways. I2djs provides a set of texture APIs to handle sprites efficiently and easily.
The below example explains the sprite animation in detail:
canvasLayer method invoked with ContainerID "#myCanvas". It returns the Canvas2D renderer instance.
Create texture instance using canvasLayer API - createTexture as shown below. In the below example we are creating a texture of size 100 * 100. and the Clip property of the createTexture gives details of the image block on the sprite image. This will help the renderer in extracting the exact pixel block from the sprite.
Clip: { sx: 0, sy: 0, swidth: 108, sheight: 140}
- sx - sprite x start pixel position, sy -sprite y start pixel position, swidth - sprite width pixels, sheight - sprite height pixel.
Create an image element with TextureInstance as the source and set the height, width, x, and y for the image element.
The result will be :
we need to animate the clip property of the texture instance to create the sprite animations. Move the sx and sy properties of the clip property continuously based on the animation time factor f to create the below animation.