From 490e6528e033df00f2f2bb7c5a9c28fc785d8b14 Mon Sep 17 00:00:00 2001 From: RGBdust <128970981+rgbdust@users.noreply.github.com> Date: Fri, 10 Oct 2025 08:38:54 +0530 Subject: [PATCH] Create colorChange.js --- colorChange.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 colorChange.js diff --git a/colorChange.js b/colorChange.js new file mode 100644 index 00000000..11590872 --- /dev/null +++ b/colorChange.js @@ -0,0 +1,49 @@ + const randomColor = () => { + const r = Math.floor(Math.random() * 256); // 0-255 + const g = Math.floor(Math.random() * 256); // 0-255 + const b = Math.floor(Math.random() * 256); // 0-255 + return `rgb(${r}, ${g}, ${b})`; + } + + let intervalID; + let isChanging = false; + + // Toggle function + function toggleColors() { + if (!isChanging) { + intervalID = setInterval(() => { + document.querySelector('body').style.backgroundColor = randomColor(); + }, 200); + isChanging = true; + } else { + clearInterval(intervalID); + isChanging = false; + } + } + + // Desktop - Spacebar + addEventListener('keydown', function(event) { + if (event.code === 'Space') { + toggleColors(); + } + }); + + // Mobile - Double tap + let lastTap = 0; + document.addEventListener('touchend', function(event) { + const currentTime = new Date().getTime(); + const tapLength = currentTime - lastTap; + if (tapLength < 300 && tapLength > 0) { // double tap within 300ms + toggleColors(); + } + lastTap = currentTime; + }); + + + if (window.innerWidth < 768) { + document.querySelector("h3").innerText = + "Double tap anywhere to start/stop changing background colors"; + } + + +