|  | @ -8,13 +8,16 @@ var Draw = { | 
		
	
		
			
				|  |  |     ctx: null, |  |  |     ctx: null, | 
		
	
		
			
				|  |  |     draw: null, |  |  |     draw: null, | 
		
	
		
			
				|  |  |     save: null, |  |  |     save: null, | 
		
	
		
			
				|  |  | 
 |  |  |  | 
		
	
		
			
				|  |  |     drawing: false, |  |  |  | 
		
	
		
			
				|  |  |     mousePos: null, |  |  |     mousePos: null, | 
		
	
		
			
				|  |  |     lastPos: null, |  |  |     lastPos: null, | 
		
	
		
			
				|  |  |  |  |  |     drawingData: null, | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |  |     drawing: false, | 
		
	
		
			
				|  |  |     snapBackground: false, |  |  |     snapBackground: false, | 
		
	
		
			
				|  |  |  |  |  |     ratio: 1, | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |     init: function (snapBackground) { |  |  |     init: function (snapBackground) { | 
		
	
		
			
				|  |  |  |  |  |         Draw.drawingData = []; | 
		
	
		
			
				|  |  |         Draw.snapBackground = snapBackground; |  |  |         Draw.snapBackground = snapBackground; | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |         Draw.draw = document.getElementById('draw'); |  |  |         Draw.draw = document.getElementById('draw'); | 
		
	
	
		
			
				|  | @ -26,7 +29,7 @@ var Draw = { | 
		
	
		
			
				|  |  |         Draw.draw.classList.add('open'); |  |  |         Draw.draw.classList.add('open'); | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |         let height, width; |  |  |         let height, width; | 
		
	
		
			
				|  |  |         if (snapBackground) { |  |  |  | 
		
	
		
			
				|  |  |  |  |  |         if (Draw.snapBackground) { | 
		
	
		
			
				|  |  |             const sheight = Snap.canvas.height; |  |  |             const sheight = Snap.canvas.height; | 
		
	
		
			
				|  |  |             const swidth = Snap.canvas.width; |  |  |             const swidth = Snap.canvas.width; | 
		
	
		
			
				|  |  |             const dheight = document.body.clientHeight; |  |  |             const dheight = document.body.clientHeight; | 
		
	
	
		
			
				|  | @ -39,9 +42,11 @@ var Draw = { | 
		
	
		
			
				|  |  |                 if (sheight <= dheight || !s_taller) { |  |  |                 if (sheight <= dheight || !s_taller) { | 
		
	
		
			
				|  |  |                     width = dwidth; |  |  |                     width = dwidth; | 
		
	
		
			
				|  |  |                     height = dwidth * sheight / swidth; |  |  |                     height = dwidth * sheight / swidth; | 
		
	
		
			
				|  |  |  |  |  |                     Draw.ratio = swidth / dwidth; | 
		
	
		
			
				|  |  |                 } else if (swidth <= dwidth || s_taller) { |  |  |                 } else if (swidth <= dwidth || s_taller) { | 
		
	
		
			
				|  |  |                     height = dheight; |  |  |                     height = dheight; | 
		
	
		
			
				|  |  |                     width = dheight * swidth / sheight; |  |  |                     width = dheight * swidth / sheight; | 
		
	
		
			
				|  |  |  |  |  |                     Draw.ratio = sheight / dheight; | 
		
	
		
			
				|  |  |                 } |  |  |                 } | 
		
	
		
			
				|  |  |             } |  |  |             } | 
		
	
		
			
				|  |  |         } else { |  |  |         } else { | 
		
	
	
		
			
				|  | @ -63,7 +68,7 @@ var Draw = { | 
		
	
		
			
				|  |  |         Draw.canvasbg.height = height; |  |  |         Draw.canvasbg.height = height; | 
		
	
		
			
				|  |  |         bgctx = Draw.canvasbg.getContext('2d'); |  |  |         bgctx = Draw.canvasbg.getContext('2d'); | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |         if (snapBackground) { |  |  |  | 
		
	
		
			
				|  |  |  |  |  |         if (Draw.snapBackground) { | 
		
	
		
			
				|  |  |             // copy over snap image
 |  |  |             // copy over snap image
 | 
		
	
		
			
				|  |  |             bgctx.drawImage(Snap.canvas, 0, 0, width, height); |  |  |             bgctx.drawImage(Snap.canvas, 0, 0, width, height); | 
		
	
		
			
				|  |  |         } else { |  |  |         } else { | 
		
	
	
		
			
				|  | @ -139,12 +144,12 @@ var Draw = { | 
		
	
		
			
				|  |  |         clear.addEventListener('click', (e) => { |  |  |         clear.addEventListener('click', (e) => { | 
		
	
		
			
				|  |  |             const rect = Draw.canvas.getBoundingClientRect(); |  |  |             const rect = Draw.canvas.getBoundingClientRect(); | 
		
	
		
			
				|  |  |             Draw.ctx.clearRect(0, 0, rect.width, rect.height); |  |  |             Draw.ctx.clearRect(0, 0, rect.width, rect.height); | 
		
	
		
			
				|  |  |  |  |  |             Draw.drawingData = []; | 
		
	
		
			
				|  |  |         }, false); |  |  |         }, false); | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |         // Save (background +) drawing
 |  |  |         // Save (background +) drawing
 | 
		
	
		
			
				|  |  |         Draw.save = document.getElementById('draw-save'); |  |  |         Draw.save = document.getElementById('draw-save'); | 
		
	
		
			
				|  |  |         Draw.save.onclick = (e) => { |  |  |         Draw.save.onclick = (e) => { | 
		
	
		
			
				|  |  |             const rect = Draw.canvas.getBoundingClientRect(); |  |  |  | 
		
	
		
			
				|  |  |             const finalCanvas = document.createElement('canvas'); |  |  |             const finalCanvas = document.createElement('canvas'); | 
		
	
		
			
				|  |  |             const rect = Draw.canvas.getBoundingClientRect(); |  |  |             const rect = Draw.canvas.getBoundingClientRect(); | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
	
		
			
				|  | @ -157,10 +162,43 @@ var Draw = { | 
		
	
		
			
				|  |  |             } |  |  |             } | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |             const finalctx = finalCanvas.getContext('2d'); |  |  |             const finalctx = finalCanvas.getContext('2d'); | 
		
	
		
			
				|  |  |  |  |  |             finalctx.lineCap = 'round'; | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |             if (Draw.snapBackground) { |  |  |             if (Draw.snapBackground) { | 
		
	
		
			
				|  |  |  |  |  |                 // re-draw upscaled
 | 
		
	
		
			
				|  |  |  |  |  |                 for (let i = 0; i < Draw.drawingData.length; i++) { | 
		
	
		
			
				|  |  |  |  |  |                     finalctx.globalCompositeOperation = Draw.drawingData[i].gco; | 
		
	
		
			
				|  |  |  |  |  |                     finalctx.lineWidth = Draw.drawingData[i].width * Draw.ratio; | 
		
	
		
			
				|  |  |  |  |  |                     finalctx.strokeStyle = Draw.drawingData[i].color; | 
		
	
		
			
				|  |  |  |  |  |                     let j = 0; | 
		
	
		
			
				|  |  |  |  |  |                     finalctx.moveTo( | 
		
	
		
			
				|  |  |  |  |  |                         Draw.drawingData[i].points[j].x * Draw.ratio, | 
		
	
		
			
				|  |  |  |  |  |                         Draw.drawingData[i].points[j].y * Draw.ratio | 
		
	
		
			
				|  |  |  |  |  |                     ); | 
		
	
		
			
				|  |  |  |  |  |                     for (j = 1; j < Draw.drawingData[i].points.length - 2; j++) { | 
		
	
		
			
				|  |  |  |  |  |                         const c = (Draw.drawingData[i].points[j].x + Draw.drawingData[i].points[j + 1].x) / 2; | 
		
	
		
			
				|  |  |  |  |  |                         const d = (Draw.drawingData[i].points[j].y + Draw.drawingData[i].points[j + 1].y) / 2; | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |  |                         finalctx.quadraticCurveTo( | 
		
	
		
			
				|  |  |  |  |  |                             Draw.drawingData[i].points[j].x * Draw.ratio, | 
		
	
		
			
				|  |  |  |  |  |                             Draw.drawingData[i].points[j].y * Draw.ratio, | 
		
	
		
			
				|  |  |  |  |  |                             c * Draw.ratio, | 
		
	
		
			
				|  |  |  |  |  |                             d * Draw.ratio | 
		
	
		
			
				|  |  |  |  |  |                         ); | 
		
	
		
			
				|  |  |  |  |  |                     } | 
		
	
		
			
				|  |  |  |  |  |                     finalctx.quadraticCurveTo( | 
		
	
		
			
				|  |  |  |  |  |                         Draw.drawingData[i].points[j].x * Draw.ratio, | 
		
	
		
			
				|  |  |  |  |  |                         Draw.drawingData[i].points[j].y * Draw.ratio, | 
		
	
		
			
				|  |  |  |  |  |                         Draw.drawingData[i].points[j + 1].x * Draw.ratio, | 
		
	
		
			
				|  |  |  |  |  |                         Draw.drawingData[i].points[j + 1].y * Draw.ratio | 
		
	
		
			
				|  |  |  |  |  |                     ); | 
		
	
		
			
				|  |  |  |  |  |                     finalctx.stroke(); | 
		
	
		
			
				|  |  |  |  |  |                     finalctx.beginPath(); | 
		
	
		
			
				|  |  |  |  |  |                 } | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |  |                 // add background at then end so erased parts look correct
 | 
		
	
		
			
				|  |  |  |  |  |                 finalctx.globalCompositeOperation = 'destination-over'; | 
		
	
		
			
				|  |  |                 finalctx.drawImage(Snap.canvas, 0, 0, Snap.canvas.width, Snap.canvas.height); |  |  |                 finalctx.drawImage(Snap.canvas, 0, 0, Snap.canvas.width, Snap.canvas.height); | 
		
	
		
			
				|  |  |                 finalctx.drawImage(Draw.canvas, 0, 0, Snap.canvas.width, Snap.canvas.height); |  |  |  | 
		
	
		
			
				|  |  |             } else { |  |  |             } else { | 
		
	
		
			
				|  |  |                 const bgimg = document.getElementById('draw-background'); |  |  |                 const bgimg = document.getElementById('draw-background'); | 
		
	
		
			
				|  |  |                 finalctx.drawImage(bgimg, 0, 0, rect.width, rect.height); |  |  |                 finalctx.drawImage(bgimg, 0, 0, rect.width, rect.height); | 
		
	
	
		
			
				|  | @ -184,7 +222,6 @@ var Draw = { | 
		
	
		
			
				|  |  |             this.classList.add('selected'); |  |  |             this.classList.add('selected'); | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |             Draw.ctx.globalCompositeOperation = 'destination-out'; |  |  |             Draw.ctx.globalCompositeOperation = 'destination-out'; | 
		
	
		
			
				|  |  |             Draw.ctx.strokeStyle = 'rgba(0,0,0,1)'; |  |  |  | 
		
	
		
			
				|  |  |         }, false); |  |  |         }, false); | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |         // Change pencil color
 |  |  |         // Change pencil color
 | 
		
	
	
		
			
				|  | @ -230,19 +267,29 @@ var Draw = { | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |         // Add a fleg to not re-bind event listeners
 |  |  |         // Add a fleg to not re-bind event listeners
 | 
		
	
		
			
				|  |  |         Draw.draw.classList.add('bound'); |  |  |         Draw.draw.classList.add('bound'); | 
		
	
		
			
				|  |  | 
 |  |  |  | 
		
	
		
			
				|  |  |     }, |  |  |     }, | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |     stopDrawing: function(e) { |  |  |     stopDrawing: function(e) { | 
		
	
		
			
				|  |  |         Draw.drawing = false; |  |  |         Draw.drawing = false; | 
		
	
		
			
				|  |  |         Draw.lastPos = null; |  |  |         Draw.lastPos = null; | 
		
	
		
			
				|  |  |         Draw.mousePos = null; |  |  |         Draw.mousePos = null; | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  |         Draw.ctx.beginPath(); |  |  |         Draw.ctx.beginPath(); | 
		
	
		
			
				|  |  |     }, |  |  |     }, | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |     startDrawing: function(e) { |  |  |     startDrawing: function(e) { | 
		
	
		
			
				|  |  |         if (e.buttons == 1) { |  |  |         if (e.buttons == 1) { | 
		
	
		
			
				|  |  |             Draw.drawing = true; |  |  |             Draw.drawing = true; | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |  |             // save data
 | 
		
	
		
			
				|  |  |  |  |  |             const data = { | 
		
	
		
			
				|  |  |  |  |  |                 gco: Draw.ctx.globalCompositeOperation, | 
		
	
		
			
				|  |  |  |  |  |                 width: Draw.ctx.lineWidth, | 
		
	
		
			
				|  |  |  |  |  |                 color: Draw.ctx.strokeStyle, | 
		
	
		
			
				|  |  |  |  |  |                 points: [] | 
		
	
		
			
				|  |  |  |  |  |             } | 
		
	
		
			
				|  |  |  |  |  |             Draw.drawingData.push(data); | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  |             Draw.lastPos = Draw.getPos(Draw.canvas, e); |  |  |             Draw.lastPos = Draw.getPos(Draw.canvas, e); | 
		
	
		
			
				|  |  |         } |  |  |         } | 
		
	
		
			
				|  |  |     }, |  |  |     }, | 
		
	
	
		
			
				|  | @ -258,6 +305,12 @@ var Draw = { | 
		
	
		
			
				|  |  |             x = event.clientX - rect.left; |  |  |             x = event.clientX - rect.left; | 
		
	
		
			
				|  |  |             y = event.clientY - rect.top; |  |  |             y = event.clientY - rect.top; | 
		
	
		
			
				|  |  |         } |  |  |         } | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |  |         if (Draw.drawing) { | 
		
	
		
			
				|  |  |  |  |  |             let points = Draw.drawingData[Draw.drawingData.length - 1].points; | 
		
	
		
			
				|  |  |  |  |  |             points.push({ x, y }); | 
		
	
		
			
				|  |  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  |         return { x, y }; |  |  |         return { x, y }; | 
		
	
		
			
				|  |  |     }, |  |  |     }, | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
	
		
			
				|  | 
 |