Search code examples
createjseaseljs

Create js clone a Shape with dynamically drawn graphics


I have a Image on a stage and I'm drawing over it and erasing it. Using the following method

http://jsfiddle.net/lannymcnie/ZNYPD/

Now i want to take a clone of the user drawings but its not working. I tried

 var drawingAreaClone = drawingArea.clone(true);

but its not working .

Is there a way to clone it. kindly Help


Solution

  • The demo you posted doesn't clear the stage, but instead clears the graphics each frame. If you clone the shape, it will have no instructions.

    @Catalin's answer is right if you just need a visual -- but another option is to use the Graphics store() method instead of clearing the graphics: http://createjs.com/docs/easeljs/classes/Graphics.html#method_store

    Essentially this method just sets an internal pointer to where the graphics draw from. By storing after each draw, only future draw calls are made. This will have the same application as the demo you posted, only you can call unstore() later to reset the Graphics to draw from the beginning. If you clone it this way, it should work.

    var erase = document.getElementById("toggle").checked;
    wrapper.updateCache(erase?"destination-out":"source-over");
    //drawing.graphics.clear();
    drawing.graphics.store(); // Don't draw anything before this point
    
    // Later
    var newGraphics = drawing.graphics.clone();
    newGraphics.unstore(); // Might be redundant
    var shape = new Shape(newGraphics);
    

    Note that cloning Graphics doesn't recreate the entire graphics tree, and simply clones the array that stores the instructions. Modifying the individual instructions after the fact would impact any clones of that Graphics object.

    Hope that helps.