xxxxxxxxxx
258
/*
☆☆☆☆☆☆☆☆
☆Black Hole☆
☆☆☆☆☆☆☆☆
by Danny Baghdasarians
☆Mouse & Functions☆
[Mouse Press]: Black Hole Follows Mouse {Toggle On/Off}
☆Keys & Functions☆
[B]: Enter the Black Hole {Toggle On/Off}
[SPACE]: Pause the "[B]: Enter the Black Hole" Animation {Toggle On/Off}
[W]: Wireframe Mode to See Hidden Pattern {Toggle On/Off}
[+]: Increase the Scale of the Black Hole
[-]: Decrease the Scale of the Black Hole
☆Code Functions☆
mousePressed(): Controls what happens when mouse is pressed
keyPressed(): Controls what happens when specific keys are pressed
createInner(x, y): Creation of the Inner Circle of Black Hole
createOuter(x, y, size): Creation of Outer Rectangles of Black Hole (Pattern)
starLocations(): Initialize Random Star Spawning Locations
spawnStars(): Spawn our Stars based on Pre-Defined Locations
*/
// Integer Variables
let angle;
let size;
let allSize;
let starSize;
let trackX;
let trackY;
let blackHoleAngle;
// Boolean Variables
let trackLock;
let blackHoleLock;
let blackWhiteLock;
let blackHolePauseLock;
// Array Variables
let starX = [];
let starY = [];
function setup() {
createCanvas(700, 700);
// Initialize our Star (X,Y) Coordinates
starLocations();
// Initialize our Integer Variables
angle = 0;
size = 100;
allSize = 1;
starSize = 5;
trackX = width / 2;
trackY = height / 2;
blackHoleAngle = 0;
// Initialize our Boolean Variables
trackLock = false;
blackHoleLock = false;
blackWhiteLock = false;
blackHolePauseLock = false;
}
function draw() {
// Spawn in our Stars with pre-defined locations
spawnStars();
// Assisting in Mouse Tracking
if (trackLock) {
trackX = -mouseX;
trackY = -mouseY;
} else {
trackX = width / 2;
trackY = height / 2;
}
// Assisting in "Black Hole Mode"
if (blackHoleLock) {
if (!blackHolePauseLock) {
blackHoleAngle += 0.01;
}
}
// Creation of our Black Hole
createOuter(trackX, trackY, size);
createInner(trackX, trackY);
// Spin our Black Hole
angle += 0.0075;
}
function mousePressed() {
// Mouse Press
if (!trackLock) {
trackLock = true;
} else {
trackLock = false;
}
}
function keyPressed() {
// Key 'B' - "Black Hole Mode"
if (keyCode === 66) {
if (!blackHoleLock) {
blackHoleLock = true;
} else {
blackHoleLock = false;
blackHolePauseLock = false;
blackHoleAngle = 0;
}
}
// Key 'B' - "Black Hole Pause Animation"
if (keyCode === 32) {
if (!blackHolePauseLock) {
blackHolePauseLock = true;
} else {
blackHolePauseLock = false;
}
}
// Key 'W' - "Wireframe Mode"
if (keyCode === 87) {
if (!blackWhiteLock) {
blackWhiteLock = true;
} else {
blackWhiteLock = false;
}
}
// Key '+' - "Scale Up"
if (keyCode === 187) {
if (allSize < 1.5) {
allSize += 0.1;
}
}
// Key '-' - "Scale Down"
if (keyCode === 189) {
if (allSize > 0.2) {
allSize -= 0.1;
}
}
}
//
// Creation of the Inner Circle of Black Hole
//
function createInner(x, y) {
push();
translate(x, y);
// Begin to Shear Blackhole for "Black Hole Mode" //
if (blackHoleLock) {
shearX(blackHoleAngle / 3);
} else {
shearX(0);
}
scale(allSize);
fill(0, 0, 0);
circle(0, 0, 100);
pop();
}
//
// Creation of Outer Rectangles of Black Hole (Pattern)
//
function createOuter(x, y, size) {
for (let expander = 4; expander >= 1; expander -= 0.5) {
for (let amount = 1; amount < 10; amount++) {
for (let rotation = 0; rotation < 10; rotation += 0.9) {
push();
// Follow our Mouse //
if (trackLock) {
translate(-x, -y);
} else {
translate(x, y);
}
rectMode(CORNER);
// Wireframe Mode //
if (blackWhiteLock) {
} else {
strokeWeight(0);
fill(expander * 90, -150 + expander * 70, 100, 10);
}
// Begin to Shear Blackhole for "Black Hole Mode" //
if (blackHoleLock) {
shearX(blackHoleAngle / 3);
} else {
shearX(0);
}
scale(allSize);
rotate(amount / 10 + angle + rotation);
rect(
35,
35,
size * expander - amount * 10,
size * expander - amount * 7
);
pop();
}
}
}
}
//
// Initialize Random Star Spawning Locations
//
function starLocations() {
for (let starAmount = 0; starAmount < 50; starAmount++) {
starX.push(random(0, 700));
starY.push(random(0, 700));
}
}
//
// Spawn our Stars based on Pre-Defined Locations.
//
function spawnStars() {
background(0);
push();
let starColor;
for (let star = 0; star < 50; star++) {
starColor = random(100, 255);
fill(starColor);
circle(starX[star], starY[star], starSize);
}
pop();
}