jsketch/jsketch.min.js

1 line
4.0 KiB
JavaScript

!function(i){var t=function(i,t){return new s(i,t)},s=function(i,t){return i?("undefined"==typeof t&&(t={}),this.context(i),this.stageWidth=i.getAttribute("width"),this.stageHeight=i.getAttribute("height"),this.graphics.fillStyle="undefined"!=typeof t.fillStyle?t.fillStyle:"#F00",this.graphics.strokeStyle="undefined"!=typeof t.strokeStyle?t.strokeStyle:"#F0F",this.graphics.lineWidth="undefined"!=typeof t.lineWidth?t.lineWidth:2,this.graphics.lineCap="undefined"!=typeof t.lineCap?t.lineCap:"round",this.graphics.lineJoin="undefined"!=typeof t.lineJoin?t.lineJoin:"round",this.graphics.mitterLimit="undefined"!=typeof t.mitterLimit?t.mitterLimit:10,this.graphics.options=Object.keys(t),this.data={},this):void 0};t.fn=s.prototype={context:function(i){if(null==i)throw"No canvas element specified.";return this.canvas=i,this.graphics=i.getContext("2d"),this},size:function(i,t){return this.stageWidth=i,this.stageHeight=t,this.canvas.width=i,this.canvas.height=t,this},background:function(i){var t=this.graphics.fillStyle;return this.beginFill(i),this.graphics.fillRect(0,0,this.stageWidth,this.stageHeight),this.beginFill(t),this},stage:function(i,t,s){return this.size(i,t).background(s),this},beginFill:function(i){return this.saveGraphics(),this.graphics.fillStyle=i,this},endFill:function(){return this.graphics.fillStyle=this.data.fillStyle,this},lineStyle:function(i,t,s,e,h){return this.graphics.strokeStyle=i||this.graphics.strokeStyle,this.graphics.lineWidth=t||this.graphics.lineWidth,this.graphics.lineCap=s||this.graphics.lineCap,this.graphics.lineJoin=e||this.graphics.lineJoin,this.graphics.mitterLimit=h||this.graphics.mitterLimit,this},moveTo:function(i,t){return this.graphics.moveTo(i,t),this},lineTo:function(i,t){return this.graphics.lineTo(i,t),this},line:function(i,t,s,e){return this.graphics.moveTo(i,t),this.lineTo(s,e),this},curveTo:function(i,t,s,e){return this.graphics.quadraticCurveTo(s,e,i,t),this},curve:function(i,t,s,e,h,r){return this.graphics.moveTo(i,t),this.curveTo(s,e,h,r),this},stroke:function(){return this.graphics.stroke(),this},strokeRect:function(i,t,s,e){return this.graphics.beginPath(),this.graphics.strokeRect(i,t,s,e),this.graphics.closePath(),this},fillRect:function(i,t,s,e){return this.graphics.beginPath(),this.graphics.fillRect(i,t,s,e),this.graphics.closePath(),this},strokeCircle:function(i,t,s){return this.graphics.beginPath(),this.graphics.arc(i,t,s,0,2*Math.PI,!1),this.graphics.stroke(),this.graphics.closePath(),this},fillCircle:function(i,t,s){return this.graphics.beginPath(),this.graphics.arc(i,t,s,0,2*Math.PI,!1),this.graphics.fill(),this.graphics.closePath(),this},radialCircle:function(i,t,s,e,h){var r=this.graphics.createRadialGradient(i,t,s,i,t,h);return r.addColorStop(0,e),r.addColorStop(1,"rgba(0,0,0,0)"),this.graphics.fillStyle=r,this.fillCircle(i,t,s),this},beginPath:function(){return this.saveGraphics(),this.graphics.beginPath(),this},closePath:function(){return this.graphics.closePath(),this.restoreGraphics(),this},eraser:function(i){return"undefined"==typeof i&&(i=15),this.graphics.globalCompositeOperation="destination-out",this.graphics.lineWidth=i,this},pencil:function(i){return"undefined"!=typeof i&&(this.graphics.lineWidth=i),this.graphics.globalCompositeOperation="source-over",this},clear:function(){return this.graphics.clearRect(0,0,this.stageWidth,this.stageHeight),this.data={},this},save:function(){return this.graphics.save(),this},restore:function(){return this.graphics.restore(),this},saveGraphics:function(i){"undefined"==typeof i&&(i=this.graphics.options),"string"==typeof i&&(i=i.split(" "));for(var t=0;t<i.length;t++){var s=i[t];this.data[s]=this.graphics[s]}return this},restoreGraphics:function(i){"undefined"==typeof i&&(i=this.graphics.options),"string"==typeof i&&(i=i.split(" "));for(var t=0;t<i.length;t++){var s=i[t];this.graphics[s]=this.data[s]}return this},drawImage:function(i,t,s){"undefined"==typeof t&&(t=0),"undefined"==typeof s&&(s=0);var e=this,h=new Image;return h.src=i,h.onload=function(){e.graphics.drawImage(h,t,s)},this}},i.jSketch=t}(this);