Skip to content

Instantly share code, notes, and snippets.

@ynaoto
Last active September 12, 2016 01:26
Show Gist options
  • Save ynaoto/51bc44426c8b00b2d22c6f3ec27cd5c4 to your computer and use it in GitHub Desktop.
Save ynaoto/51bc44426c8b00b2d22c6f3ec27cd5c4 to your computer and use it in GitHub Desktop.
var h = 575; // x vertex, half of total bounce duration
var k = 160; // y vertex, total bounce height
var a = -4 * k / Math.pow(h * 2, 2); // coefficient: -.000483932
var ypos, start, time;
function setup() {
createCanvas(66, 226);
fill('#EC245E');
noStroke();
}
function draw() {
var timestamp = millis();
if (!start) { start = timestamp };
time = timestamp - start;
// Position as a function of time, using the vertex form
// of the quadratic formula: f(x) = a(x - h)^2 + k,
// (where [h, k] is the vertex). See it graphically at:
// https://www.desmos.com/calculator/i6yunccp7v
ypos = a * Math.pow(((time + h) % (h * 2) - h), 2) + k;
clear();
ellipse(30, 200 - ypos, 30, 30);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment