Last active
August 3, 2019 12:41
-
-
Save GoSubRoutine/dc44acdaa0f05c1402ea33bd60a69da4 to your computer and use it in GitHub Desktop.
Easing Box
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: 500 |
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 async src=https://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
/** | |
* Easing Box (v2.2) | |
* Author: MsKelly (2016-Oct-04) | |
* Mod: GoToLoop | |
* | |
* forum.Processing.org/two/discussion/18404/ | |
* processing-code-doesn-t-work-in-p5#Item_6 | |
* | |
* p5js.SketchPad.cc/sp/pad/view/ro.CQ494WRivD81VV/latest | |
* Bl.ocks.org/GoSubRoutine/dc44acdaa0f05c1402ea33bd60a69da4 | |
*/ | |
"use strict"; | |
const EASING = .05, LIMIT = .1, | |
RAD = 15, EDGE = 50, | |
INNER = EDGE + RAD; | |
let mx = INNER, my = INNER, | |
iw, ih, ew, eh, | |
boxCol, ballCol; | |
function setup() { | |
createCanvas(400, 500); | |
ellipseMode(RADIUS).rectMode(CORNERS); | |
noStroke().background(0); | |
iw = width - INNER, ih = height - INNER; | |
ew = width - EDGE, eh = height - EDGE; | |
boxCol = color(196, 239, 234); | |
ballCol = color(146, 140, 211); | |
} | |
function draw() { | |
const dx = mouseX - mx, dy = mouseY - my; | |
if (abs(dx) > LIMIT) mx = constrain(mx + dx*EASING, INNER, iw); | |
if (abs(dy) > LIMIT) my = constrain(my + dy*EASING, INNER, ih); | |
fill(boxCol).rect(EDGE, EDGE, ew, eh); | |
fill(ballCol).ellipse(mx, my, RAD, RAD); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment