JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
HTML Settings
Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.
<!-- click to generate new sprite -->
body{
margin:0;
overflow: hidden;
background:linear-gradient(to top, #6ee 0%, #39c 100%);
min-height:100vh;
text-align: center;
}
canvas {
position: absolute;
left: 0;right: 0;top: 0;bottom: 0;
pointer-events:none;
z-index: -1;
}
let graphics = [];
let gSprite;
let flakes = [];
const SPRITE_AMOUNT = 6;
const DENSITY = .5;
let N = 1;
const DRAW_SIZE_MIN = 20;
const DRAW_SIZE_MAX = 50;
let DRAW_SPRITE = true;
function setup() {
N = Math.floor(DENSITY * (windowWidth*windowHeight)/(DRAW_SIZE_MAX* DRAW_SIZE_MAX));
createCanvas(windowWidth, windowHeight);
imageMode(CENTER);
rectMode(CENTER);
generate_snowflake_sprite();
for (var x = 0; x < N; x++) {
flakes.push(new Snowflake(random(width), random(height)));
}
}
function generate_snowflake_sprite() {
gSprite = createGraphics(100 * SPRITE_AMOUNT, 100);
graphics=[];
for (let x = 0; x < SPRITE_AMOUNT; x++) {
var g = createGraphics(100,100);
generateSnowflake(g);
gSprite.image(g, x*100, 0);
graphics.push(g);
}
}
function windowResized() {
N = Math.floor(DENSITY * (windowWidth*windowHeight)/(DRAW_SIZE_MAX* DRAW_SIZE_MAX));
flakes = [];
resizeCanvas(windowWidth, windowHeight);
for (var x = 0; x < N; x++) {
flakes[x] = (new Snowflake(random(width), random(height)));
}
}
function draw() {
clear();
for (var x = flakes.length - 1 ; x >=0; x--) {
let fl = flakes[x];
fl.update();
fl.draw();
if (fl.isOutOfView()) {
flakes[x] = new Snowflake();
}
}
if (DRAW_SPRITE) {
var scale_factor = 1;
if (gSprite.width > width) scale_factor = width / gSprite.width;
fill(0);
rect(width/2,height/2,(gSprite.width + 10)*scale_factor, (gSprite.height + 10)*scale_factor);
image(gSprite,width/2,height/2, gSprite.width * scale_factor, gSprite.height * scale_factor);
}
}
function mouseReleased(e) {
if (e.button == 0) {
generate_snowflake_sprite();
//DRAW_SPRITE = !DRAW_SPRITE;
}
}
function Snowflake(x,y) {
this.size = random(DRAW_SIZE_MAX - DRAW_SIZE_MIN) + DRAW_SIZE_MIN;
if (typeof x == "undefined") x = random(width)
if (typeof y == "undefined") y = -this.size;
this.pos = createVector(x,y);
this.vel = createVector(0,1);
this.index = Math.floor(random(SPRITE_AMOUNT));
this.spriteSize = 100;
this.rot = random(TWO_PI);
this.rotNoiseX = random(1000);
}
Snowflake.prototype.update = function() {
let v = this.vel.copy();
v.mult(this.size*this.size / 1000);
this.pos.add(v);
this.vel.rotate(random(-.05, .05));
this.rot += (noise(this.rotNoiseX) - .5) / 10;
this.rotNoiseX += .01;
}
Snowflake.prototype.draw = function() {
push();
translate(this.pos.x, this.pos.y);
rotate(this.rot);
image(gSprite, 0, 0, this.size, this.size,
this.spriteSize*this.index, 0, this.spriteSize, this.spriteSize);
pop();
}
Snowflake.prototype.isOutOfView = function() {
return (this.pos.x < -this.size || this.pos.y < -this.size || this.pos.x > width + this.size || this.pos.y > height + this.size);
}
function generateSnowflake(g) {
let snowflake = [];
g.translate(g.width/2, g.height/2);
g.rotate(PI/6);
let current = new Particle(height/2, 0);
while(true) {
let count = 0;
while (!current.finished() && !current.intersects(snowflake)) {
current.update();
count++;
}
snowflake.push(current);
current = new Particle(g.height/2, 0);
// If a particle doesn't move at all we're done
// This is an exit condition not implemented in the video
if (count == 0) {
for (let i = 0; i < 6; i++) {
g.rotate(PI/3);
for (let p of snowflake) {
p.show(g);
}
g.push();
g.scale(1, -1);
for (let p of snowflake) {
p.show(g);
}
g.pop();
}
return;
}
}
}
// Coding Challenge 127: Brownian Motion Snowflake
// Daniel Shiffman
// https://thecodingtrain.com/CodingChallenges/127-brownian-snowflake.html
// https://youtu.be/XUA8UREROYE
class Particle {
constructor(radius, angle) {
this.pos = p5.Vector.fromAngle(angle);
this.pos.mult(radius);
this.r = 1;
}
update() {
this.pos.x -= 1;
this.pos.y += random(-2, 2);
let angle = this.pos.heading();
angle = constrain(angle, 0, PI/6);
let magnitude = this.pos.mag();
this.pos = p5.Vector.fromAngle(angle);
this.pos.setMag(magnitude);
}
show(g) {
g.fill(255);
g.noStroke();
g.ellipse(this.pos.x, this.pos.y, this.r * 2);
}
intersects(snowflake) {
let result = false;
for (let s of snowflake) {
let d = dist(s.pos.x, s.pos.y, this.pos.x, this.pos.y);
if (d < this.r * 2) {
result = true;
break;
}
}
return result;
}
finished() {
return (this.pos.x < 1);
}
}
Also see: Tab Triggers