Remember callStack

This commit is contained in:
Luis Leiva 2019-05-26 19:34:21 +03:00
parent 361e33cdc6
commit 16401758a9
1 changed files with 14 additions and 7 deletions

View File

@ -1,5 +1,5 @@
/*!
* Memento plugin for jQuery Sketchable | v2.1 | Luis A. Leiva | MIT license
* Memento plugin for jQuery Sketchable | v2.2 | Luis A. Leiva | MIT license
*/
/* eslint-env browser */
@ -28,10 +28,12 @@
/**
* Update state.
* @param {Image} snapshot - Image object.
* @param {array} strokes - Strokes associated with snapshot.
* @param {object} state - State associated with snapshot.
* @param {object} state.strokes - Strokes data.
* @param {object} state.callStack - Actions history.
* @private
*/
function draw(snapshot, strokes) {
function draw(snapshot, state) {
// Manipulate canvas via jQuery sketchable API.
// This way, we don't lose default drawing settings et al.
$instance.sketchable('handler', function(elem, data) {
@ -40,8 +42,9 @@
// so use the native HTMLCanvasElement.drawImage method instead.
data.sketch.clear();
data.sketch.context.drawImage(snapshot, 0, 0);
// Update strokes.
data.strokes = strokes.slice();
// Update state data.
data.strokes = state.strokes.slice();
data.sketch.callStack = state.callStack.slice();
});
}
/**
@ -111,7 +114,11 @@
if (ev && ev.identifier > 0) {
stack[stpos].strokes = data.strokes.slice();
} else {
stack.push({image: elem[0].toDataURL(), strokes: data.strokes.slice()});
stack.push({
image: elem.get(0).toDataURL(),
strokes: data.strokes.slice(),
callStack: data.sketch.callStack.slice(),
});
stpos++;
}
});
@ -137,7 +144,7 @@
var snapshot = new Image();
snapshot.src = state.image;
snapshot.onload = function() {
draw(this, state.strokes);
draw(this, state);
};
return this;
};