aboutsummaryrefslogtreecommitdiff
path: root/tools/draw/app.js
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2018-09-19 02:00:43 +0200
committerMitja Felicijan <mitja.felicijan@gmail.com>2018-09-19 02:00:43 +0200
commit1881b541a2083c8d556a4d4477494f134269834f (patch)
tree1db1f7d3e576c9db715ce559d259450602571976 /tools/draw/app.js
parent853166ae4298094e63402e0724ad246d310d9bd6 (diff)
downloadmitjafelicijan.com-1881b541a2083c8d556a4d4477494f134269834f.tar.gz
content update
Diffstat (limited to 'tools/draw/app.js')
-rw-r--r--tools/draw/app.js111
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 @@
1let paintStyle = getComputedStyle(document.querySelector('section')); 1window.addEventListener('load', function(evt) {
2let canvas = document.querySelector('canvas');
3let ctx = canvas.getContext('2d');
4 2
5canvas.width = parseInt(paintStyle.getPropertyValue('width')); 3 let paintStyle = getComputedStyle(document.querySelector('section'));
6canvas.height = parseInt(paintStyle.getPropertyValue('height')); 4 let canvas = document.querySelector('canvas');
5 let ctx = canvas.getContext('2d');
7 6
8var mouse = { 7 canvas.width = parseInt(paintStyle.getPropertyValue('width'));
9 x: 0, 8 canvas.height = parseInt(paintStyle.getPropertyValue('height'));
10 y: 0
11};
12 9
13ctx.lineWidth = 3; 10 var mouse = {
14ctx.lineJoin = 'round'; 11 x: 0,
15ctx.lineCap = 'round'; 12 y: 0
16ctx.strokeStyle = 'limegreen'; 13 };
17 14
18canvas.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
23canvas.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
29canvas.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
33var 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
40document.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