- Translate the context to a point on the canvas around which the object should rotate.
- The rotation of the context.
- Or:
- Translate the context to a negative offset inside the object for the center of rotation, and then draw the object at
0,0 or - Draw the image using a negative offset inside the object for the center of rotation.
eg.
ctx.save(); ctx.translate( canvasLocX, canvasLocY ); ctx.rotate( ballRotationInRadians ); ctx.drawImage( ball_img, -ballCenterX, -ballCenterY ); ctx.restore();
Please note: if you need absolute speed, instead of saving and restoring the canvas (processing many properties that you have not changed), you can simply cancel your work:
ctx.translate( canvasLocX, canvasLocY ); ctx.rotate( ballRotationInRadians ); ctx.drawImage( ball_img, -ballCenterX, -ballCenterY ); ctx.rotate( -ballRotationInRadians ); ctx.translate( -canvasLocX, -canvasLocY );
The previous bit of premature optimization was blindly blown from someone else; I personally have not verified that this is correct.
Change I added an example of work here: http://phrogz.net/tmp/canvas_beachball.html
source share