Skip to content

Instantly share code, notes, and snippets.

@Sciman101
Last active September 21, 2023 14:44
Show Gist options
  • Save Sciman101/f952382959c0a5c4a80ebf234213b541 to your computer and use it in GitHub Desktop.
Save Sciman101/f952382959c0a5c4a80ebf234213b541 to your computer and use it in GitHub Desktop.
Cohost Bouncy Eggbug
// ==UserScript==
// @name Cohost Bouncy Eggbug
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Boing
// @icon https://www.google.com/s2/favicons?sz=64&domain=cohost.org
// @author @sciman101
// @match *://cohost.org/*
// @grant none
// ==/UserScript==
const randomSign = () => Math.random() < 0.5 ? -1 : 1;
const sign = (x) => x > 0 ? 1 : -1;
const gravity = 900;
const startSpeed = 100;
const size = 200;
const eggbugPicture = 'https://cohost.org/static/f0c56e99113f1a0731b4.svg';
let x = Math.random() * (window.innerWidth - size - 40) + 20;
let y = 20;
let vx = randomSign() * startSpeed;
let vy = 0;
let angle = 0;
let spin = 10 * randomSign();
let juggle = 0;
let eggbug;
let lastTimestamp;
function setJuggle(j) {
juggle = j;
}
function move(time) {
if (time) {
if (lastTimestamp === undefined) {
lastTimestamp = time;
}
let dt = (time - lastTimestamp) / 1000; // Delta time in milliseconds
if (dt > 1) dt = 0.1;
angle += dt * spin;
if (Math.abs(spin) > 10) {
spin -= dt * 75 * sign(spin);
if (Math.abs(spin) < 10) spin = sign(spin) * 10;
}
if (x < 0 || x > window.innerWidth - size) vx *= -1;
if (y < 0 || y > window.innerHeight - size) {
vy *= -0.9;
if (y >= 0) {
setJuggle(0);
}
}
x = Math.max(Math.min(x,window.innerWidth - size),0);
y = Math.max(Math.min(y,window.innerHeight - size),0);
x += vx * dt;
y += vy * dt;
vy += gravity * dt;
eggbug.style.left = x+'px';
eggbug.style.top = y+'px';
eggbug.style.transform = `rotate(${angle}deg)`;
lastTimestamp = time;
}
requestAnimationFrame(move);
}
function makeEggbug() {
eggbug = document.createElement('img');
eggbug.src = eggbugPicture;
document.body.appendChild(eggbug);
eggbug.style.width = size+'px';
eggbug.style.position = 'fixed';
eggbug.style.left = '0px';
eggbug.style.top = '0px';
eggbug.style.zIndex = '9001';
eggbug.style.userSelect = 'none';
eggbug.style.cursor = 'pointer';
eggbug.onclick = () => {
vy = -gravity;
spin = randomSign() * 200;
setJuggle(juggle + 1);
};
}
makeEggbug();
move();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment