Last active
October 21, 2020 05:57
-
-
Save GoSubRoutine/cf739128f389766f5d9cb631c9c358a6 to your computer and use it in GitHub Desktop.
p5.Play Instance Mode Example
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: 600 | |
scrolling: no | |
border: yes | |
license: cc-by-4.0 |
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
<meta charset=utf8> | |
<meta name=viewport content=width=device-width,initial-scale=1> | |
<script defer src=https://cdn.JsDelivr.net/npm/p5></script> | |
<script defer src=https://MolleIndustria.GitHub.io/p5.play/lib/p5.play.js></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
/** | |
* p5.Play Instance Mode Example (v1.0.1) | |
* GoToLoop (2019-Apr-04) | |
* | |
* https://GitHub.com/molleindustria/p5.play/issues/12 | |
* https://Bl.ocks.org/GoSubRoutine/cf739128f389766f5d9cb631c9c358a6 | |
*/ | |
"use strict"; | |
new p5(p => { | |
let bg, sprite; | |
p.setup = function () { | |
p.createCanvas(800, 600).mousePressed(reset); | |
bg = p.color(0o350); | |
sprite = p.createSprite(p.width>>1, p.height>>1, 0o100, 0o100); | |
reset(); | |
}; | |
p.draw = function () { | |
p.background(bg); | |
bounce(); | |
p.drawSprites(); | |
}; | |
function reset() { | |
sprite.setVelocity(p.random(-5, 5), p.random(-5, 5)); | |
sprite.shapeColor = p.color('#' + p.hex(~~p.random(0x1000), 3)); | |
} | |
function bounce() { | |
const { position: { x, y }, width: w, height: h } = sprite, | |
ww = w >> 1, hh = h >> 1; | |
if (x < ww || x + ww >= p.width) sprite.velocity.x *= -1; | |
if (y < hh || y + hh >= p.height) sprite.velocity.y *= -1; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment