|
5 | 5 | <title>HTML5 Canvas</title> |
6 | 6 | </head> |
7 | 7 | <body> |
8 | | -<canvas id="draw" width="800" height="800"></canvas> |
| 8 | +<canvas id="js-draw" width="800" height="800"></canvas> |
9 | 9 | <script> |
10 | | -const canvas = document.querySelector('#draw'); |
11 | | -const ctx = canvas.getContext('2d'); |
12 | | -canvas.width = window.innerWidth; |
13 | | -canvas.height = window.innerHeight; |
14 | | -ctx.strokeStyle = '#BADA55'; |
15 | | -ctx.lineJoin = 'round'; |
16 | | -ctx.lineCap = 'round'; |
17 | | -ctx.lineWidth = 100; |
18 | | -// ctx.globalCompositeOperation = 'multiply'; |
| 10 | + |
| 11 | + const canvas = document.querySelector('#js-draw'); |
| 12 | + const ctx = canvas.getContext('2d'); // have to note if the artwork context is 2d or 3d |
| 13 | + canvas.width = window.innerWidth; // fill viewport width |
| 14 | + canvas.height = window.innerHeight; // fill viewport height |
| 15 | + ctx.strokeStyle = '#BADA55'; // default color |
| 16 | + ctx.lineJoin = 'round'; // canvas stroke-linejoin options: mitre | round | bevel | inherit |
| 17 | + ctx.lineCap = 'round'; // canvas stroke-linecap options: butt | round | square | inherit |
| 18 | + ctx.lineWidth = 100; // brush size |
| 19 | + // ctx.globalCompositeOperation = 'multiply'; |
19 | 20 |
|
20 | | -let isDrawing = false; |
21 | | -let lastX = 0; |
22 | | -let lastY = 0; |
23 | | -let hue = 0; |
24 | | -let direction = true; |
| 21 | + let isDrawing = false; |
| 22 | + let lastX = 0; |
| 23 | + let lastY = 0; |
| 24 | + let hue = 0; |
| 25 | + let direction = true; |
25 | 26 |
|
26 | | -function draw(e) { |
27 | | - if (!isDrawing) return; // stop the fn from running when they are not moused down |
28 | | - console.log(e); |
29 | | - ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`; |
30 | | - ctx.beginPath(); |
31 | | - // start from |
32 | | - ctx.moveTo(lastX, lastY); |
33 | | - // go to |
34 | | - ctx.lineTo(e.offsetX, e.offsetY); |
35 | | - ctx.stroke(); |
36 | | - [lastX, lastY] = [e.offsetX, e.offsetY]; |
| 27 | + function draw(e) { |
| 28 | + if (!isDrawing) return; // stop the fn from running when they are not moused down |
| 29 | + console.log(e); |
| 30 | + ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`; |
| 31 | + ctx.beginPath(); |
| 32 | + // start from |
| 33 | + ctx.moveTo(lastX, lastY); |
| 34 | + // go to |
| 35 | + ctx.lineTo(e.offsetX, e.offsetY); |
| 36 | + ctx.stroke(); |
| 37 | + // destructuring an array, values of lastX, lastY are set in a single line |
| 38 | + [lastX, lastY] = [e.offsetX, e.offsetY]; // where the mouse ended up when it stopped moving |
37 | 39 |
|
38 | | - hue++; |
39 | | - if (hue >= 360) { |
40 | | - hue = 0; |
41 | | - } |
42 | | - if (ctx.lineWidth >= 100 || ctx.lineWidth <= 1) { |
43 | | - direction = !direction; |
44 | | - } |
| 40 | + hue++; // starts at 0 and increments to orange, yellow, green, blue, indigo, violet, back to red |
45 | 41 |
|
46 | | - if(direction) { |
47 | | - ctx.lineWidth++; |
48 | | - } else { |
49 | | - ctx.lineWidth--; |
50 | | - } |
| 42 | + // hue goes from red (0) to red (360) across the rainbow as the mouse is held down |
| 43 | + if (hue >= 360) { |
| 44 | + hue = 0; |
| 45 | + } |
51 | 46 |
|
52 | | -} |
| 47 | + // Increment 0 to 100, then 100 down to 0 |
| 48 | + if (ctx.lineWidth >= 100 || ctx.lineWidth <= 1) { |
| 49 | + direction = !direction; |
| 50 | + } |
| 51 | + |
| 52 | + // make the line width thicker or thinner as the mouse is held down |
| 53 | + if(direction) { |
| 54 | + ctx.lineWidth++; |
| 55 | + } else { |
| 56 | + ctx.lineWidth--; |
| 57 | + } |
| 58 | + |
| 59 | + } |
53 | 60 |
|
54 | | -canvas.addEventListener('mousedown', (e) => { |
55 | | - isDrawing = true; |
56 | | - [lastX, lastY] = [e.offsetX, e.offsetY]; |
57 | | -}); |
| 61 | + // before mousemove mousedown fires, so track lastX, lastY |
| 62 | + canvas.addEventListener('mousedown', (e) => { |
| 63 | + isDrawing = true; |
| 64 | + [lastX, lastY] = [e.offsetX, e.offsetY]; |
| 65 | + }); |
58 | 66 |
|
59 | 67 |
|
60 | | -canvas.addEventListener('mousemove', draw); |
61 | | -canvas.addEventListener('mouseup', () => isDrawing = false); |
62 | | -canvas.addEventListener('mouseout', () => isDrawing = false); |
| 68 | + canvas.addEventListener('mousemove', draw); |
| 69 | + canvas.addEventListener('mouseup', () => isDrawing = false); |
| 70 | + canvas.addEventListener('mouseout', () => isDrawing = false); |
63 | 71 |
|
64 | 72 | </script> |
65 | 73 |
|
|
0 commit comments