xxxxxxxxxx
116
const synth = new Tone.PolySynth(Tone.Synth).toDestination();
let now = Tone.now();
let orbs = [];
let amount=10;
let notes = ["C4", "G3","D3","F3","A3"];
let speeds=[1,2,4,8];
function setup() {
createCanvas(windowWidth, windowHeight);
for (let i = 0; i < amount; i++) {
orbs[i] = new Ball(width / 2, i*(height/amount)+25, 15, speeds[floor(random(4))],0, false, random(360), random(60, 100), random(60, 100), false);
}
colorMode(HSB);
console.log(synth);
}
function draw() {
background(245);
for (let i = 0; i < orbs.length; i++) {
orbs[i].move();
orbs[i].collide();
orbs[i].hitt(notes[floor(random(notes.length))], random(0.1,.1));
orbs[i].show();
if(orbs[i].die()){
orbs.splice(i,1);
orbs.push(new Ball(width / 2, random(25,height-25), 15, speeds[floor(random(4))],0, false, random(360), random(60, 100), random(60, 100), false))
}
}
}
// function keyPressed() {
// now = Tone.now();
// synth.triggerAttack("D4", now);
// synth.triggerAttack("F4", now);
// synth.triggerAttack("A4", now);
// synth.triggerAttack("C5", now);
// synth.triggerAttack("E5", now);
// synth.triggerRelease(["D4", "F4", "A4", "C5", "E5"], now + 0.50);
// }
function hey() {
// console.log("hey")
}
function mousePressed() {
orbs.push(new Ball(mouseX,mouseY, 15, random(-5, 5), 0 , false, random(360), random(60, 100), random(60, 100), false));
}
class Ball {
constructor(_x, _y, _sz, _xsp, _ysp, _ispl, h, s, b, hit) {
this.v = createVector(_x, _y);
this.x = _x;
this.y = _y;
this.size = _sz;
this.speed = createVector(_xsp, _ysp);
this.isPlaying = _ispl;
this.h = h;
this.s = s;
this.b = b;
this.hit = hit;
this.sound = false;
this.synth = new Tone.PolySynth(Tone.Synth).toDestination();
this.pv = createVector(_x, _y);
this.count=16;
}
show() {
fill(this.h, this.s, this.b)
ellipse(this.v.x, this.v.y, this.size);
}
move() {
this.v.add(this.speed);
}
collide() {
if (this.v.x >= width || this.v.x <= 0) {
this.pv = createVector(this.x, this.y);
this.speed.x = -this.speed.x;
this.hit = true;
this.sound = true;
this.count--;
}
if (this.v.y >= height || this.v.y <= 0) {
this.pv = createVector(this.x, this.y);
this.speed.y = -this.speed.y;
this.hit = true;
this.sound = true;
this.count--;
}
}
hitt(note, end) {
this.now = Tone.now();
if (this.hit) {
//this.speed.mult(random(0.99, 1.01));
this.synth.triggerAttack(note, this.now);
// this.synth.triggerAttack("F4", this.now);
// this.synth.triggerAttack("A4", this.now);
this.synth.triggerRelease(note, this.now + end)
this.hit = false;
}
}
die() {
if(this.count<=0){
return true;
}
}
}