From 049affae6a545e114d8ee14671c1228a60fdd90a Mon Sep 17 00:00:00 2001 From: Benjamin Wiegand Date: Tue, 10 Sep 2024 09:22:10 -0700 Subject: [PATCH] bounce duck at edge of screen so it doesn't fly off and expand the document --- www/cursor-thing.js | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/www/cursor-thing.js b/www/cursor-thing.js index c1f1b38..b25fea4 100644 --- a/www/cursor-thing.js +++ b/www/cursor-thing.js @@ -29,6 +29,7 @@ const turnSpeedCoeff = .08; // coefficient of turning // other stuff let brake = false; const brakeDist = 70; +const duckSize = 50; // animation let alt = false; @@ -113,6 +114,26 @@ function update() { xPos += xVel * deltaT; yPos += yVel * deltaT; + // bounce duck at edge of screen + if (xPos > document.scrollingElement.scrollWidth - duckSize) { + xPos = document.scrollingElement.scrollWidth - duckSize; + angle = 2*Math.PI - angle; + } else if (xPos < 0) { + xPos = 0; + angle = 2*Math.PI - angle; + } + if (yPos > document.scrollingElement.scrollHeight - duckSize) { + yPos = document.scrollingElement.scrollHeight - duckSize; + angle = Math.PI - angle; + } else if (yPos < 0) { + yPos = 0; + angle = Math.PI - angle; + } + + // limit angle again + if (angle > 2*Math.PI) angle -= 2*Math.PI; + else if (angle < 0) angle += 2*Math.PI; + // console.log(`accel: ${accel}`); // console.log(`velocity: ${velocity} (${xVel}, ${yVel})`) @@ -128,19 +149,19 @@ function update() { // thing.style['background-color'] = '#ff0000'; // } - let fuck = visualAngle + Math.PI/4; - if (fuck < Math.PI/2 || fuck > Math.PI * 2) { + let tAngle = visualAngle + Math.PI/4; + if (tAngle < Math.PI/2 || tAngle > Math.PI * 2) { // front-facing - thing.style['background-position-x'] = '100px'; - } else if (fuck < Math.PI) { + thing.style['background-position-x'] = (2*duckSize) + 'px'; + } else if (tAngle < Math.PI) { // left-facing thing.style['background-position-x'] = '0px'; - } else if (fuck < Math.PI*3/2) { + } else if (tAngle < Math.PI*3/2) { // right-facing - thing.style['background-position-x'] = '150px'; + thing.style['background-position-x'] = (3*duckSize) + 'px'; } else { // back-facing - thing.style['background-position-x'] = '50px'; + thing.style['background-position-x'] = duckSize + 'px'; } if (thisUpdate - lastAlt > 70) { @@ -149,13 +170,14 @@ function update() { } if (velocity > 50) { - thing.style['background-position-y'] = alt ? '50px' : '100px'; + thing.style['background-position-y'] = (duckSize * (alt + 1)) + 'px'; } else { thing.style['background-position-y'] = '0px'; } } +// scroll and mouse are separate so the cursor is followed when scrolled document.addEventListener('mousemove', e => { mouseX = e.clientX; mouseY = e.clientY; @@ -167,6 +189,7 @@ document.addEventListener('scroll', e => { scrollY = el.scrollTop; }); +// kick-start duck let duck_start = false; function click_duck() { if (duck_start) return;