Canvas drawImage using data URL

徘徊边缘 提交于 2020-01-31 22:08:10

问题


I'll start with the script:


  function saveInstance() {
   _savedInstance = document.getElementById('canvasID').toDataURL();
  }
  function restoreInstance() {
   ctx.drawImage(_savedInstance,0,0);
  }

The purpose is to save an instance of the canvas and re-apply it later [Similar to how ctx.save() saves the style and transformations].

However, I got the error that says incompatible types (Uncaught Error: TYPE_MISMATCH_ERR: DOM Exception 17). Is there any canvas method that will allow me to use the data URL string to re-draw the instance?

**If there's a better way to implement this save/restore idea I have, that'd also be much appreciated.

-Firstmate


回答1:


Yes, you can create an image element with its source as _savedInstance and then draw it to the canvas.

var img = new Image();
img.src = _savedInstance;
ctx.drawImage(img,0,0);


来源:https://stackoverflow.com/questions/3379893/canvas-drawimage-using-data-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!