Skip to content

Instantly share code, notes, and snippets.

@GoSubRoutine
Last active November 12, 2019 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GoSubRoutine/fa085945d45152786698f44a9523ccac to your computer and use it in GitHub Desktop.
Save GoSubRoutine/fa085945d45152786698f44a9523ccac to your computer and use it in GitHub Desktop.
Damper Balls
height: 150
<script defer src=http://cdn.JsDelivr.net/npm/p5></script>
<script defer src=sketch.js></script>
/**
* Damper Balls (v1.1)
* KristianP89 (2017-Jun-05)
* Modded GoToLoop
*
* Forum.Processing.org/two/discussion/22925/
* p5-easing-how-can-i-do-easing-with-a-function-value-duration-
* instead-of-creating-new-variables#Item_5
*
* Bl.ocks.org/GoSubRoutine/fa085945d45152786698f44a9523ccac
*/
"use strict";
const DAMPERS = 6, BOLD = 1.5, HEIGHT = 150, CY = HEIGHT >> 1,
dampers = new Set;
let bg;
function setup() {
windowResized();
colorMode(HSL).ellipseMode(CENTER);
strokeWeight(BOLD).stroke(0).frameRate(60);
bg = color(100, 50, 90);
for (let x = width/DAMPERS, c = 360/DAMPERS | 0, i = 0; i < DAMPERS; ++i)
dampers.add(new Damper(x * (i + 1), CY, color(c*i, 50, 50)));
}
function draw() {
background(bg);
let x = mouseX;
for (const d of dampers) ( { x } = d.update(x).display() );
}
function windowResized() {
const { marginLeft, marginRight } = getComputedStyle(canvas.parentElement),
w = windowWidth - round(float(marginLeft) + float(marginRight));
resizeCanvas(w, HEIGHT, true);
}
class Damper {
static get FACTOR() { return .05; }
static get DIAM() { return CY; }
constructor(x = 0, y = 0, col = 0) { this.x = x, this.y = y, this.c = col; }
update(x = 0) { this.x += Damper.FACTOR * (x - this.x); return this; }
display() { fill(this.c).ellipse(this.x, this.y, Damper.DIAM); return this; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment