search

Home  >  Q&A  >  body text

javascript - About canvas rotation

$("#bt-lottery").one("click",function(){
            var reg=1;
            setInterval(function(){
                ctx.save();
                ctx.translate(249.5,249.5);//将原点移动到画布中心
                ctx.rotate(reg*Math.PI/180);
                ctx.clearRect(-pin.width/2,-pin.height/2,pin.width,pin.height);
                ctx.drawImage(pin,-pin.width/2,-pin.height/2);
                ctx.restore();
                reg++;
            },5);
        });

I want to realize the function of the pointer (pin.png) rotating on the turntable (pan.png), but if ctx.clearRect(); will appearAs shown in the figure; The result I want is , how to achieve it? ? Ask for advice

女神的闺蜜爱上我女神的闺蜜爱上我2748 days ago819

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-06-28 09:30:14

    Due to insufficient information, I can only give my judgment.

    1. png is not transparent, the probability is very small

    2. You are divided into two canvases, but the canvas with the pointer has a background color, the probability is also very small

    3. Code problem:

    setInterval(function(){
        ctx.clearRect(0,0,canvas.width,canvas.height);
        //这里你需要绘制背景图片(转盘),或者将转盘作为离屏canvas分离出去(因为转盘不会变动),只绘制指针。 ---我猜你缺少了这一步。。
        ctx.save();
        ctx.translate(249.5,249.5);//将原点移动到画布中心
        //ctx.clearRect(-pin.width/2,-pin.height/2,pin.width,pin.height);至于为什么清理出来的区域是个圆形跟你代码执行的顺序有关,先旋转后清理和先清理后旋转是不一样的。
        ctx.rotate(reg*Math.PI/180);
        ctx.drawImage(pin,-pin.width/2,-pin.height/2);
        ctx.restore();
        reg++;
    },5);

    reply
    0
  • Cancelreply