var c=document.getElementById("theCircle")
function drawCircle(end=2*Math.PI){
height = window.innerHeight
width = window.innerWidth
var ctx=c.getContext("2d");
ctx.beginPath();
ctx.arc(width/2,height/2,radius,0,end,true);
ctx.lineWidth=end/5;
ctx.closePath();
ctx.stroke();
}
function counter(start,finish,speed){
var i = start
var animation = setInterval(function(){
i += speed
if(i>=finish){clearInterval(animation)}
drawCircle(i);
}, 20)
}
counter(0.0,2*Math.PI,0.05)
window.addEventListener('resize',posFix,false);
function posFix(){
c.height = window.innerHeight
c.width = window.innerWidth
drawCircle();
}
调整canvas元素的大小时,将删除内部绘图。正如你所看到的,我正在调用重新调整大小,但它似乎没有正常启动。
我错过了什么?
答案 0 :(得分:1)
正如评论中所讨论的,解决方案是清除resize事件中的动画间隔,然后重新开始动画。
var c = document.getElementById("theCircle"),
radius = 800;
posFix();
window.addEventListener('resize', posFix, false);
function posFix() {
c.height = window.innerHeight
c.width = window.innerWidth
clearInterval(animation);
counter(0.0, 2 * Math.PI, 0.05)
}
function drawCircle(end = 2 * Math.PI) {
height = window.innerHeight
width = window.innerWidth
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.arc(width / 2, height / 2, radius, 0, end, true);
ctx.lineWidth = end / 5;
ctx.closePath();
ctx.stroke();
}
var animation;
function counter(start, finish, speed) {
var i = start
animation = setInterval(function () {
i += speed
if (i >= finish) {
clearInterval(animation);
}
drawCircle(i);
}, 20);
}
jsfiddle:http://jsfiddle.net/27WVW/4/