Created
January 21, 2019 14:51
-
-
Save ksen0/3a14b84cde6cc7fae8897626efb40c15 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Can run as an openprocessing.org sketch | |
// Otherwise, requires p5.js and ps.sound.js | |
var F = 240; | |
var osc, osc2; | |
var playing = false; | |
var spawnX = 0; | |
var currentX = 0; | |
var vertical_drift = 10; | |
var horizontal_drift = 0.2; | |
var currentY = 0; | |
var nextX = 0; | |
var nextY = 0; | |
var current = 0 | |
var notes = [60,62,64,65,67] | |
var lines = [] | |
function setup() { | |
createCanvas(350, 350); | |
background(255,255,255); | |
backgroundColor = color(100,0,100); | |
frameRate(50); | |
colors = new Array(color(100,0,0),color(200,0,100),color(100,200,0),color(0,100,200), | |
color(100,50,0),color(100,200,100),color(100,0,200)); | |
stroke(colors[0]) | |
} | |
function draw() { | |
if (osc == undefined || osc2 == undefined) { | |
osc = new p5.Oscillator(); | |
osc.setType('sine'); | |
osc.freq(F); | |
osc.amp(0); | |
osc.start(); | |
osc2 = new p5.Oscillator(); | |
osc2.setType('sine'); | |
osc2.freq(F + 10); | |
osc2.amp(0); | |
osc2.start(); | |
} | |
background(255,255,255); | |
stroke(color(0)) | |
line(0,mouseY,width,mouseY) | |
nextY = currentY + random(vertical_drift) | |
nextX = currentX + random(horizontal_drift) | |
if (nextY > height || nextX > width) { | |
currentY = 0 | |
currentX = 0 | |
horizontal_drift += random(2) | |
if (nextY < vertical_drift) { | |
horizontal_drift = 1 | |
} | |
current += 1 | |
} | |
else{ | |
stroke(color(200)) | |
line(0,0,currentX,currentY) | |
stroke(colors[current%colors.length]) | |
fill(colors[current%colors.length]) | |
intersect = line_intersect(0,mouseY,width,mouseY,0,0,currentX,currentY) | |
if (intersect!= null && intersect.x >= 0 && intersect.x <= width && intersect.y < currentY){ | |
ellipse(intersect.x, intersect.y, 10) | |
osc.freq(intersect.x%400+50) | |
osc2.freq(intersect.y%400+50) | |
playing = true | |
} else { | |
playing = false | |
} | |
amp_update() | |
line(currentX, currentY, nextX, nextY) | |
currentY = nextY | |
currentX = nextX | |
horizontal_drift | |
} | |
} | |
// http://paulbourke.net/geometry/pointlineplane/ | |
function line_intersect(x1, y1, x2, y2, x3, y3, x4, y4) | |
{ | |
var ua, ub, denom = (y4 - y3)*(x2 - x1) - (x4 - x3)*(y2 - y1); | |
if (denom == 0) { | |
return null; | |
} | |
ua = ((x4 - x3)*(y1 - y3) - (y4 - y3)*(x1 - x3))/denom; | |
ub = ((x2 - x1)*(y1 - y3) - (y2 - y1)*(x1 - x3))/denom; | |
return { | |
x: x1 + ua*(x2 - x1), | |
y: y1 + ua*(y2 - y1), | |
seg1: ua >= 0 && ua <= 1, | |
seg2: ub >= 0 && ub <= 1 | |
}; | |
} | |
function amp_update() { | |
if (playing) { | |
// ramp amplitude to 0.5 over 0.1 seconds | |
osc.amp(0.5, 0.05); | |
osc2.amp(0.5, 0.05); | |
} else { | |
// ramp amplitude to 0 over 0.5 seconds | |
osc.amp(0, 0.5); | |
osc2.amp(0, 0.5); | |
} | |
} | |
function mouseClicked() { | |
lines.push(mouseY) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment