How to rotate one image in a canvas?

前端 未结 6 825
萌比男神i
萌比男神i 2020-12-20 15:26

I am making an HTML5 canvas game, and I wish to rotate one of the images.

var link = new Image();
link.src=\'img/link.png\';
link.onload=function(){
    ctx.         


        
6条回答
  •  时光说笑
    2020-12-20 16:15

    You might want to put a translate(); there because the image is going to rotate around the origin and that is in the top left corner by default so you use the translate(); to change the origin.

    link.onload=function(){
        ctx.save();
        ctx.translate(x, y); // change origin
        ctx.rotate(Math.PI);
        ctx.drawImage(link,-10,-10,10,10);
        ctx.restore()
    }
    

提交回复
热议问题