html5: copy a canvas to image and back

后端 未结 3 1062
我寻月下人不归
我寻月下人不归 2020-11-28 09:57

I implemented a zoom in and out function on a canvas element. it works by scaling the canvas, translating it, and then redraw the whole scene again. the problem is that it t

3条回答
  •  旧时难觅i
    2020-11-28 10:33

    The drawImage() method can draw to a canvas using another canvas instead of an image.

    You could create a 'backup' canvas, of the same size as your original, draw the first one to there and then draw that one back to the original when you need it.

    e.g.

    // Assume we have a main canvas
    // canvas = 
    ctx = canvas.getContext('2d'); .. // create backing canvas var backCanvas = document.createElement('canvas'); backCanvas.width = canvas.width; backCanvas.height = canvas.height; var backCtx = backCanvas.getContext('2d'); // save main canvas contents backCtx.drawImage(canvas, 0,0); .. // restore main canvas ctx.drawImage(backCanvas, 0,0);

提交回复
热议问题