diff --git a/README.md b/README.md index 9713dd1..496f85a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ jsketch ======= -A lightweight JavaScript library for drawing facilities on HTML5 canvas. +A lightweight JavaScript library for drawing facilities on HTML5 canvas, +conveniently wrapped as a jQuery plugin. Demos and docs available at http://personales.upv.es/luileito/jsketch/ + +Documented with JSDoc: https://github.com/jsdoc3/jsdoc diff --git a/jquery.sketchable.js b/jquery.sketchable.js index bd6bb27..67814bf 100644 --- a/jquery.sketchable.js +++ b/jquery.sketchable.js @@ -1,19 +1,19 @@ /*! - * jQuery sketchable 1.6 | Luis A. Leiva | MIT license + * jQuery sketchable | v1.6 | Luis A. Leiva | MIT license * This is a jQuery plugin for the jSketch drawing class. */ /** -* @name $ -* @class -* See the jQuery library for full details. -* This just documents the method that is added to jQuery by this plugin. -*/ + * @name $ + * @class + * See the jQuery library for full details. + * This just documents the method that is added to jQuery by this plugin. + */ /** -* @name $.fn -* @class -* See the jQuery library for full details. -* This just documents the method that is added to jQuery by this plugin. -*/ + * @name $.fn + * @class + * See the jQuery library for full details. + * This just documents the method that is added to jQuery by this plugin. + */ ;(function($){ // config options + namespace ID var options, _ns = "sketchable"; @@ -30,24 +30,23 @@ * @param {Object} opts plugin configuration (see defaults) * @return jQuery * @name init - * @ignore + * @ignore * @methodOf methods * @example $(selector).sketchable(); */ init: function(opts) { - // options will be available for all plugin methods + // Options will be available for all plugin methods. options = $.extend({}, $.fn.sketchable.defaults, opts || {}); return this.each(function() { var elem = $(this), data = elem.data(_ns); - // first-time checks if (!data) { - // TODO: add more drawing properties (and set them configurable) + // TODO: Add more drawing properties (and set them configurable). var sketch = new jSketch(this, { fillStyle: options.graphics.fillStyle, strokeStyle: options.graphics.strokeStyle, lineWidth: options.graphics.lineWidth, }); - // Flag drawing state. + // Flag drawing state on a per-canvas basis. sketch.isDrawing = false; elem.data(_ns, { // All strokes will be stored here. @@ -181,13 +180,12 @@ /** * Creates a new jQuery.sketchable object. - * @param {String|Object} method name of the method to invoke, - * or a configuration object. - * @returns jQuery + * @param {String|Object} method name of the method to invoke, or a configuration object. + * @return jQuery * @class * @version 1.6 * @date 9 Apr 2014 - * @example + * @example * $(selector).sketchable(); * $(selector).sketchable({interactive:false}); */ @@ -208,7 +206,7 @@ /** * Default configuration (publicly modifiable). - * Note that on[···] callbacks are triggered only if interactive == true. + * Note that mouse[···] callbacks are triggered only if interactive is set to true. * @name defaults * @default * @memberOf $.fn.sketchable @@ -273,6 +271,7 @@ }; function saveMousePos(data, pt) { + // TODO: Compress mouse data in order to save bandwidth. //var delta = (new Date).getTime() - data.timestamp; var time = (new Date).getTime(); data.coords.push([ pt.x, pt.y, time, +data.sketch.isDrawing ]); @@ -304,7 +303,7 @@ options.events.mousedown(elem, data, e); } }; - + function mouseupHandler(e) { var elem = $(e.target), data = elem.data(_ns); data.sketch.isDrawing = false; @@ -315,7 +314,7 @@ options.events.mouseup(elem, data, e); } }; - + function touchHandler(e) { e.preventDefault(); var elem = $(e.target); diff --git a/jquery.sketchable.min.js b/jquery.sketchable.min.js index 329d132..d681c30 100644 --- a/jquery.sketchable.min.js +++ b/jquery.sketchable.min.js @@ -1,5 +1,5 @@ /*! - * jQuery sketchable 1.5 | Luis A. Leiva | MIT license + * jQuery sketchable | v1.6 | Luis A. Leiva | MIT license * This is a jQuery plugin for the jSketch drawing class. */ -(function(g){var j,e="sketchable";var b={init:function(k){j=g.extend({},g.fn.sketchable.defaults,k);return this.each(function(){var l=g(this),m=l.data(e);if(!m){var n=new jSketch(this,{fillStyle:j.graphics.fillStyle,strokeStyle:j.graphics.strokeStyle,lineWidth:j.graphics.lineWidth,});n.isDrawing=false;l.data(e,{strokes:[],coords:[],timestamp:new Date().getTime(),canvas:n});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}}}if(typeof j.events.init==="function"){j.events.init(l,l.data(e))}})},strokes:function(k){if(k){return this.each(function(){var m=g(this),n=m.data(e);n.strokes=k})}else{var l=g(this).data(e);return l.strokes}},handler:function(k){return this.each(function(){var l=g(this),m=l.data(e);k(l,m)})},clear:function(){return this.each(function(){var k=g(this),l=k.data(e);l.canvas.clear();l.strokes=[];if(typeof j.events.clear==="function"){j.events.clear(k,l)}})},reset:function(k){return this.each(function(){var l=g(this),m=l.data(e);l.sketchable("destroy").sketchable(k);if(typeof j.events.reset==="function"){j.events.reset(l,m)}})},destroy:function(){return this.each(function(){var k=g(this),l=k.data(e);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(k){if(k==="methods"||k==="functions"){return b}else{if(b[k]){return b[k].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof k==="object"||!k){return b.init.apply(this,arguments)}else{g.error("Method "+k+' does not exist. See jQuery.sketchable("methods").')}}}return this};g.fn.sketchable.defaults={interactive:true,mouseupMovements:false,events:{},graphics:{firstPointSize:0,lineWidth:3,strokeStyle:"#F0F",fillStyle:"#F0F"}};function c(l){var k=g(l.target),m=k.offset();return{x:Math.round(l.pageX-m.left),y:Math.round(l.pageY-m.top)}}function a(k,m){var l=(new Date).getTime();k.coords.push([m.x,m.y,l,+k.canvas.isDrawing])}function f(n){var k=g(n.target),l=k.data(e);if(!j.mouseupMovements&&!l.canvas.isDrawing){return}var m=c(n);if(l.canvas.isDrawing){l.canvas.lineTo(m.x,m.y)}a(l,m);if(typeof j.events.mousemove==="function"){j.events.mousemove(k,l,n)}}function h(n){var k=g(n.target),l=k.data(e);l.canvas.isDrawing=true;var m=c(n);l.canvas.beginPath();if(j.graphics.firstPointSize>0){l.canvas.fillCircle(m.x,m.y,j.graphics.firstPointSize)}a(l,m);if(typeof j.events.mousedown==="function"){j.events.mousedown(k,l,n)}}function i(m){var k=g(m.target),l=k.data(e);l.canvas.isDrawing=false;l.canvas.closePath();l.strokes.push(l.coords);l.coords=[];if(typeof j.events.mouseup==="function"){j.events.mouseup(k,l,m)}}function d(l){l.preventDefault();var k=g(l.target);var n=l.originalEvent.changedTouches[0];for(var m in l){n[m]=l[m]}switch(l.type){case"touchstart":k.unbind(l.type,h);h(n);break;case"touchmove":k.unbind(l.type,f);f(n);break;case"touchend":k.unbind(l.type,i);i(n);break;default:return}}})(jQuery); \ No newline at end of file +(function(g){var j,e="sketchable";var b={init:function(k){j=g.extend({},g.fn.sketchable.defaults,k||{});return this.each(function(){var l=g(this),m=l.data(e);if(!m){var n=new jSketch(this,{fillStyle:j.graphics.fillStyle,strokeStyle:j.graphics.strokeStyle,lineWidth:j.graphics.lineWidth,});n.isDrawing=false;l.data(e,{strokes:[],coords:[],timestamp:new Date().getTime(),sketch:n});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}}}if(typeof j.events.init==="function"){j.events.init(l,l.data(e))}})},strokes:function(k){if(k){return this.each(function(){var m=g(this),n=m.data(e);n.strokes=k})}else{var l=g(this).data(e);return l.strokes}},handler:function(k){return this.each(function(){var l=g(this),m=l.data(e);k(l,m)})},clear:function(){return this.each(function(){var k=g(this),l=k.data(e);l.sketch.clear();l.strokes=[];if(typeof j.events.clear==="function"){j.events.clear(k,l)}})},reset:function(k){return this.each(function(){var l=g(this),m=l.data(e);l.sketchable("destroy").sketchable(k);if(typeof j.events.reset==="function"){j.events.reset(l,m)}})},destroy:function(){return this.each(function(){var k=g(this),l=k.data(e);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(k){if("methods functions hooks".split(" ").indexOf(k)>-1){return b}else{if(b[k]){return b[k].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof k==="object"||!k){return b.init.apply(this,arguments)}else{g.error("Method "+k+' does not exist. See jQuery.sketchable("methods").')}}}return this};g.fn.sketchable.defaults={interactive:true,mouseupMovements:false,events:{},graphics:{firstPointSize:0,lineWidth:3,strokeStyle:"#F0F",fillStyle:"#F0F"}};function c(l){var k=g(l.target),m=k.offset();return{x:Math.round(l.pageX-m.left),y:Math.round(l.pageY-m.top)}}function a(k,m){var l=(new Date).getTime();k.coords.push([m.x,m.y,l,+k.sketch.isDrawing])}function f(n){var k=g(n.target),l=k.data(e);if(!j.mouseupMovements&&!l.sketch.isDrawing){return}var m=c(n);if(l.sketch.isDrawing){l.sketch.lineTo(m.x,m.y)}a(l,m);if(typeof j.events.mousemove==="function"){j.events.mousemove(k,l,n)}}function h(n){var k=g(n.target),l=k.data(e);l.sketch.isDrawing=true;var m=c(n);l.sketch.beginPath();if(j.graphics.firstPointSize>0){l.sketch.fillCircle(m.x,m.y,j.graphics.firstPointSize)}a(l,m);if(typeof j.events.mousedown==="function"){j.events.mousedown(k,l,n)}}function i(m){var k=g(m.target),l=k.data(e);l.sketch.isDrawing=false;l.sketch.closePath();l.strokes.push(l.coords);l.coords=[];if(typeof j.events.mouseup==="function"){j.events.mouseup(k,l,m)}}function d(l){l.preventDefault();var k=g(l.target);var n=l.originalEvent.changedTouches[0];for(var m in l){n[m]=l[m]}switch(l.type){case"touchstart":k.unbind(l.type,h);h(n);break;case"touchmove":k.unbind(l.type,f);f(n);break;case"touchend":k.unbind(l.type,i);i(n);break;default:return}}})(jQuery); \ No newline at end of file