17

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 Answer 1

18

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); 
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, you can also draw from a canvas/video element as well, see the spec here: whatwg.org/specs/web-apps/current-work/multipage/…
Worked like a charm! And thanks for the link Peol, that comes in handy for another part of the project XD
@Vanuan, how do you overcome the issue with the image not loading immediately if your method to load the image need to be blocking?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.