Clear entire, transformed HTML5 Canvas while preserving context transform

前端 未结 2 2063
走了就别回头了
走了就别回头了 2020-12-16 00:38

I want to zoom and pan an HTML5 Canvas by transforming the context using translate() and scale(), clearing the canvas, and then redrawing. Note tha

2条回答
  •  离开以前
    2020-12-16 01:10

    Keeping track of all the transformation information like you are presumably doing is what several others so far have done (like cake.js and my own library, for two). I think doing this will pretty much be an inevitability for any large canvas library.

    Ilmari of cake.js even complained to mozilla: https://bugzilla.mozilla.org/show_bug.cgi?id=408804

    You could instead call save/restore around your clear method:

    // I have lots of transforms right now
    ctx.save();
    ctx.setTransform(1,0,0,1,0,0);
    // Will always clear the right space
    ctx.clearRect(0,0,ctx.canvas.width,ctx.canvas.height);
    ctx.restore();
    // Still have my old transforms
    

    Won't that satisfy your case?

提交回复
热议问题