xxxxxxxxxx
78
class Piece {
constructor(x, y, image, rand) {
this.x = x;
this.y = y;
this.image = image;
this.rand = rand;
this.speed = random(0.1, 0.4);
this.over = false;
}
draw() {
image(this.image, this.x, this.y);
}
rise() {
this.y -= this.speed;
this.speed *= 1.01;
if (round(this.y % 1) == 0) {
this.image.loadPixels();
for (let i = 0; i < this.image.pixels.length; i += 4) {
this.image.pixels[i] += this.rand[0];
this.image.pixels[i+1] += this.rand[1];
this.image.pixels[i+2] += this.rand[2];
}
this.image.updatePixels();
}
// mark piece as "over" if not visible anymore
if (this.y < -this.image.height) this.over = true;
}
}
class DisintegrationFilter {
constructor(capture, offsetX, offsetY) {
this.capture = capture;
this.offsetX = offsetX;
this.offsetY = offsetY;
this.pieces = [];
}
trigger() {
this.getPieces();
}
draw(isColorOn) {
// clean up pieces that are "over"
this.pieces = this.pieces.filter(p => !p.over);
for (let i = 0; i < this.pieces.length; i++) {
this.pieces[i].rise();
this.pieces[i].draw(isColorOn);
}
}
getPieces() {
if (this.capture.loadedmetadata) {
this.capture.loadPixels();
let rand = [random(1), random(1), random(1)];
for (let x = 0; x < this.capture.width; x += 60) {
for (let y = 0; y < this.capture.height; y += 80) {
let rectX = random(5, 40) + x;
let rectY = random(10, 55) + y;
let rectWidth = random(25, 40);
let rectHeight = random(25, 50);
if (rectX > this.capture.width - rectWidth ||
rectY > this.capture.height - rectHeight) {
continue;
}
let rectImage = this.capture.get(rectX, rectY, rectWidth, rectHeight);
this.pieces.push(new Piece(rectX + this.offsetX, rectY + this.offsetY, rectImage, rand));
}
}
}
}
}