Skip to content

Instantly share code, notes, and snippets.

@GoSubRoutine
Last active August 3, 2019 12:41
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/dc44acdaa0f05c1402ea33bd60a69da4 to your computer and use it in GitHub Desktop.
Save GoSubRoutine/dc44acdaa0f05c1402ea33bd60a69da4 to your computer and use it in GitHub Desktop.
Easing Box
height: 500
<script async src=https://cdn.JsDelivr.net/npm/p5></script>
<script defer src=sketch.js></script>
/**
* 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