Created
July 23, 2023 15:39
-
-
Save ehzawad/fbbe0c559c0511191f8241c9d32ce377 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <style> | |
| body { | |
| margin: 0; | |
| padding: 0; | |
| overflow: hidden; | |
| } | |
| canvas { | |
| position: absolute; | |
| top: 0; | |
| left: 0; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <canvas id="myCanvas"> | |
| Your browser does not support the HTML canvas tag. | |
| </canvas> | |
| <script> | |
| var canvas = document.getElementById("myCanvas"); | |
| var ctx = canvas.getContext("2d"); | |
| var painting = false; | |
| var lastPoint = { x: 0, y: 0 }; | |
| var lastTime = 0; | |
| var lastWidth = 2; // Initialize pen width | |
| var color = getRandomColor(); // Initialize color | |
| // Set the canvas size to match the window's size | |
| canvas.width = window.innerWidth; | |
| canvas.height = window.innerHeight; | |
| // Update the canvas size when the window is resized | |
| window.onresize = function() { | |
| canvas.width = window.innerWidth; | |
| canvas.height = window.innerHeight; | |
| } | |
| canvas.onmouseenter = function(e) { | |
| painting = true; | |
| lastPoint = { x: e.clientX, y: e.clientY }; | |
| lastTime = Date.now(); | |
| }; | |
| canvas.onmouseleave = function() { | |
| painting = false; | |
| }; | |
| canvas.onmousemove = function(e) { | |
| if (!painting) return; | |
| var currentPoint = { x: e.clientX, y: e.clientY }; | |
| var currentTime = Date.now(); | |
| // calculate the speed | |
| var speed = Math.sqrt( | |
| Math.pow(currentPoint.x - lastPoint.x, 2) + | |
| Math.pow(currentPoint.y - lastPoint.y, 2) | |
| ) / (currentTime - lastTime); | |
| var penWidth = Math.min(20 / speed, 30); // Modify here for speed to size scaling | |
| ctx.beginPath(); | |
| ctx.lineWidth = penWidth; | |
| ctx.lineJoin = "round"; | |
| ctx.lineCap = "round"; | |
| ctx.strokeStyle = color; | |
| // draw the line | |
| ctx.moveTo(lastPoint.x, lastPoint.y); | |
| ctx.lineTo(currentPoint.x, currentPoint.y); | |
| ctx.stroke(); | |
| lastPoint = currentPoint; | |
| lastTime = currentTime; | |
| // if mouse paused for 3 seconds, change the color | |
| if (speed < 0.01) { // adjust this threshold as needed | |
| clearTimeout(colorTimeout); | |
| var colorTimeout = setTimeout(function() { | |
| color = getRandomColor(); | |
| }, 3000); | |
| } | |
| }; | |
| // Function to generate random color | |
| function getRandomColor() { | |
| var letters = '0123456789ABCDEF'; | |
| var color = '#'; | |
| for (var i = 0; i < 6; i++) { | |
| color += letters[Math.floor(Math.random() * 16)]; | |
| } | |
| return color; | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment