Added paranoid check

This commit is contained in:
Luis Leiva 2014-07-10 17:26:11 +02:00
parent 70aa9e51b7
commit b18703841a
2 changed files with 13 additions and 1 deletions

View File

@ -122,6 +122,7 @@
data.sketch.clear(); data.sketch.clear();
data.strokes = []; data.strokes = [];
data.coords = {}; data.coords = {};
if (typeof options.events.clear === 'function') { if (typeof options.events.clear === 'function') {
options.events.clear(elem, data); options.events.clear(elem, data);
} }
@ -140,6 +141,7 @@
return this.each(function(){ return this.each(function(){
var elem = $(this), data = elem.data(_ns), options = data.options; var elem = $(this), data = elem.data(_ns), options = data.options;
elem.sketchable('destroy').sketchable(opts); elem.sketchable('destroy').sketchable(opts);
if (typeof options.events.reset === 'function') { if (typeof options.events.reset === 'function') {
options.events.reset(elem, data); options.events.reset(elem, data);
} }
@ -163,6 +165,7 @@
elem.unbind("touchmove", touchHandler); elem.unbind("touchmove", touchHandler);
} }
elem.removeData(_ns); elem.removeData(_ns);
if (typeof options.events.destroy === 'function') { if (typeof options.events.destroy === 'function') {
options.events.destroy(elem, data); options.events.destroy(elem, data);
} }
@ -275,6 +278,11 @@
* @private * @private
*/ */
function saveMousePos(idx, data, pt) { function saveMousePos(idx, data, pt) {
// Ensure that coords is properly initialized.
if (!data.coords[idx]) {
data.coords[idx] = [];
}
var time = (new Date).getTime(); var time = (new Date).getTime();
if (data.options.relTimestamps) { if (data.options.relTimestamps) {
// The first timestamp is relative to initialization time; // The first timestamp is relative to initialization time;
@ -282,6 +290,7 @@
if (data.strokes.length === 0 && data.coords[idx].length === 0) data.timestamp = time; if (data.strokes.length === 0 && data.coords[idx].length === 0) data.timestamp = time;
time -= data.timestamp; time -= data.timestamp;
} }
data.coords[idx].push([ pt.x, pt.y, time, +data.sketch.isDrawing ]); data.coords[idx].push([ pt.x, pt.y, time, +data.sketch.isDrawing ]);
}; };
@ -302,6 +311,7 @@
data.sketch.beginPath().line(last[0], last[1], p.x, p.y).stroke().closePath(); data.sketch.beginPath().line(last[0], last[1], p.x, p.y).stroke().closePath();
} }
saveMousePos(idx, data, p); saveMousePos(idx, data, p);
if (typeof options.events.mousemove === 'function') { if (typeof options.events.mousemove === 'function') {
options.events.mousemove(elem, data, e); options.events.mousemove(elem, data, e);
} }
@ -331,6 +341,7 @@
data.coords[idx] = []; data.coords[idx] = [];
} }
saveMousePos(idx, data, p); saveMousePos(idx, data, p);
if (typeof options.events.mousedown === 'function') { if (typeof options.events.mousedown === 'function') {
options.events.mousedown(elem, data, e); options.events.mousedown(elem, data, e);
} }
@ -346,6 +357,7 @@
data.sketch.isDrawing = false; data.sketch.isDrawing = false;
data.strokes.push(data.coords[idx]); data.strokes.push(data.coords[idx]);
data.coords[idx] = []; data.coords[idx] = [];
if (typeof options.events.mouseup === 'function') { if (typeof options.events.mouseup === 'function') {
options.events.mouseup(elem, data, e); options.events.mouseup(elem, data, e);
} }

View File

