views:

88

answers:

1

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

+2  A: 

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);
pixl coder
Yes, you can also draw from a canvas/video element as well, see the spec here: http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-drawimage
peol
Worked like a charm!And thanks for the link Peol, that comes in handy for another part of the project XD
Firstmate