Destroying Canvas

Ganesh2 picture Ganesh2 · Jun 3, 2013 · Viewed 7.4k times · Source

I'm trying to accomplish undo/redo. I'm using loadFromJSON(...) to re-build the canvas from the canvas states I've stored in an array. Basically, my idea is to destroy the existing canvas and re-construct canvas. Here is my code.

    // TODO: How to destroy existing canvas?

    canvas = new fabric.Canvas('canvas', {
        containerClass : 'myCanvasContainer',           
    });

    canvas.loadFromJSON(history[historyStep]);      

This code adds another canvas on top of existing canvas. However, I need a way to destroy the canvas and to build brand new canvas from my JSON data (replacing old canvas with new one).

Please advise whether it's possible or any other options I have.

Answer

Hooman Askari picture Hooman Askari · Oct 31, 2016

I believe canvas.dispose() is what you are looking for.

Read more here: http://fabricjs.com/docs/fabric.StaticCanvas.html#dispose