|
7 | 7 | <body> |
8 | 8 | <canvas id="draw" width="800" height="800"></canvas> |
9 | 9 | <script> |
| 10 | + const canvas = document.querySelector('#draw'); |
| 11 | + const ctx = canvas.getContext('2d'); |
| 12 | + //resize canvas |
| 13 | + canvas.width = window.innerWidth; |
| 14 | + canvas.height = window.innerHeight; |
| 15 | + ctx.strokeStyle = '#BADA55'; |
| 16 | + ctx.lineJoin = 'round'; |
| 17 | + ctx.lineCap = 'round'; |
| 18 | + ctx.lineWidth = 100; |
| 19 | + // ctx.globalCompositeOperation = 'multiply'; |
| 20 | + //this would be blending the colors on top of each other |
| 21 | + |
| 22 | + let isDrawing = false; |
| 23 | + let lastX = 0; |
| 24 | + let lastY = 0; |
| 25 | + let hue = 0; |
| 26 | + let direction = true; |
| 27 | + //building up ^ |
| 28 | + |
| 29 | + function draw(e){ |
| 30 | + if (!isDrawing) return; |
| 31 | + //function won't run if mouse is not down |
| 32 | + console.log(e); |
| 33 | + ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`; |
| 34 | + // ctx.lineWidth = hue; |
| 35 | + //ctx is where you do all the drawing for the canvas |
| 36 | + ctx.beginPath(); |
| 37 | + //starting from |
| 38 | + ctx.moveTo(lastX, lastY); |
| 39 | + //going to |
| 40 | + ctx.lineTo(e.offsetX, e.offsetY); |
| 41 | + ctx.stroke(); |
| 42 | + // lastX = e.offsetX; |
| 43 | + // lastY = e.offsetY; |
| 44 | + //or |
| 45 | + |
| 46 | + //es6 |
| 47 | + [lastX, lastY] = [e.offsetX, e.offsetY]; |
| 48 | + //destructuring an array |
| 49 | + hue++; |
| 50 | + //if hsl goes over 360, it will keep going |
| 51 | + if (hue >= 360){ |
| 52 | + hue = 0 |
| 53 | + } |
| 54 | + //resetting it |
| 55 | + |
| 56 | + if(ctx.lineWidth >= 100 || ctx.lineWidth <= 1){ |
| 57 | + direction = !direction; |
| 58 | + } |
| 59 | + if (direction){ |
| 60 | + ctx.lineWidth++; |
| 61 | + } else { |
| 62 | + ctx.lineWidth--; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + canvas.addEventListener('mousedown', (e) => { |
| 67 | + isDrawing = true; |
| 68 | + [lastX, lastY] = [e.offsetX, e.offsetY]; |
| 69 | + }); |
| 70 | + canvas.addEventListener('mousemove', draw); |
| 71 | + canvas.addEventListener('mouseup', () => |
| 72 | + isDrawing = false); |
| 73 | + canvas.addEventListener('mouseout', () => |
| 74 | + isDrawing = false); |
10 | 75 | </script> |
11 | 76 |
|
12 | 77 | <style> |
|
0 commit comments