|
6 | 6 | </head>
|
7 | 7 | <body>
|
8 | 8 | <canvas id="draw" width="800" height="800"></canvas>
|
| 9 | + |
9 | 10 | <script>
|
| 11 | + const canvas = document.querySelector('#draw'); |
| 12 | + const ctx = canvas.getContext('2d'); |
| 13 | + canvas.width = window.innerWidth; |
| 14 | + canvas.height = window.innerHeight; |
| 15 | + |
| 16 | + ctx.strokeStyle = '#BADA55'; |
| 17 | + ctx.lineJoin = 'round'; |
| 18 | + ctx.lineCap = 'round'; |
| 19 | + ctx.lineWidth = 11; |
| 20 | + // ctx.globalCompositeOperation = 'multiply'; |
| 21 | + |
| 22 | + let isDrawing = false; |
| 23 | + let lastX = 0; |
| 24 | + let lastY = 0; |
| 25 | + |
| 26 | + // starting color (red) |
| 27 | + let hue = 0; |
| 28 | + let direction = true; |
| 29 | + |
| 30 | + function draw(e) { |
| 31 | + if (!isDrawing) return; |
| 32 | + // console.log(e); |
| 33 | + |
| 34 | + ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`; |
| 35 | + // ctx.lineWidth = hue; |
| 36 | + |
| 37 | + ctx.beginPath(); |
| 38 | + ctx.moveTo(lastX, lastY); |
| 39 | + ctx.lineTo(e.offsetX, e.offsetY); |
| 40 | + ctx.stroke(); |
| 41 | + |
| 42 | + // lastX = e.offsetX; |
| 43 | + // lastY = e.offsetY; |
| 44 | + [lastX, lastY] = [e.offsetX, e.offsetY]; |
| 45 | + |
| 46 | + // increment values |
| 47 | + hue++; |
| 48 | + if (hue >= 360) { |
| 49 | + hue = 0; |
| 50 | + } |
| 51 | + |
| 52 | + if (ctx.lineWidth >= 60 || ctx.lineWidth <= 10) { |
| 53 | + direction = !direction; |
| 54 | + } |
| 55 | + if (direction) { |
| 56 | + ctx.lineWidth++; |
| 57 | + } else { |
| 58 | + ctx.lineWidth--; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + canvas.addEventListener('mousedown', (e) => { |
| 63 | + isDrawing = true; |
| 64 | + [lastX, lastY] = [e.offsetX, e.offsetY]; |
| 65 | + }); |
| 66 | + |
| 67 | + canvas.addEventListener('mousemove', draw); |
| 68 | + canvas.addEventListener('mouseup', () => isDrawing = false); |
| 69 | + canvas.addEventListener('mouseout', () => isDrawing = false); |
10 | 70 | </script>
|
11 | 71 |
|
12 | 72 | <style>
|
|
0 commit comments