Last active
November 12, 2019 03:10
-
-
Save GoSubRoutine/fa085945d45152786698f44a9523ccac to your computer and use it in GitHub Desktop.
Damper Balls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
height: 150 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script defer src=http://cdn.JsDelivr.net/npm/p5></script> | |
<script defer src=sketch.js></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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