Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destroying Canvas

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.

like image 732
Ganesh2 Avatar asked Jun 03 '13 05:06

Ganesh2


2 Answers

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

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

like image 80
Hooman Askari Avatar answered Sep 23 '22 23:09

Hooman Askari


You can use the canvas.clear() function.

like image 21
rodrigopandini Avatar answered Sep 24 '22 23:09

rodrigopandini