mirror of https://github.com/luileito/jsketch.git
5 lines
2.9 KiB
JavaScript
5 lines
2.9 KiB
JavaScript
/*
|
|
* jQuery sketchable 1.3 | Luis A. Leiva | MIT license
|
|
* This is a jQuery plugin built on top of jSketch drawing class.
|
|
*/
|
|
(function(f){var i,d="sketchable";var a={init:function(j){i=f.extend({},f.fn.sketchable.defaults,j);return this.each(function(){var k=f(this),l=k.data(d);if(!l){var m=new jSketch(this,{fillStyle:i.graphics.fillStyle,strokeStyle:i.graphics.strokeStyle,lineWidth:i.graphics.lineWidth,});k.data(d,{strokes:[],coords:[],canvas:m});if(i.interactive){k.bind("mousedown",g);k.bind("mouseup",h);k.bind("mousemove",e);k.bind("touchstart",c);k.bind("touchend",c);k.bind("touchmove",c);this.onselectstart=function(){return false}}}if(typeof i.events.create==="function"){i.events.create(k,k.data(d))}})},strokes:function(j){if(j){return this.each(function(){var l=f(this),m=l.data(d);m.strokes=j})}else{var k=f(this).data(d);return k.strokes}},handler:function(j){return this.each(function(){var k=f(this),l=k.data(d);j(k,l)})},clear:function(){return this.each(function(){var j=f(this),k=j.data(d);k.canvas.clear();k.strokes=[];if(typeof i.events.clear==="function"){i.events.clear(j,k)}})},reset:function(j){return this.each(function(){var k=f(this),l=k.data(d);k.sketchable("destroy").sketchable(j);if(typeof i.events.reset==="function"){i.events.reset(k,l)}})},destroy:function(){return this.each(function(){var j=f(this),k=j.data(d);if(i.interactive){j.unbind("mousedown",g);j.unbind("mouseup",h);j.unbind("mousemove",e);j.unbind("touchstart",c);j.unbind("touchend",c);j.unbind("touchmove",c)}j.removeData(d);if(typeof i.events.destroy==="function"){i.events.destroy(j,k)}})}};f.fn.sketchable=function(j){if(a[j]){return a[j].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof j==="object"||!j){return a.init.apply(this,arguments)}else{f.error("Method "+j+" does not exist on jQuery.sketchable")}}return this};f.fn.sketchable.defaults={interactive:true,events:{},graphics:{fillStyle:"#F0F",strokeStyle:"#F0F",lineWidth:2}};function b(k){var j=f(k.target),l=j.offset();return{x:k.pageX-l.left,y:k.pageY-l.top}}function e(m){var j=f(m.target),k=j.data(d);if(!k.canvas.isDrawing){return}var l=b(m);k.canvas.lineTo(l.x,l.y);k.coords.push([l.x,l.y,0]);if(typeof i.events.mouseMove==="function"){i.events.mouseMove(m)}}function g(m){var j=f(m.target),k=j.data(d);k.canvas.isDrawing=true;var l=b(m);k.canvas.beginPath();k.canvas.fillCircle(l.x,l.y,i.graphics.lineWidth);k.coords.push([l.x,l.y,1]);if(typeof i.events.mouseDown==="function"){i.events.mouseDown(m)}}function h(l){var j=f(l.target),k=j.data(d);k.canvas.isDrawing=false;k.canvas.closePath();k.strokes.push(k.coords);k.coords=[];if(typeof i.events.mouseUp==="function"){i.events.mouseUp(l)}}function c(k){k.preventDefault();var j=f(k.target);var l=k.originalEvent.changedTouches[0];l.type=k.type;switch(k.type){case"touchstart":j.unbind(k.type,g);g(l);break;case"touchmove":j.unbind(k.type,e);e(l);break;case"touchend":j.unbind(k.type,h);h(l);break;default:return}}})(jQuery); |