diff --git a/jsketch.js b/jsketch.js index b63694c..8302620 100644 --- a/jsketch.js +++ b/jsketch.js @@ -34,17 +34,15 @@ var Sketch = function(elem, options){ // Although discouraged, we can instantiate the class without arguments. if (!elem) return; - // We can pass default setup values. - if (typeof options === 'undefined') options = {}; // Set drawing context first. this.context(elem); // Scene defaults. this.stageWidth = elem.width; this.stageHeight = elem.height; - // Set drawing defaults. - this.drawingDefaults(options); // Make room for storing some data such as brush type, colors, etc. this.data = {}; + // Set drawing defaults. + this.drawingDefaults(options); // Make constructor chainable. return this; }; @@ -83,21 +81,16 @@ */ drawingDefaults: function(options) { if (typeof options === 'undefined') options = {}; - this.graphics.fillStyle = typeof options.fillStyle !== 'undefined' ? options.fillStyle : '#F00'; - this.graphics.strokeStyle = typeof options.strokeStyle !== 'undefined' ? options.strokeStyle : '#F0F'; - this.graphics.lineWidth = typeof options.lineWidth !== 'undefined' ? options.lineWidth : 2; - this.graphics.lineCap = typeof options.lineCap !== 'undefined' ? options.lineCap : 'round'; - this.graphics.lineJoin = typeof options.lineJoin !== 'undefined' ? options.lineJoin : 'round'; - this.graphics.miterLimit = typeof options.miterLimit !== 'undefined' ? options.miterLimit : 10; - // Remember graphic options for later saveing/restoring drawing status. - this.graphics.options = { - fillStyle: this.graphics.fillStyle, - strokeStyle: this.graphics.strokeStyle, - lineWidth: this.graphics.lineWidth, - lineCap: this.graphics.lineCap, - lineJoin: this.graphics.lineJoin, - miterLimit: this.graphics.miterLimit - }; + if (typeof options.fillStyle === 'undefined') options.fillStyle = '#F00'; + if (typeof options.strokeStyle === 'undefined') options.strokeStyle = '#F0F'; + if (typeof options.lineWidth === 'undefined') options.lineWidth = 2; + if (typeof options.lineCap === 'undefined') options.lineCap = 'round'; + if (typeof options.lineJoin === 'undefined') options.lineJoin = 'round'; + if (typeof options.miterLimit === 'undefined') options.miterLimit = 10; + // Remember graphic options for later saving/restoring drawing status. + this.saveGraphics(options); + // Apply defaults. + this.restoreGraphics(options); return this; }, /** @@ -112,7 +105,7 @@ this.stageHeight = height; this.canvas.width = width; this.canvas.height = height; - // On resizing we lost drawing options, so reset. + // On resizing we lose drawing options, so restore them. this.restoreGraphics(); return this; }, @@ -263,7 +256,6 @@ */ strokeRect: function(x,y,width,height) { this.graphics.beginPath(); - //this.moveTo(x,y).lineTo(x+width,y).lineTo(x+width,y+height).lineTo(y,y+height).lineTo(x,y); this.graphics.strokeRect(x,y,width,height); this.graphics.closePath(); return this; @@ -344,7 +336,7 @@ }, /** * Sets brush to eraser mode. - * @param {Number} brushSize - Brush size. + * @param {Number} [brushSize] - Brush size. * @return jSketch * @memberof jSketch */ @@ -356,7 +348,7 @@ }, /** * Sets brush to pencil mode. - * @param {Number} brushSize - Brush size. + * @param {Number} [brushSize] - Brush size. * @return jSketch * @memberof jSketch */ @@ -395,27 +387,23 @@ }, /** * Saves given drawing settings. - * @param propList {Array|String} propList - Array or space-separated String. + * @param {Object} [options] - Graphics options. * @return jSketch * @memberof jSketch */ saveGraphics: function(options) { - if (typeof options === 'undefined') options = this.graphics.options; - else this.graphics.options = options; - for (var opt in options) { - this.data[opt] = options[opt]; - } + if (typeof options === 'undefined') options = this.data.options; + this.data.options = options; return this; }, /** * Restores given drawing settings. - * @param propList {Array|String} propList - Array or space-separated String. + * @param {Object} [options] - Graphics options. * @return jSketch * @memberof jSketch */ restoreGraphics: function(options) { - if (typeof options === 'undefined') options = this.graphics.options; - else this.graphics.options = options; + if (typeof options === 'undefined') options = this.data.options; for (var opt in options) { this.graphics[opt] = options[opt]; } diff --git a/jsketch.min.js b/jsketch.min.js index eb2c258..3aad5fb 100644 --- a/jsketch.min.js +++ b/jsketch.min.js @@ -1,5 +1 @@ -/*! - * jSketch 0.8 | Luis A. Leiva | MIT license - * A simple JavaScript library for drawing facilities on HTML5 canvas. - */ -(function(a){var c=function(e,d){return new b(e,d)};var b=function(e,d){if(!e){return}if(typeof d==="undefined"){d={}}this.context(e);this.stageWidth=e.width;this.stageHeight=e.height;this.drawingDefaults(d);this.data={};return this};c.fn=b.prototype={context:function(d){if(d===null){throw ("No canvas element specified.")}this.canvas=d;this.graphics=d.getContext("2d");return this},drawingDefaults:function(d){if(typeof d==="undefined"){d={}}this.graphics.fillStyle=typeof d.fillStyle!=="undefined"?d.fillStyle:"#F00";this.graphics.strokeStyle=typeof d.strokeStyle!=="undefined"?d.strokeStyle:"#F0F";this.graphics.lineWidth=typeof d.lineWidth!=="undefined"?d.lineWidth:2;this.graphics.lineCap=typeof d.lineCap!=="undefined"?d.lineCap:"round";this.graphics.lineJoin=typeof d.lineJoin!=="undefined"?d.lineJoin:"round";this.graphics.miterLimit=typeof d.miterLimit!=="undefined"?d.miterLimit:10;this.graphics.options=Object.keys(d);return this},size:function(e,d){this.stageWidth=e;this.stageHeight=d;this.canvas.width=e;this.canvas.height=d;this.drawingDefaults();return this},background:function(d){this.beginFill(d);this.graphics.fillRect(0,0,this.stageWidth,this.stageHeight);this.endFill();return this},stage:function(f,d,e){this.size(f,d).background(e);return this},beginFill:function(d){this.saveGraphics();this.graphics.fillStyle=d;return this},endFill:function(){this.graphics.fillStyle=this.data.fillStyle;return this},lineStyle:function(d,e,f,h,g){this.graphics.strokeStyle=d||this.graphics.strokeStyle;this.graphics.lineWidth=e||this.graphics.lineWidth;this.graphics.lineCap=f||this.graphics.lineCap;this.graphics.lineJoin=h||this.graphics.lineJoin;this.graphics.miterLimit=g||this.graphics.miterLimit;return this},moveTo:function(d,e){this.graphics.moveTo(d,e);return this},lineTo:function(d,e){this.graphics.lineTo(d,e);return this},line:function(e,g,d,f){this.graphics.moveTo(e,g);this.lineTo(d,f);return this},curveTo:function(d,g,f,e){this.graphics.quadraticCurveTo(f,e,d,g);return this},curve:function(f,i,d,h,g,e){this.graphics.moveTo(f,i);this.curveTo(d,h,g,e);return this},stroke:function(){this.graphics.stroke();return this},strokeRect:function(e,g,f,d){this.graphics.beginPath();this.graphics.strokeRect(e,g,f,d);this.graphics.closePath();return this},fillRect:function(e,g,f,d){this.graphics.beginPath();this.graphics.fillRect(e,g,f,d);this.graphics.closePath();return this},strokeCircle:function(e,f,d){this.graphics.beginPath();this.graphics.arc(e,f,d,0,Math.PI*2,false);this.graphics.stroke();this.graphics.closePath();return this},fillCircle:function(e,f,d){this.graphics.beginPath();this.graphics.arc(e,f,d,0,Math.PI*2,false);this.graphics.fill();this.graphics.closePath();return this},radialCircle:function(e,j,d,f,h){var i=this.graphics.createRadialGradient(e,j,d,e,j,h);i.addColorStop(0,f);i.addColorStop(1,"rgba(0,0,0,0)");this.graphics.fillStyle=i;this.fillCircle(e,j,d);return this},beginPath:function(){this.saveGraphics();this.graphics.beginPath();return this},closePath:function(){this.graphics.closePath();this.restoreGraphics();return this},eraser:function(d){if(typeof d==="undefined"){d=15}this.graphics.globalCompositeOperation="destination-out";this.graphics.lineWidth=d;return this},pencil:function(d){if(typeof d!=="undefined"){this.graphics.lineWidth=d}this.graphics.globalCompositeOperation="source-over";return this},clear:function(){this.graphics.clearRect(0,0,this.stageWidth,this.stageHeight);this.data={};return this},save:function(){this.graphics.save();return this},restore:function(){this.graphics.restore();return this},saveGraphics:function(d){if(typeof d==="undefined"){d=this.graphics.options}if(typeof d==="string"){d=d.split(" ")}for(var e=0;e