Skip to content

Instantly share code, notes, and snippets.

@kmancher
Last active November 18, 2017 20:53
Show Gist options
  • Select an option

  • Save kmancher/b558beead6960f48405cbb1d9c495c11 to your computer and use it in GitHub Desktop.

Select an option

Save kmancher/b558beead6960f48405cbb1d9c495c11 to your computer and use it in GitHub Desktop.
Sparkle
var unit = 50;
var count;
var mods = [];
function preload() {
bg = loadImage("library-full.jpg");
}
function setup() {
createCanvas(windowWidth, windowHeight);
noStroke();
// var wideCount = width / unit;
// var highCount = height / unit;
// count = wideCount * highCount;
var index = 0;
for (var x = 0; x < 50; x++) {
// mods[index++] = new Module(x*unit, y*unit, unit/2, unit/2,
// random(0.05, 0.8), unit);
mods[index++] = new Module(random(width), random(height),
random(0.05, 0.8), unit);
}
}
function draw() {
background(bg, 100);
Sparkle();
}
function Sparkle() {
for (var i = 0; i < count; i++) {
mods[i].update();
mods[i].draw();
}
}
function Module(_x, _y, _speed, _unit) {
this.x = _x;
this.y = _y;
this.speed = _speed;
this.unit = _unit;
this.xDir = 1;
this.yDir = 1;
this.alpha = random(255);
this.lightUp = true;
}
// Custom method for updating the variables
Module.prototype.update = function() {
// this.x = this.x + (this.speed * this.xDir);
// if (this.x >= this.unit || this.x <= 0) {
// this.xDir *= -1;
// this.x = this.x + (1 * this.xDir);
// this.y = this.y + (1 * this.yDir);
// }
// if (this.y >= this.unit || this.y <= 0) {
// this.yDir *= -1;
// this.y = this.y + (1 * this.yDir);
// }
if(this.lightUp == true) {
this.alpha += 10;
}
if(this.alpha >= 255) {
this.lightUp = false;
}
if(this.lightUp == false) {
this.alpha -= 10;
}
if(this.alpha <= 0) {
// this.x = random(width);
// this.y = random(height);
this.lightUp = true;
}
// this.width = this.height = map(this.alpha, 0, 255, 8, 15);
}
// Custom method for drawing the object
Module.prototype.draw = function() {
fill(random(255), 0, 255, this.alpha);
// ellipse(this.xOff + this.x, this.yOff + this.y, 6, 6);
// star(this.x, this.y, map(this.alpha, 0, 255, 8, 15), 2, 4);
ellipse(this.x, this.y, 10,10);
}
// Star shape
function star(x, y, radius1, radius2, npoints) {
var angle = TWO_PI / npoints;
var halfAngle = angle/2.0;
beginShape();
for (var a = 0; a < TWO_PI; a += angle) {
var sx = x + cos(a) * radius2;
var sy = y + sin(a) * radius2;
vertex(sx, sy);
sx = x + cos(a+halfAngle) * radius1;
sy = y + sin(a+halfAngle) * radius1;
vertex(sx, sy);
}
endShape(CLOSE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment