xxxxxxxxxx
145
//Used grosser webcam example from class
// p5 sound library for mic
//the stars were used from a code found here "https://editor.p5js.org/YuqiaoQin/sketches/SyFaITU_g"
//touch functions were used from cclass demos
let cam;
let xScale;
let yScale;
let showCamera = false;
let c = 0;
let f = 0;
var mic;
var stars = [];
var n = 0;
let h = 0;
function setup() {
background(0);
createCanvas(windowWidth, windowHeight);
pixelDensity(displayDensity());
noStroke();
angleMode(DEGREES);
// galaxy();
cam = createCapture(VIDEO); // create capture obj
cam.size(40, 60); // use low res input for speed
cam.hide(); // hide default cam stream display
mic = new p5.AudioIn();
mic.start();
xScale = 1 / 5 * cam.width;
yScale = 1 / 5 * cam.height;
for (var i = 0; i < 600; i++) {
stars[i] = new Star();
}
}
function draw() {
translate(width / 2, height / 2);
var vol = mic.getLevel();
c = c + 5;
if (c > 1000) {
c = 0
}
if (showCamera) cam.show();
else cam.hide();
cam.loadPixels();
if (keyIsDown(SHIFT)) {
translate(0, 0);
c = 3000;
u = 0;
h = 0;
}
if (mouseIsPressed) {
for (var i = 0; i < stars.length; i++) {
stars[i].update();
stars[i].show();
stroke(200);
strokeWeight(0.04);
}
}
for (var e = 0; e <= noise(n) * 100; e++) {
rotate(e);
n += 0.1
e += 0.2;
stroke(255, noise(n) * 60);
strokeWeight(0.2);
fill(0);
light(e);
}
for (let y = 0; y < cam.height; y++) {
for (let x = cam.width; x >= 0; x--) {
let index = (x * y);
let a = 2*cam.pixels[index + mouseX];
let b = cam.pixels[index + mouseY];
let f = cam.pixels[index + 1000 * vol];
if (a != undefined) {
rotate(1 / 16 * c);
fill(a, b, f, 10);
ellipse(
(y * xScale - 100) + c,
(x * yScale - 100) + c,
map(a, 0, width, 80, xScale),
map(a, 0, height, 80, yScale)
);
}
}
}
print(vol)
cam.updatePixels();
console.log(vol);
}
function light(u) {
h = h + 1
line(0, 0, u, h);
if (h > 4000) {
h = 0;
u = 0;
}
}
function Star() {
this.position = createVector(-width, width, -height, height, random(90));
this.velocity = createVector(0, 0, -15);
this.acceleration = createVector(0, 0, -0.0001);
this.update = function() {
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
if (this.position.z < 1) {
this.position.z = 1300;
this.position.x = random(-width, width);
this.position.y = random(-height, height);
}
}
this.show = function() {
fill(255);
stroke(255, 150);
strokeWeight(random(2));
var sx = map(this.position.x / this.position.z, 0, 1, 0, width);
var sy = map(this.position.y / this.position.z, 0, 1, 0, height);
var r = map(this.position.z, 0, width, 10, 0);
ellipse(sx, sy, r, r);
}
}
function touchStarted() {
return false;
}
function touchEnded() {
return false;
}