Sketchy/build/mobile/js/functions.js

1 line
5.6 KiB
JavaScript
Raw Permalink Normal View History

2014-02-19 21:36:39 +00:00
"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: