jsketch/dist/jquery.sketchable.min.js

1 line
5.7 KiB
JavaScript

!function(a){function b(a,b){b||(b=a.data(q).options);var c=a.get(0);b.cssCursors&&(c.style.cursor=b.interactive?"pointer":"not-allowed"),c.onselectstart=function(){return!1}}function c(a,b){b=b.split(".");for(var c=0;c<b.length;c++){var d=b[c];a=a[d]}return a}function d(b){var c=a(b.target),d=c.offset();return{x:Math.round(b.pageX-d.left),y:Math.round(b.pageY-d.top),time:Date.now()}}function e(a,b,c){var d=b.coords[a];if(b.options.relTimestamps&&(0===b.strokes.length&&0===d.length&&(b.timestamp=c.time),c.time-=b.timestamp),d.push([c.x,c.y,c.time,+b.sketch.isDrawing,a]),b.options.filterCoords&&d.length>1){var e=d.length-1,f=d[e],g=d[e-1];f[0]==g[0]&&f[1]==g[1]&&d.splice(e,1)}}function f(a){return a.originalEvent.touches?!1:void l(a)}function g(a){return a.originalEvent.touches?!1:void m(a)}function h(a){return a.originalEvent.touches?!1:void n(a)}function i(a){o(a,l),a.preventDefault()}function j(a){o(a,m),a.preventDefault()}function k(a){o(a,n),a.preventDefault()}function l(b){if(3===b.which)return!1;var c=Math.abs(b.identifier||0),f=a(b.target),g=f.data(q),h=g.options;if(h.interactive){var i=d(b);"function"==typeof h.events.mousedownBefore&&h.events.mousedownBefore(f,g,b),h.graphics.firstPointSize>0&&g.sketch.beginFill(h.graphics.fillStyle).fillCircle(i.x,i.y,h.graphics.firstPointSize).endFill(),g.sketch.isDrawing=!0,g.sketch.beginPath();var j=g.coords[c];j||(j=[]),j.length>0&&g.strokes.push(j),g.coords[c]=[],e(c,g,i),"function"==typeof h.events.mousedown&&h.events.mousedown(f,g,b)}}function m(b){var c=Math.abs(b.identifier||0),f=a(b.target),g=f.data(q),h=g.options;if(h.interactive&&(h.mouseupMovements&&0!==g.strokes.length||g.sketch.isDrawing)){var i=d(b);"function"==typeof h.events.mousemoveBefore&&h.events.mousemoveBefore(f,g,b);var j=g.coords[c],k=j[j.length-1];if(k){var l,m;g.sketch.isDrawing?(l=h.strokeStyle,m=h.lineWidth):h.mouseupMovements&&(l=h.mouseupMovements.strokeStyle||"#DDD",m=h.mouseupMovements.lineWidth||1),g.sketch.lineStyle(l,m).line(k[0],k[1],i.x,i.y).stroke()}e(c,g,i),"function"==typeof h.events.mousemove&&h.events.mousemove(f,g,b)}}function n(b){var c=Math.abs(b.identifier||0),d=a(b.target),e=d.data(q),f=e.options;f.interactive&&("function"==typeof f.events.mouseupBefore&&f.events.mouseupBefore(d,e,b),e.sketch.isDrawing=!1,e.sketch.closePath(),e.strokes.push(e.coords[c]),e.coords[c]=[],"function"==typeof f.events.mouseup&&f.events.mouseup(d,e,b))}function o(b,c){var d=a(b.target),e=d.data(q),f=e.options;if(f.multitouch)for(var g=b.originalEvent.changedTouches,h=0;h<g.length;h++){var i=g[h];c(i)}else{var i=b.originalEvent.touches[0];c(i)}}function p(a){var b=a.data(q),c=b.options,d=b.sketch;d.clear();for(var e=0;e<b.strokes.length;e++)for(var f=b.strokes[e],g=0;g<f.length;g++){var h=f[g],i=f[g+1],j=!0;if(h.length>3&&(j=1===h[3]),!j&&!c.mouseupMovements)break;if(0===g){c.graphics.firstPointSize>0&&d.beginFill(c.graphics.fillStyle).fillCircle(h[0],h[1],c.graphics.firstPointSize).endFill();var k,l;j?(k=c.strokeStyle,l=c.lineWidth):c.mouseupMovements&&(k=c.mouseupMovements.strokeStyle||"#DDD",l=c.mouseupMovements.lineWidth||1),d.lineStyle(k,l)}else i&&d.beginPath().line(h[0],h[1],i[0],i[1]).stroke().closePath()}}var q="sketchable",r={init:function(c){var d=a.extend(!0,{},a.fn.sketchable.defaults,c||{});return this.each(function(){var c=a(this),e=c.data(q);e||(c.bind("mousedown",f),c.bind("mousemove",g),c.bind("mouseup",h),c.bind("touchstart",i),c.bind("touchmove",j),c.bind("touchend",k),b(c,d));var l=new jSketch(this,d.graphics);c.data(q,{strokes:[],coords:{},timestamp:0,sketch:l,options:d}),"function"==typeof d.events.init&&d.events.init(c,c.data(q));for(var m in a.fn.sketchable.plugins)a.fn.sketchable.plugins[m](c)})},config:function(c){return c?this.each(function(){var d=a(this),e=d.data(q);e.options=a.extend(!0,{},a.fn.sketchable.defaults,e.options,c),b(d)}):a(this).data(q).options},data:function(b){var c=a(this).data(q);return b?c[b]:c},strokes:function(b){if(b)return this.each(function(){var c=a(this),d=c.data(q);d.strokes=b,p(c)});var c=a(this).data(q);return c.strokes},handler:function(b){return this.each(function(){var c=a(this),d=c.data(q);b(c,d)})},trigger:function(b){return this.each(function(){var c=a(this),d=c.data(q),e=d.options.events;e&&"function"==typeof e[b]&&e[b](c,d)})},clear:function(b){return this.each(function(){var c=a(this),d=c.data(q),e=d.options;d.sketch.clear(),b||(d.strokes=[],d.coords={}),"function"==typeof e.events.clear&&e.events.clear(c,d)})},reset:function(b){return this.each(function(){var c=a(this),d=c.data(q),e=d.options;c.sketchable("destroy").sketchable(b),"function"==typeof e.events.reset&&e.events.reset(c,d)})},destroy:function(){return this.each(function(){var b=a(this),c=b.data(q),d=c.options;b.unbind("mouseup",h),b.unbind("mousemove",g),b.unbind("mousedown",f),b.unbind("touchstart",i),b.unbind("touchmove",j),b.unbind("touchend",k),b.removeData(q),d&&"function"==typeof d.events.destroy&&d.events.destroy(b,c)})},decorate:function(b,c,d){return this.each(function(){var e=a(this),f=e.data(q),g=f.options,h="_bound$"+b+"."+d;if(!f[h])if(f[h]=!0,g.events&&"function"==typeof g.events[b]){var i=g.events[b];g.events[b]=function(){i.apply(this,arguments),c.apply(this,arguments)}}else g.events[b]=c})}};a.fn.sketchable=function(b){var d=Array.prototype.slice.call(arguments,1);if("object"==typeof b||!b)return r.init.apply(this,arguments);if(b.indexOf(".")>-1){var e=c(r,b);return e.apply(this,d)}return r[b]?r[b].apply(this,d):(a.error("Unknown method: "+b),this)},a.fn.sketchable.api=r,a.fn.sketchable.plugins={},a.fn.sketchable.defaults={interactive:!0,mouseupMovements:!1,relTimestamps:!1,multitouch:!0,cssCursors:!0,filterCoords:!1,events:{},graphics:{firstPointSize:3,lineWidth:3,strokeStyle:"#F0F",fillStyle:"#F0F",lineCap:"round",lineJoin:"round",miterLimit:10}}}(jQuery);