mirror of https://github.com/luileito/jsketch.git
2 lines
19 KiB
JavaScript
2 lines
19 KiB
JavaScript
/*! jSketch drawing lib (all in one) | v2.0.1 | 2019-09-03 */
|
|
!function(a){function b(a,b){if(!a)throw new Error("Sketchable requires a DOM element or selector.");return"string"==typeof a&&(a=document.querySelector(a)),this.setContext(a),this.stageWidth=a.offsetWidth,this.stageHeight=a.offsetHeight,this.data=b,this.callStack=[],this.setDefaults()}b.prototype={setContext:function(a){if(!a)throw new Error("No canvas element specified.");return this.canvas=a,this.context=a.getContext("2d"),this},setDefaults:function(){var a={fillStyle:this.data.fillStyle||"#F00",strokeStyle:this.data.strokeStyle||"#F0F",lineWidth:this.data.lineWidth||2,lineCap:this.data.lineCap||"round",lineJoin:this.data.lineJoin||"round",miterLimit:this.data.miterLimit||10};for(var b in a){var c=a[b];this.callStack.push({property:b,value:c})}return this.saveGraphics(a).restoreGraphics()},size:function(a,b){var c=parseFloat(a),d=parseFloat(b);return this.stageWidth=c,this.stageHeight=d,this.canvas.width=c,this.canvas.height=d,this.restoreGraphics(),this},background:function(a){var b=[0,0,this.stageWidth,this.stageHeight];return this.beginFill(a),this.context.fillRect.apply(this.context,b),this.endFill(),this.callStack.push({property:"fillStyle",value:a}),this.callStack.push({method:"fillRect",args:b}),this},stage:function(a,b,c){return this.size(a,b).background(c),this},beginFill:function(a){return this.saveGraphics({fillStyle:a}),this.context.fillStyle=a,this.callStack.push({property:"fillStyle",value:a}),this},endFill:function(){return this.restoreGraphics(),this},lineStyle:function(a,b,c,d,e){var f={strokeStyle:a,lineWidth:b,lineCap:c,lineJoin:d,miterLimit:e};return this.saveGraphics(f).restoreGraphics()},moveTo:function(a,b){var c=[].slice.call(arguments);return this.context.moveTo.apply(this.context,c),this.callStack.push({method:"moveTo",args:c}),this},lineTo:function(a,b){var c=[].slice.call(arguments);return this.context.lineTo.apply(this.context,c),this.callStack.push({method:"lineTo",args:c}),this},line:function(a,b,c,d){return this.moveTo(a,b),this.lineTo(c,d),this},curveTo:function(a,b,c,d){var e=[c,d,a,b];return this.context.quadraticCurveTo.apply(this.context,e),this.callStack.push({method:"quadraticCurveTo",args:e}),this},curve:function(a,b,c,d,e,f){return this.moveTo(a,b),this.curveTo(c,d,e,f),this},stroke:function(){return this.context.stroke(),this.callStack.push({method:"stroke"}),this},strokeRect:function(a,b,c,d){var e=[].slice.call(arguments);return this.context.beginPath(),this.context.strokeRect.apply(this.context,e),this.context.closePath(),this.callStack.push({method:"strokeRect",args:e}),this},fillRect:function(a,b,c,d){var e=[].slice.call(arguments);return this.context.beginPath(),this.context.fillRect.apply(this.context,e),this.context.closePath(),this.callStack.push({method:"fillRect",args:e}),this},rect:function(a,b,c,d){var e=[].slice.call(arguments);return this.fillRect.apply(this,e),this.strokeRect.apply(this,e),this},strokeCircle:function(a,b,c){var d=[a,b,c,0,2*Math.PI,!1];return this.context.beginPath(),this.context.arc.apply(this.context,d),this.context.stroke(),this.context.closePath(),this.callStack.push({method:"strokeCircle",args:d}),this},fillCircle:function(a,b,c){var d=[a,b,c,0,2*Math.PI,!1];return this.context.beginPath(),this.context.arc.apply(this.context,d),this.context.fill(),this.context.closePath(),this.callStack.push({method:"fillCircle",args:d}),this},circle:function(a,b,c){var d=[].slice.call(arguments);return this.fillCircle.apply(this,d),this.strokeCircle.apply(this,d),this},radialCircle:function(a,b,c,d,e){("undefined"==typeof d||0>d)&&(d=1);var f=this.context.createRadialGradient(a,b,c,a,b,d);e&&"array"===e.constructor.name.toLowerCase()||(e=[this.context.fillStyle,"white"]);for(var g=0;g<e.length;g++){var h=e[g];f.addColorStop(g,h)}return this.beginFill(f).fillCircle(a,b,c).endFill(),this},beginPath:function(){return this.context.beginPath(),this.callStack.push({method:"beginPath"}),this},closePath:function(){return this.context.closePath(),this.callStack.push({method:"closePath"}),this},eraser:function(){return this.context.globalCompositeOperation="destination-out",this.callStack.push({property:"comp-op",value:"dst_out"}),this},pencil:function(){return this.context.globalCompositeOperation="source-over",this.callStack.push({property:"comp-op",value:"src_over"}),this},clear:function(){var a=[0,0,this.stageWidth,this.stageHeight];return this.context.clearRect.apply(this.context,a),this.callStack.push({method:"clear"}),this},save:function(){return this.context.save(),this.callStack.push({method:"save"}),this},restore:function(){return this.context.restore(),this.callStack.push({method:"restore"}),this},saveGraphics:function(a){for(var b in a){var c=a[b];c&&c!==this.data[b]&&(this.data[b]=c,this.callStack.push({property:b,value:c}))}return this},restoreGraphics:function(){for(var a in this.data)this.context[a]=this.data[a];return this},drawImage:function(a,b,c){"undefined"==typeof b&&(b=0),"undefined"==typeof c&&(c=0);var d=this,e=new Image;return e.src=a,e.onload=function(){d.context.drawImage(e,b,c),d.callStack.push({method:"drawImage",args:[e,b,c]}),d.callStack.push({method:"removeAsync"})},e.onerror=function(){d.callStack.push({method:"removeAsync"})},d.callStack.push({method:"addAsync"}),this}},a.jSketch=b}(this),function(a){a.jSketch.prototype.toSVG=function(a){function b(){var a='<?xml version="1.0" encoding="utf-8" standalone="no" ?>';return a+='<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">',a+='<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="'+e.stageWidth+'" height="'+e.stageHeight+'" viewBox="0 0 '+e.stageWidth+" "+e.stageHeight+'">',a+="<desc>Generated with jSketch: https://luis.leiva.name/jsketch/</desc>",a+="<defs></defs>",a+="<g>",a+=c(),a+="</g>",a+="</svg>",a.replace(/\s+/g," ")}function c(){for(var a="",b=0;b<e.callStack.length;b++){var c=e.callStack[b];if(c.property&&"object"!=typeof c.value)h[c.property]=c.value;else if(c.method){if(!i[c.method]){console.warn("Method not implemented:",c.method);continue}a+=i[c.method].apply(null,c.args)}else console.warn("Unknown call:",c)}return a}function d(){if(0>=g){clearInterval(j);var c=b();a(c)}else console.info("Waiting for %s async operations to be finished ...",g)}if("function"!=typeof a)throw new Error("You must supply a callback in toSVG() method.");var e=this,f=[],g=0,h={},i={addAsync:function(){return g++,""},removeAsync:function(){return g--,""},drawImage:function(a,b,c){return'<image preserveAspectRatio="none" width="'+a.width+'" height="'+a.height+'" transform="translate('+b+", "+c+')" xlink:href="'+a.src+'"></image>'},fillRect:function(a,b,c,d){return'<rect stroke="none" width="'+c+'" height="'+d+'" x="'+a+'" y="'+b+'" fill="'+h.fillStyle+'"></rect>'},strokeRect:function(a,b,c,d){return'<rect fill="none" width="'+c+'" height="'+d+'" x="'+a+'" y="'+b+'" stroke="'+h.strokeStyle+'" stroke-width="'+h.lineWidth+'" stroke-linecap="'+h.lineCap+'" stroke-linejoin="'+h.lineJoin+'" stroke-miterlimit="'+h.miterLimit+'"></rect>'},strokeCircle:function(a,b,c){return'<circle fill="none" cx="'+a+'" cy="'+b+'" r="'+c+'" stroke="'+h.strokeStyle+'" stroke-width="'+h.lineWidth+'" />'},fillCircle:function(a,b,c){return'<circle stroke="none" cx="'+a+'" cy="'+b+'" r="'+c+'" fill="'+h.fillStyle+'" />'},beginPath:function(){return f=[],""},closePath:function(){var a="";return f.length>0&&(a='<path fill="none" stroke="'+h.strokeStyle+'" stroke-width="'+h.lineWidth+'" stroke-linecap="'+h.lineCap+'" stroke-linejoin="'+h.lineJoin+'" stroke-miterlimit="'+h.miterLimit+'" d="'+f.join(" ")+'" />',f=[]),a},moveTo:function(a,b){return f.push("M "+a+" "+b),""},lineTo:function(a,b){return f.push("L "+a+" "+b),""},quadraticCurveTo:function(a,b,c,d){return f.push("Q "+a+" "+b+" "+c+" "+d),""}},j=setInterval(d,150);return this}}(this),function(){function a(a){var d=a[c],e=b.length;return d||(d=a[c]=e,b[d]={}),b[d]}var b=[0],c="data"+Date.now();window.dataBind=a}(),window.Event={add:function(a,b,c){return a?("string"==typeof a&&(a=document.querySelector(a)),void(a.addEventListener?a.addEventListener(b,c,!1):a.attachEvent?a.attachEvent("on"+b,c):a[b+c]=function(){c(window.event)})):!1},remove:function(a,b,c){return a?("string"==typeof a&&(a=document.querySelector(a)),void(a.removeEventListener?a.removeEventListener(b,c,!1):a.detachEvent?a.detachEvent("on"+b,c):a[b+c]=null)):!1},isRightClick:function(a){return a||(a=window.event),a.which?3===a.which:a.button?2===e.button:!1}},window.deepExtend=function(a){a=a||{};for(var b=1;b<arguments.length;b++){var c=arguments[b];for(var d in c)c.hasOwnProperty(d)&&("object"==typeof c[d]?a[d]=deepExtend(a[d],c[d]):a[d]=c[d])}return a},function(a){function b(a,b){if(!a)throw new Error("Sketchable requires a DOM element.");return"string"==typeof a&&(a=s.querySelector(a)),this.elem=a,this.init(b)}function c(b){var c=b.getBoundingClientRect(),d=s.body,e=s.documentElement,f=a.pageYOffset||e.scrollTop||d.scrollTop,g=a.pageXOffset||e.scrollLeft||d.scrollLeft,h=e.clientTop||d.clientTop||0,i=e.clientLeft||d.clientLeft||0,j=c.top+f-h,k=c.left+g-i;return{top:Math.round(j),left:Math.round(k)}}function d(a,b){b||(b=dataBind(a)[r].options),b.cssCursors&&(a.style.cursor=b.interactive?"pointer":"not-allowed"),a.onselectstart=function(){return!1}}function e(a){var b=a.target,d=c(b);return{x:Math.round(a.pageX-d.left),y:Math.round(a.pageY-d.top),time:Date.now()}}function f(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 g(a){return a.touches?!1:void m(a)}function h(a){return a.touches?!1:void n(a)}function i(a){return a.touches?!1:void o(a)}function j(a){p(a,m),a.preventDefault()}function k(a){p(a,n),a.preventDefault()}function l(a){p(a,o),a.preventDefault()}function m(a){if(Event.isRightClick(a))return!1;var b=Math.abs(a.identifier||0),c=a.target,d=dataBind(c)[r],g=d.options;if(g.interactive){var h=e(a);"function"==typeof g.events.mousedownBefore&&g.events.mousedownBefore(c,d,a),g.graphics.firstPointSize>0&&d.sketch.beginFill(g.graphics.fillStyle).fillCircle(h.x,h.y,g.graphics.firstPointSize).endFill(),d.sketch.isDrawing=!0,d.sketch.beginPath();var i=d.coords[b];i||(i=[]),i.length>0&&d.strokes.push(i),d.coords[b]=[],f(b,d,h),"function"==typeof g.events.mousedown&&g.events.mousedown(c,d,a)}}function n(a){var b=Math.abs(a.identifier||0),c=a.target,d=dataBind(c)[r],g=d.options;if(g.interactive&&(g.mouseupMovements&&0!==d.strokes.length||d.sketch.isDrawing)){var h=e(a);"function"==typeof g.events.mousemoveBefore&&g.events.mousemoveBefore(c,d,a);var i=d.coords[b],j=i[i.length-1];if(j){var k=d.sketch;d.sketch.isDrawing?k.lineStyle(g.graphics.strokeStyle,g.graphics.lineWidth):g.mouseupMovements&&k.lineStyle(g.mouseupMovements.strokeStyle||"#DDD",g.mouseupMovements.lineWidth||1),k.line(j[0],j[1],h.x,h.y).stroke()}f(b,d,h),"function"==typeof g.events.mousemove&&g.events.mousemove(c,d,a)}}function o(a){var b=Math.abs(a.identifier||0),c=a.target,d=dataBind(c)[r],e=d.options;e.interactive&&("function"==typeof e.events.mouseupBefore&&e.events.mouseupBefore(c,d,a),d.sketch.isDrawing=!1,d.sketch.closePath(),d.strokes.push(d.coords[b]),d.coords[b]=[],"function"==typeof e.events.mouseup&&e.events.mouseup(c,d,a))}function p(a,b){var c=a.target,d=dataBind(c)[r],e=d.options;if(e.multitouch)for(var f=a.changedTouches,g=0;g<f.length;g++){var h=f[g];b(h)}else{var h=a.touches[0];b(h)}a.preventDefault()}function q(a){var b=dataBind(a)[r],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 r="sketchable",s=a.document;b.prototype={init:function(a){var c=deepExtend({},b.prototype.defaults,a||{}),e=this.elem,f=dataBind(e)[r];f||(Event.add(e,"mousedown",g),Event.add(e,"mousemove",h),Event.add(e,"mouseup",i),Event.add(e,"touchstart",j),Event.add(e,"touchmove",k),Event.add(e,"touchend",l),d(e,c));var m=new jSketch(e,c.graphics);dataBind(e)[r]=f={strokes:[],coords:{},timestamp:0,sketch:m,instance:this,options:c},"function"==typeof c.events.init&&c.events.init(e,f);for(var n in this.plugins)this.plugins[n](this);return this},config:function(a){var c=this.elem,e=dataBind(c)[r];return a?(e.options=deepExtend({},b.prototype.defaults,e.options,a),d(c),this):e.options},data:function(a){var b=this.elem,c=dataBind(b)[r];return a?c[a]:c},strokes:function(a){var b=this.elem,c=dataBind(b)[r];return a?(c.strokes=a,q(b),this):c.strokes},handler:function(a){var b=this.elem,c=dataBind(b)[r];return a(b,c),this},trigger:function(a){var b=this.elem,c=dataBind(b)[r],d=c.options.events;return d&&"function"==typeof d[a]&&d[a](b,c),this},clear:function(a){var b=this.elem,c=dataBind(b)[r],d=c.options;return c.sketch.clear(),a||(c.strokes=[],c.coords={}),"function"==typeof d.events.clear&&d.events.clear(b,c),this},reset:function(a){var b=this.elem,c=dataBind(b)[r],d=c.options;return this.destroy().init(a),"function"==typeof d.events.reset&&d.events.reset(b,c),this},destroy:function(){var a=this.elem,b=dataBind(a)[r],c=b.options;return Event.remove(a,"mouseup",i),Event.remove(a,"mousemove",h),Event.remove(a,"mousedown",g),Event.remove(a,"touchstart",j),Event.remove(a,"touchmove",k),Event.remove(a,"touchend",l),dataBind(a)[r]=null,"function"==typeof c.events.destroy&&c.events.destroy(a,b),this},decorate:function(a,b,c){var d=this.elem,e=dataBind(d)[r],f=e.options,g="_bound$"+a+"."+c;if(!e[g]){if(e[g]=!0,f.events&&"function"==typeof f.events[a]){var h=f.events[a];f.events[a]=function(){h.apply(this,arguments),b.apply(this,arguments)}}else f.events[a]=b;return this}}},b.prototype.plugins={},b.prototype.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}},a.Sketchable=b}(this),function(a){function b(a){function b(b,c){a.handler(function(a,d){d.sketch.clear(),d.sketch.context.drawImage(b,0,0),d.strokes=c.strokes.slice(),d.sketch.callStack=c.callStack.slice()})}function c(b){a.handler(function(a,c){b&&b.identifier>0?e[f].strokes=c.strokes.slice():(e.push({image:a.toDataURL(),strokes:c.strokes.slice(),callStack:c.sketch.callStack.slice()}),f++)})}function d(a){if(a.ctrlKey)switch(a.which){case 26:a.shiftKey?g.redo():g.undo();break;case 25:g.redo()}}var e=[],f=-1,g=this;this.undo=function(){return f>0&&(f--,this.restore(),a.trigger("mementoundo")),this},this.redo=function(){return f<e.length-1&&(f++,this.restore(),a.trigger("mementoredo")),this},this.reset=function(){return e=[],f=-1,c(),this},this.save=function(b){return c(b),a.trigger("mementosave"),this},this.state=function(){return JSON.parse(JSON.stringify(e[f]))},this.restore=function(c){c||(c=e[f]);var d=new Image;return d.src=c.image,d.onload=function(){b(this,c),a.trigger("mementochange")},this},this.init=function(){return Event.remove(document,"keypress",d),Event.add(document,"keypress",d),a.trigger("mementoinit"),c(),this},this.destroy=function(){return Event.remove(document,"keypress",d),a.trigger("mementodestroy"),this.reset()}}var c="sketchable";Sketchable.prototype.plugins.memento=function(a){for(var d={clear:function(a,b){b.memento.reset()},mouseup:function(a,b,c){b.memento.save(c)},destroy:function(a,b){b.memento.destroy()}},e="mouseup clear destroy".split(" "),f=0;f<e.length;f++){var g=e[f];a.decorate(g,d[g],"memento")}deepExtend(a,{memento:{undo:function(){var b=dataBind(a.elem)[c];return b.memento.undo(),a},redo:function(){var b=dataBind(a.elem)[c];return b.memento.redo(),a},save:function(){var b=dataBind(a.elem)[c];return b.memento.save(),a},state:function(){var b=dataBind(a.elem)[c];return b.memento.state()},restore:function(b){var d=dataBind(a.elem)[c];return d.memento.restore(b),a}}});var h=dataBind(a.elem)[c];h.memento=new b(a),h.memento.init()}}(this),function(a){function b(a){function b(b,c,d){var e=c[d],f=c[d+1];if(0===d||e.strokeId!==f.strokeId){if(b.data.firstPointSize){var g=d>0?f:e;b.beginFill(b.data.strokeStyle).fillCircle(g.x,g.y,b.data.firstPointSize).endFill()}d>0&&a.trigger("animationstep"),b.closePath().beginPath()}e.strokeId===f.strokeId&&b.line(e.x,e.y,f.x,f.y).stroke()}function d(a){return a instanceof Array?{x:a[0],y:a[1],time:a[2],strokeId:a[4]}:a}for(var e=dataBind(a.elem)[c],f=e.sketch,g=e.strokes,h=(e.options.events,e.options.graphics),i=[],j=0;j<g.length;j++)for(var k=g[j],l=0;l<k.length;l++){var m=d(k[l]);m.strokeId||(m.strokeId=j),i.push(m)}a.trigger("animationstart");var n,o=0;f.lineStyle(h.strokeStyle,h.lineWidth),function p(){n=requestAnimationFrame(p);try{b(f,i,o,h)}catch(c){console.error(c),cancelAnimationFrame(n)}++o===i.length-1&&(cancelAnimationFrame(n),a.trigger("animationend"))}(),this.cancel=function(){return cancelAnimationFrame(n),this}}var c="sketchable";Sketchable.prototype.plugins.animate=function(a){for(var d={clear:function(a,b){b.animate&&b.animate.cancel()},destroy:function(a,b){b.animate&&b.animate.cancel()}},e="clear destroy".split(" "),f=0;f<e.length;f++){var g=e[f];a.decorate(g,d[g],"animate")}deepExtend(a,{animate:{strokes:function(){var d=dataBind(a.elem)[c];return d.animate=new b(a),a}}})}}(this),function(a){var b="sketchable";Sketchable.prototype.plugins.serializer=function(a){deepExtend(a,{serializer:{save:function(){var c=dataBind(a.elem)[b];return JSON.stringify({options:c.options,strokes:c.strokes,actions:c.sketch.callStack})},load:function(c){for(var d=dataBind(a.elem)[b],e=JSON.parse(c),f=d.sketch,g=0;g<e.actions.length;g++){var h=e.actions[g];h.property&&"object"!=typeof h.value?f.data[h.property]=h.value:h.method?f[h.method].apply(f,h.args):console.warn("Unknown call:",h)}return d.sketch.callStack=e.actions.slice(),d.strokes=e.strokes.slice(),d.options=e.options,a}}})}}(this),function(a){var b="sketchable";Sketchable.prototype.plugins.svg=function(a){for(var c={clear:function(a,b){b.sketch.callStack=[]},destroy:function(a,b){b.sketch.callStack=[]}},d="clear destroy".split(" "),e=0;e<d.length;e++){var f=d[e];a.decorate(f,c[f],"svg")}deepExtend(a,{svg:{create:function(c){var d=dataBind(a.elem)[b];return d.sketch.toSVG(c),a}}})}}(this); |