Allow preserving strokes on high-level clear() method

This commit is contained in:
Luis Leiva 2019-07-22 15:55:42 +03:00
parent adda3bbf7f
commit c4cae4d8e0
2 changed files with 15 additions and 6 deletions

View File

@ -155,6 +155,7 @@
/**
* Clears canvas <b>together with</b> associated strokes data.
* @return {object} jQuery
* @param {boolean} [keepStrokes] - Preserve stroke data (default: false).
* @memberof $.fn.sketchable
* @see $.fn.sketchable.handler
* @example
@ -166,11 +167,13 @@
* data.sketch.clear();
* });
*/
clear: function() {
clear: function(keepStrokes) {
return this.each(function() {
var elem = $(this), data = elem.data(namespace), options = data.options;
if (data.sketch) {
data.sketch.clear();
data.sketch.clear();
if (!keepStrokes) {
data.strokes = [];
data.coords = {};
}

View File

@ -186,6 +186,7 @@
/**
* Clears canvas <b>together with</b> associated strokes data.
* @see Sketchable.handler
* @param {boolean} [keepStrokes] - Preserve stroke data (default: false).
* @return {Sketchable}
* @memberof Sketchable
* @example
@ -193,16 +194,21 @@
* // Warning: This will remove strokes data as well.
* sketcher.clear();
* // If you only need to clear the canvas, just do:
* sketcher.clear(true);
* // Or, alternatively:
* sketcher.handler(function(elem, data) {
* data.sketch.clear();
* });
*/
clear: function() {
clear: function(keepStrokes) {
var elem = this.elem, data = dataBind(elem)[namespace], options = data.options;
data.sketch.clear();
data.strokes = [];
data.coords = {};
if (!keepStrokes) {
data.strokes = [];
data.coords = {};
}
if (typeof options.events.clear === 'function')
options.events.clear(elem, data);