码迷,mamicode.com
首页 > 其他好文 > 详细

canvas缓动3

时间:2016-11-29 22:47:21      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:ext   旋转   document   draw   function   restore   begin   animation   lan   

这次缓动的是旋转旋转,写完之后才知道原来rotate是绕原点旋转,而且还带着rect本身的X与Y一起,所以我采用了translate达到位移效果,以免旋转到画布外面去,画完之后效果惨不忍睹,原来是忘记还原变形了,最后加上了save和restore才让效果达到

var canvas = document.getElementById("canvas");
var cxt=canvas.getContext("2d");
var RectX=0,RectY=0,esaing=0.01;
var rotation=0,targetrotation=360;
function draw(){
cxt.beginPath();
cxt.clearRect(0,0,canvas.width,canvas.height);
cxt.fillStyle="red";
cxt.save()
cxt.translate(50,50);
rotation+=(targetrotation-rotation)*esaing;
console.log(rotation)

cxt.rotate(rotation*Math.PI/180)
cxt.rect(0,0,20,20);
cxt.fill();
cxt.restore()
cxt.closePath();
var id = requestAnimationFrame(draw);
if(targetrotation-rotation<=0.1)
{
cancelAnimationFrame(id);
}
}
draw();

canvas缓动3

标签:ext   旋转   document   draw   function   restore   begin   animation   lan   

原文地址:http://www.cnblogs.com/MyZsy/p/6115049.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!