How to rotate one image in a canvas?

前端 未结 6 867
萌比男神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:16

    Use .save() and .restore() (more information):

    link.onload=function(){
        ctx.save(); // save current state
        ctx.rotate(Math.PI); // rotate
        ctx.drawImage(link,x,y,20,20); // draws a chain link or dagger
        ctx.restore(); // restore original states (no rotation etc)
    }
    

提交回复
热议问题