HTML5 translate method, how to reset to default?

后端 未结 3 663
猫巷女王i
猫巷女王i 2020-12-05 13:49

Since HTML5\'s translate-method moves the origin of drawing relatively to its former origin apparently. (when I use ctx.translate(20,20) twice in a row I get the same result

相关标签:
3条回答
  • 2020-12-05 14:22

    You can do that by using .save() and .restore()

    ctx.save();
    ctx.translate(// do some translations);
    // draw whatever
    ctx.restore();
    

    You need to call save() to "save" the current contexts state. Then you can perform translations, rotations, etc. After your finished call restore() to reset the context's state to what it was when you initially called save().

    Live Demo

    MDN Tutorial also explaining it

    0 讨论(0)
  • 2020-12-05 14:32
    ctx.setTransform(1, 0, 0, 1, 0, 0);
    

    MDN setTransform documentation

    0 讨论(0)
  • 2020-12-05 14:36
    ctx.resetTransform();
    

    See MDN documentation for more info. It has very low browser compatibility.

    0 讨论(0)
提交回复
热议问题