Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was able to implement a simple solution based on a <a href="http://www.codicode.com/art/undo_and_redo_to_the_html5_canvas.aspx" rel="nofollow noreferrer">post by Chtiwi Malek at CodiCode</a>. I also used some of the code from this <a href="https://stackoverflow.com/questions/13866660/create-a-rectangle-with-mousedown-event-in-kineticjs">problem</a> as an example to draw rectangles, so credits go to them and Chtiwi.</p> <p>The only difference in my solution is I used toJSON() to store each layer state in an array instead of toDataURL() on the canvas. I think toJSON() is needed over toDataURL() to be able to serialize all the data necessary to store each action on the canvas, but I'm not 100% on this so if someone else knows please leave a comment.</p> <pre><code>function makeHistory() { historyStep++; if (historyStep &lt; history.length) { history.length = historyStep; } json = layer.toJSON(); history.push(json); } </code></pre> <p>Call this function everytime you want to save a step to undo or redo. In my case, I call this function on every mouseup event.</p> <p>Bind these 2 functions to the Undo/Redo events.</p> <pre><code>function undoHistory() { if (historyStep &gt; 0) { historyStep--; layer.destroy(); layer = Kinetic.Node.create(history[historyStep], 'container') stage.add(layer); } } function redoHistory() { if (historyStep &lt; history.length-1) { historyStep++; layer.destroy(); layer = Kinetic.Node.create(history[historyStep], 'container') stage.add(layer); } } </code></pre> <p>Here's the <a href="http://jsfiddle.net/projeqht/zNRAB/" rel="nofollow noreferrer">jsfiddle</a>. Don't forget to initialize the array and step counter up top. Good luck!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload