1 line
5.6 KiB
JavaScript
1 line
5.6 KiB
JavaScript
"use strict";function sizeAndPos(){var a=c.getImageData(0,0,$c.width(),$c.height()),b=$(window).width(),d=$(window).height()-53;$c.attr("width",b*window.devicePixelRatio),$c.attr("height",d*window.devicePixelRatio),$c.css({width:b,height:d}),c.clear(),c.putImageData(a,0,0)}function relative(a,b,c){var c=c||$c,d=c.offset();return{x:(a-d.left)*window.devicePixelRatio,y:(b-d.top)*window.devicePixelRatio}}function threshold(a,b,c,d,e){var f=e||5;return c+f>=a&&a>=c-f&&d+f>=b&&b>=d-f?!0:!1}function draw(a,b,c,d,e,f){if(e=e||{},f)var g=window.o;else var g=window.c;g.beginPath(),g.globalCompositeOperation="eraser"==settings.type?"destination-out":e.composite||settings.composite,g.lineCap=e.lineCap||settings.lineCap,g.lineJoin=e.lineJoin||settings.lineJoin,g.strokeStyle=e.color||settings.color,g.fillStyle=e.color||settings.color,g.lineWidth=(e.lineWidth||settings.lineWidth)/10,g.moveTo(a,b),g.lineTo(c,d),(!e.noStroke||settings.noStroke)&&g.stroke(),(e.fill||settings.fill)&&g.fill()}function mark(a,b){var c=window.o;c.beginPath(),c.fillStyle="red",c.arc(a,b,3,0,2*Math.PI),c.fill()}function erase(a,b,c,d,e){var e=e||{},f=window.c;f.beginPath(),f.lineWidth=(e.lineWidth||settings.lineWidth)/10,f.globalCompositeOperation="source-out",f.moveTo(a,b),f.lineTo(c,d),window.points=window.points.filter(function(e){return threshold(e.x,e.y,a,b,f.lineWidth)||threshold(e.x,e.y,c,d,f.lineWidth)?!1:!0})}function line(a,b,c){var c=c||{},d=window.o;d.beginPath(),d.lineCap=c.lineCap||settings.lineCap,d.lineJoin=c.lineJoin||settings.lineJoin,d.strokeStyle=c.color||settings.color,d.fillStyle=c.color||settings.color,d.lineWidth=(c.lineWidth||settings.lineWidth)/10;var e=settings.drawingLine.length-1;d.moveTo(settings.drawingLine[e].x,settings.drawingLine[e].y),d.lineTo(a,b),settings.drawingLine.push({x:a,y:b}),d.stroke(),(c.fill||settings.fill)&&d.fill()}function finishLine(a){var a=a||{},b=window.c;o.clear(),b.beginPath(),b.strokeStyle=a.color||settings.color,b.fillStyle=a.color||settings.color,b.lineWidth=(a.lineWidth||settings.lineWidth)/10,b.lineJoin=a.lineJoin||settings.lineJoin,b.lineCap=a.lineJoin||settings.lineJoin,b.moveTo(settings.drawingLine[0].x,settings.drawingLine[0].y);for(var c=1,d=settings.drawingLine.length;d>c;c++)b.lineTo(settings.drawingLine[c].x,settings.drawingLine[c].y);settings.stroke&&b.stroke(),settings.fill&&b.fill(),settings.drawingLine=[],window.points.history.push({data:b.getImageData(0,0,width(),height()),points:window.points.slice(0)}),window.points.history.last=window.points.history.length-1}function undo(){var a=window.points.history;if(a.last>1){var b=a[a.last-1];c.putImageData(b.data,0,0),window.points=b.points.slice(0),window.points.history=a,window.points.history.last=a.last-1}else c.clear(),window.points=[],window.points.history=a,window.points.history.last=0}function redo(){var a=window.points.history;if(a.last<a.length-1){var b=a[a.last+1];c.putImageData(b.data,0,0),window.points=b.points.slice(0),window.points.history=a,window.points.history.last=a.last+1}}function width(){return+$c.attr("width")}function height(){return+$c.attr("height")}function dataToBlob(a){for(var b=atob(a.split(",")[1]),c=[],d=a.split(",")[0].split(":")[1].split(";")[0],e=0;e<b.length;e++)c.push(b.charCodeAt(e));return new Blob([new Uint8Array(c)],{type:d})}function startPoint(a,b){points.length||points.push({x:a,y:b,type:"",start:{x:a,y:b}});var c=points[points.length-1],d=c.start,e={x:a,y:b,start:c.start||{x:a,y:b},type:settings.type};"line"!==c.type&&"line"==e.type&&(mark(a,b),settings.drawingLine.push({x:a,y:b})),"line"==c.type&&"line"==e.type&&("line"!==points[points.indexOf(c)-1].type&&o.clear(),line(a,b)),"shape"==e.type&&(settings.shapeStart=e);var f=window.mobile?[10,5]:[5,2];return points.length>1&&(d&&threshold(d.x,d.y,a,b,f[0])||threshold(c.x,c.y,a,b,f[1]))?(window.active=!1,points[points.length-1].type="",points[points.length-1].start=void 0,void finishLine()):void points.push(e)}function drawPoint(a,b){var c=points[points.length-1];switch(c.type){case"eraser":erase(c.x,c.y,a,b);case"pencil":draw(c.x,c.y,a,b);var d={x:a,y:b,start:c.start,type:c.type};points.push(d);break;case"sketch":draw(c.x,c.y,a,b);var d={x:a,y:b,start:c.start,type:c.type};points.push(d);for(var e=0,f=points.length-1;f>e;e++)if(threshold(points[e].x,points[e].y,d.x,d.y,settings.connectTelorance)){var a=points[e].x-d.x,b=points[e].y-d.y,g=settings.lineWidth/20>.2?settings.lineWidth/20:.2;draw(points[e].x-.2*a,points[e].y-.2*b,d.x+.2*a,d.y+.2*b,{strokeStyle:"rgba(0,0,0,0.4)",lineWidth:g})}break;case"fur":draw(c.x,c.y,a,b);var d={x:a,y:b,start:c.start,type:c.type};points.push(d);for(var e=0,f=points.length-1;f>e;e++)if(threshold(points[e].x,points[e].y,d.x,d.y,settings.connectTelorance)){var a=points[e].x-d.x,b=points[e].y-d.y,h=settings.furLength/100||.2,g=settings.lineWidth/20>.2?settings.lineWidth/20:.2;draw(points[e].x+a*h,points[e].y+b*h,d.x-a*h,d.y-b*h,{strokeStyle:"rgba(0,0,0,0.4)",lineWidth:g})}break;case"shape":o.clear(),o.beginPath(),o.fillStyle=settings.color,o.strokeStyle=settings.color,o.lineWidth=settings.lineWidth/20;var i=settings.shapeStart;switch(settings.shape){case"circle":var j=Math.abs(a-i.x);o.arc(i.x,i.y,j,0,2*Math.PI),settings.comShape={type:"circle",x:i.x,y:i.y,radius:j};break;case"rectangle":var g=a-i.x,k=b-i.y;o.rect(i.x,i.y,g,k),settings.comShape={type:"rectangle",x:i.x,y:i.y,w:g,h:k};break;case"square":var g=a-i.x;o.rect(i.x,i.y,g,g),settings.comShape={type:"rectangle",x:i.x,y:i.y,w:g,h:g};break;case"triangle":var l=(a-i.x)/2,m=(b-i.y)/2;o.moveTo(i.x+l,i.y),o.lineTo(a,b),o.lineTo(i.x,b),o.lineTo(i.x+l,i.y),settings.comShape={type:"triangle",start:{x:i.x,y:i.y},x:a,y:b,dix:l,diy:m}}settings.fill&&o.fill(),settings.stroke&&o.stroke()}} |