@ -2,4 +2,4 @@
* jQuery sketchable | v1.8 | Luis A. Leiva | MIT license * jQuery sketchable | v1.8 | Luis A. Leiva | MIT license
* A jQuery plugin for the jSketch drawing library. * A jQuery plugin for the jSketch drawing library.
*/ */
(function(g){var e="sketchable";var b={init:function(k){var j=g.extend(true,{},g.fn.sketchable.defaults,k||{});return this.each(function(){var l=g(this),m=l.data(e);if(!m){if(j.interactive){l.bind("mousedown",h);l.bind("mouseup",i);l.bind("mousemove",f);l.bind("touchstart",d);l.bind("touchend",d);l.bind("touchmove",d);this.onselectstart=function(){return false}}}var n=new jSketch(this,j.graphics);n.isDrawing=false;l.data(e,{strokes:[],coords:{},timestamp:new Date().getTime(),sketch:n,options:j});if(typeof j.events.init==="function"){j.events.init(l,l.data(e))}})},strokes:function(j){if(j){return this.each(function(){var l=g(this),m=l.data(e);m.strokes=j})}else{var k=g(this).data(e);return k.strokes}},handler:function(j){return this.each(function(){var k=g(this),l=k.data(e);j(k,l)})},clear:function(){return this.each(function(){var k=g(this),l=k.data(e),j=l.options;l.sketch.clear();l.strokes=[];l.coords={};if(typeof j.events.clear==="function"){j.events.clear(k,l)}})},reset:function(j){return this.each(function(){var l=g(this),m=l.data(e),k=m.options;l.sketchable("destroy").sketchable(j);if(typeof k.events.reset==="function"){k.events.reset(l,m)}})},destroy:function(){return this.each(function(){var k=g(this),l=k.data(e),j=l.options;if(j.interactive){k.unbind("mousedown",h);k.unbind("mouseup",i);k.unbind("mousemove",f);k.unbind("touchstart",d);k.unbind("touchend",d);k.unbind("touchmove",d)}k.removeData(e);if(typeof j.events.destroy==="function"){j.events.destroy(k,l)}})}};g.fn.sketchable=function(j){if("methods functions hooks".split(" ").indexOf(j)>-1){return b}else{if(b[j]){return b[j].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof j==="object"||!j){return b.init.apply(this,arguments)}else{g.error("Method "+j+' does not exist. See jQuery.sketchable("methods").')}}}return this};g.fn.sketchable.defaults={interactive:true,mouseupMovements:false,relTimestamps:false,events:{},graphics:{firstPointSize:3,lineWidth:3,strokeStyle:"#F0F",fillStyle:"#F0F",lineCap:"round",lineJoin:"round",miterLimit:10}};function c(k){var j=g(k.target),l=j.offset();return{x:Math.round(k.pageX-l.left),y:Math.round(k.pageY-l.top)}}function a(j,k,m){var l=(new Date).getTime();if(k.options.relTimestamps){if(k.strokes.length===0&&k.coords[j].length===0){k.timestamp=l}l-=k.timestamp}k.coords[j].push([m.x,m.y,l,+k.sketch.isDrawing])}function f(q,j){if(typeof j==="undefined"){j=0}var m=g(q.target),n=m.data(e),k=n.options;if((!k.mouseupMovements||n.strokes.length===0)&&!n.sketch.isDrawing){return}var o=c(q);if(n.sketch.isDrawing){var l=n.coords[j][n.coords[j].length-1];n.sketch.beginPath().line(l[0],l[1],o.x,o.y).stroke().closePath()}a(j,n,o);if(typeof k.events.mousemove==="function"){k.events.mousemove(m,n,q)}}function h(o,j){if(typeof j==="undefined"){j=0}var l=g(o.target),m=l.data(e),k=m.options;m.sketch.isDrawing=true;var n=c(o);if(k.graphics.firstPointSize>0){m.sketch.fillCircle(n.x,n.y,k.graphics.firstPointSize)}if(!m.coords[j]){m.coords[j]=[]}if(m.coords[j].length>0){m.strokes.push(m.coords[j]);m.coords[j]=[]}a(j,m,n);if(typeof k.events.mousedown==="function"){k.events.mousedown(l,m,o)}}function i(n,j){if(typeof j==="undefined"){j=0}var l=g(n.target),m=l.data(e),k=m.options;m.sketch.isDrawing=false;m.strokes.push(m.coords[j]);m.coords[j]=[];if(typeof k.events.mouseup==="function"){k.events.mouseup(l,m,n)}}function d(n){n.preventDefault();var l=g(n.target);var m=n.originalEvent.changedTouches;switch(n.type){case"touchstart":l.unbind(n.type,h);for(var k=0,j=m[k];k<m.length;k++){for(var p in n){j[p]=n[p]}h(j,j.identifier)}break;case"touchmove":l.unbind(n.type,f);for(var k=0,j=m[k];k<m.length;k++){for(var p in n){j[p]=n[p]}f(j,j.identifier)}break;case"touchend":l.unbind(n.type,i);for(var k=0,j=m[k];k<m.length;k++){for(var p in n){j[p]=n[p]}i(j,j.identifier)}break;default:return}return false}})(jQuery); (function(g){var e="sketchable";var b={init:function(k){var j=g.extend(true,{},g.fn.sketchable.defaults,k||{});return this.each(function(){var l=g(this),m=l.data(e);if(!m){if(j.interactive){l.bind("mousedown",h);l.bind("mouseup",i);l.bind("mousemove",f);l.bind("touchstart",d);l.bind("touchend",d);l.bind("touchmove",d);this.onselectstart=function(){return false}}}var n=new jSketch(this,j.graphics);n.isDrawing=false;l.data(e,{strokes:[],coords:{},timestamp:new Date().getTime(),sketch:n,options:j});if(typeof j.events.init==="function"){j.events.init(l,l.data(e))}})},strokes:function(j){if(j){return this.each(function(){var l=g(this),m=l.data(e);m.strokes=j})}else{var k=g(this).data(e);return k.strokes}},handler:function(j){return this.each(function(){var k=g(this),l=k.data(e);j(k,l)})},clear:function(){return this.each(function(){var k=g(this),l=k.data(e),j=l.options;l.sketch.clear();l.strokes=[];l.coords={};if(typeof j.events.clear==="function"){j.events.clear(k,l)}})},reset:function(j){return this.each(function(){var l=g(this),m=l.data(e),k=m.options;l.sketchable("destroy").sketchable(j);if(typeof k.events.reset==="function"){k.events.reset(l,m)}})},destroy:function(){return this.each(function(){var k=g(this),l=k.data(e),j=l.options;if(j.interactive){k.unbind("mousedown",h);k.unbind("mouseup",i);k.unbind("mousemove",f);k.unbind("touchstart",d);k.unbind("touchend",d);k.unbind("touchmove",d)}k.removeData(e);if(typeof j.events.destroy==="function"){j.events.destroy(k,l)}})}};g.fn.sketchable=function(j){if("methods functions hooks".split(" ").indexOf(j)>-1){return b}else{if(b[j]){return b[j].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof j==="object"||!j){return b.init.apply(this,arguments)}else{g.error("Method "+j+' does not exist. See jQuery.sketchable("methods").')}}}return this};g.fn.sketchable.defaults={interactive:true,mouseupMovements:false,relTimestamps:false,events:{},graphics:{firstPointSize:3,lineWidth:3,strokeStyle:"#F0F",fillStyle:"#F0F",lineCap:"round",lineJoin:"round",miterLimit:10}};function c(k){var j=g(k.target),l=j.offset();return{x:Math.round(k.pageX-l.left),y:Math.round(k.pageY-l.top)}}function a(j,k,m){if(!k.coords[j]){k.coords[j]=[]}var l=(new Date).getTime();if(k.options.relTimestamps){if(k.strokes.length===0&&k.coords[j].length===0){k.timestamp=l}l-=k.timestamp}k.coords[j].push([m.x,m.y,l,+k.sketch.isDrawing])}function f(q,j){if(typeof j==="undefined"){j=0}var m=g(q.target),n=m.data(e),k=n.options;if((!k.mouseupMovements||n.strokes.length===0)&&!n.sketch.isDrawing){return}var o=c(q);if(n.sketch.isDrawing){var l=n.coords[j][n.coords[j].length-1];n.sketch.beginPath().line(l[0],l[1],o.x,o.y).stroke().closePath()}a(j,n,o);if(typeof k.events.mousemove==="function"){k.events.mousemove(m,n,q)}}function h(o,j){if(typeof j==="undefined"){j=0}var l=g(o.target),m=l.data(e),k=m.options;m.sketch.isDrawing=true;var n=c(o);if(k.graphics.firstPointSize>0){m.sketch.fillCircle(n.x,n.y,k.graphics.firstPointSize)}if(!m.coords[j]){m.coords[j]=[]}if(m.coords[j].length>0){m.strokes.push(m.coords[j]);m.coords[j]=[]}a(j,m,n);if(typeof k.events.mousedown==="function"){k.events.mousedown(l,m,o)}}function i(n,j){if(typeof j==="undefined"){j=0}var l=g(n.target),m=l.data(e),k=m.options;m.sketch.isDrawing=false;m.strokes.push(m.coords[j]);m.coords[j]=[];if(typeof k.events.mouseup==="function"){k.events.mouseup(l,m,n)}}function d(n){n.preventDefault();var l=g(n.target);var m=n.originalEvent.changedTouches;switch(n.type){case"touchstart":l.unbind(n.type,h);for(var k=0,j=m[k];k<m.length;k++){for(var p in n){j[p]=n[p]}h(j,j.identifier)}break;case"touchmove":l.unbind(n.type,f);for(var k=0,j=m[k];k<m.length;k++){for(var p in n){j[p]=n[p]}f(j,j.identifier)}break;case"touchend":l.unbind(n.type,i);for(var k=0,j=m[k];k<m.length;k++){for(var p in n){j[p]=n[p]}i(j,j.identifier)}break;default:return}return false}})(jQuery);