diff options
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/draw/app.js | 111 |
1 files changed, 58 insertions, 53 deletions
diff --git a/tools/draw/app.js b/tools/draw/app.js index d8b6ebf..7ddc45d 100644 --- a/tools/draw/app.js +++ b/tools/draw/app.js | |||
| @@ -1,66 +1,71 @@ | |||
| 1 | let paintStyle = getComputedStyle(document.querySelector('section')); | 1 | window.addEventListener('load', function(evt) { |
| 2 | let canvas = document.querySelector('canvas'); | ||
| 3 | let ctx = canvas.getContext('2d'); | ||
| 4 | 2 | ||
| 5 | canvas.width = parseInt(paintStyle.getPropertyValue('width')); | 3 | let paintStyle = getComputedStyle(document.querySelector('section')); |
| 6 | canvas.height = parseInt(paintStyle.getPropertyValue('height')); | 4 | let canvas = document.querySelector('canvas'); |
| 5 | let ctx = canvas.getContext('2d'); | ||
| 7 | 6 | ||
| 8 | var mouse = { | 7 | canvas.width = parseInt(paintStyle.getPropertyValue('width')); |
| 9 | x: 0, | 8 | canvas.height = parseInt(paintStyle.getPropertyValue('height')); |
| 10 | y: 0 | ||
| 11 | }; | ||
| 12 | 9 | ||
| 13 | ctx.lineWidth = 3; | 10 | var mouse = { |
| 14 | ctx.lineJoin = 'round'; | 11 | x: 0, |
| 15 | ctx.lineCap = 'round'; | 12 | y: 0 |
| 16 | ctx.strokeStyle = 'limegreen'; | 13 | }; |
| 17 | 14 | ||
| 18 | canvas.addEventListener('mousemove', function(e) { | 15 | ctx.lineWidth = 3; |
| 19 | mouse.x = e.pageX - this.offsetLeft; | 16 | ctx.lineJoin = 'round'; |
| 20 | mouse.y = e.pageY - this.offsetTop; | 17 | ctx.lineCap = 'round'; |
| 21 | }, false); | 18 | ctx.strokeStyle = 'limegreen'; |
| 22 | 19 | ||
| 23 | canvas.addEventListener('mousedown', function(e) { | 20 | canvas.addEventListener('mousemove', function(e) { |
| 24 | ctx.beginPath(); | 21 | mouse.x = e.pageX - this.offsetLeft; |
| 25 | ctx.moveTo(mouse.x, mouse.y); | 22 | mouse.y = e.pageY - this.offsetTop; |
| 26 | canvas.addEventListener('mousemove', onPaint, false); | 23 | }, false); |
| 27 | }, false); | ||
| 28 | 24 | ||
| 29 | canvas.addEventListener('mouseup', function() { | 25 | canvas.addEventListener('mousedown', function(e) { |
| 30 | canvas.removeEventListener('mousemove', onPaint, false); | 26 | ctx.beginPath(); |
| 31 | }, false); | 27 | ctx.moveTo(mouse.x, mouse.y); |
| 28 | canvas.addEventListener('mousemove', onPaint, false); | ||
| 29 | }, false); | ||
| 32 | 30 | ||
| 33 | var onPaint = function() { | 31 | canvas.addEventListener('mouseup', function() { |
| 34 | ctx.lineCap = 'round'; | 32 | canvas.removeEventListener('mousemove', onPaint, false); |
| 35 | ctx.lineTo(mouse.x, mouse.y); | 33 | }, false); |
| 36 | ctx.stroke(); | ||
| 37 | }; | ||
| 38 | 34 | ||
| 35 | var onPaint = function() { | ||
| 36 | ctx.lineCap = 'round'; | ||
| 37 | ctx.lineTo(mouse.x, mouse.y); | ||
| 38 | ctx.stroke(); | ||
| 39 | }; | ||
| 39 | 40 | ||
| 40 | document.querySelectorAll('nav button').forEach(function(button, idx) { | ||
| 41 | button.addEventListener('click', function(evt) { | ||
| 42 | console.log(button.dataset.method); | ||
| 43 | 41 | ||
| 44 | switch (button.dataset.method) { | 42 | document.querySelectorAll('nav button').forEach(function(button, idx) { |
| 45 | case 'color': | 43 | button.addEventListener('click', function(evt) { |
| 46 | { | 44 | console.log(button.dataset.method); |
| 47 | ctx.strokeStyle = button.dataset.value; | 45 | |
| 48 | break; | 46 | switch (button.dataset.method) { |
| 49 | } | 47 | case 'color': |
| 50 | case 'size': | 48 | { |
| 51 | { | 49 | ctx.strokeStyle = button.dataset.value; |
| 52 | ctx.lineWidth = parseInt(button.dataset.value); | 50 | break; |
| 53 | break; | 51 | } |
| 54 | } | 52 | case 'size': |
| 55 | case 'clear': | 53 | { |
| 56 | { | 54 | ctx.lineWidth = parseInt(button.dataset.value); |
| 57 | let clear = confirm('Do you really want to clear canvas?'); | 55 | break; |
| 58 | if (clear) { | ||
| 59 | ctx.clearRect(0, 0, canvas.width, canvas.height); | ||
| 60 | } | 56 | } |
| 61 | break; | 57 | case 'clear': |
| 62 | } | 58 | { |
| 63 | } | 59 | let clear = confirm('Do you really want to clear canvas?'); |
| 64 | }); | 60 | if (clear) { |
| 61 | ctx.clearRect(0, 0, canvas.width, canvas.height); | ||
| 62 | } | ||
| 63 | break; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | }); | ||
| 67 | |||
| 68 | }); | ||
| 65 | 69 | ||
| 66 | }); | 70 | }); |
| 71 | |||
