Rotate
MouseOver on canvas above
Code
<canvas id="canvas" width="200" height="200" style="background-color: #f9f9f9;cursor:pointer;"></canvas>
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var y=0,
rotationspeed=2,
animation;
function draw() {
ctx.clearRect(0,0, canvas.width, canvas.height);
y%=360;
y+=rotationspeed;
ctx.save();
ctx.translate(100,100);
ctx.rotate(y*Math.PI/180);
ctx.translate(-100,-100);
ctx.strokeStyle="red";
ctx.strokeRect(85,85,30,30);
ctx.restore();
ctx.strokeText(y+"deg",10,10); // To show the rotation
animation = requestAnimationFrame(draw);
}
canvas.addEventListener('mouseover', function() {
animation = requestAnimationFrame(draw);
});
canvas.addEventListener('mouseout', function() {
cancelAnimationFrame(animation);
});
</script>