Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canvas bug - no rotation at 90 degrees in Chrome

I'm trying to draw a rotated and scaled circle in canvas.

When I rotate it by exactly 90 degrees, it does not rotate! It rotates at 89, 89.9. But not at exactly 90 (edit: breaks at 90 +-0.013, starts working at +-0.014). The problem only appears in Chrome (latest 29.0.1547.76 m), while Firefox and IE work fine.

c.save();
    c.translate(100, 100);
    r += 5;
    c.rotate(r * inRads);   // when r = 90, it renders as if r = 0
    c.scale(1, 2);

    c.beginPath();
    c.arc(0, 0, 20, 0, 2*Math.PI);
    c.closePath();
    c.fillStyle = '#f00';
    c.fill();
c.restore();

This is GIF of how it looked: chrome rotation 90 degrees bug

See the reproduced problem here: http://jsfiddle.net/qeWcE/1/ It is fixed now.

Is there a workaround for this?

like image 843
psycho brm Avatar asked Jan 20 '26 21:01

psycho brm


1 Answers

This seems to help:

// fixes bug when r = (90 +-0.013) jumps to r = 0
function chrome_fix_rotation_bug(r)
{
    var rr = Math.round(r);
    if (Math.abs(rr-r) < 0.02 && rr%90==0) return rr+0.02;
    return r;
}

// use like this:
r = chrome_fix_rotation_bug(r);
c.rotate(r * inRads)
like image 97
psycho brm Avatar answered Jan 22 '26 18:01

psycho brm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!