sketch
// sketch.js template for sound and DOM
//
// This is the 15104 Version 1 template for sound and Dom.
// This template prompts the user to click on the web page
// when it is first loaded.
// The function useSound() must be called in setup() if you
// use sound functions.
// The function soundSetup() is called when it is safe
// to call sound functions, so put sound initialization there.
// (But loadSound() should still be called in preload().)
var img;
var bx = 20;
var by = 400;
var cx = 20;
var cy = 300;
var tx = 20;
var ty = 200;
var mx = 20;
var my = 100;
var bikeTone;
var bikeDynamics;
var carTone;
var carDynamics;
var trainTone;
var trainDynamics;
var motorcycleTone;
var motorcycleDynamics;
function preload() {
// call loadImage() and loadSound() for all media files here
}
function setup() {
// you can change the next 2 lines:
createCanvas(480, 480);
createDiv("p5.dom.js library is loaded.");
//======== call the following to use sound =========
useSound();
}
function soundSetup() { // setup for audio generation
// you can replace any of this with your own audio code:
bikeTone = new p5.Oscillator();
bikeTone.setType('sine');
bikeTone.freq(440.0);
bikeTone.amp(0.1);
bikeTone.start();
bikeDynamics = new p5.Oscillator();
bikeDynamics.setType('sine');
bikeDynamics.disconnect();
bikeDynamics.start();
carTone = new p5.Oscillator();
carTone.setType('sine');
carTone.freq(261.63);
carTone.amp(0.1);
carTone.start();
carDynamics = new p5.Oscillator();
carDynamics.setType('sine');
carDynamics.disconnect();
carDynamics.start();
trainTone = new p5.Oscillator();
trainTone.setType('sine');
trainTone.freq(164.81);
trainTone.amp(0.1);
trainTone.start();
trainDynamics = new p5.Oscillator();
trainDynamics.setType('sine');
trainDynamics.disconnect();
trainDynamics.start();
motorcycleTone = new p5.Oscillator();
motorcycleTone.setType('sine');
motorcycleTone.freq(110);
motorcycleTone.amp(0.1);
motorcycleTone.start();
motorcycleDynamics = new p5.Oscillator();
motorcycleDynamics.setType('sine');
motorcycleDynamics.disconnect();
motorcycleDynamics.start();
}
function draw() {
// you can replace any of this with your own code:
background(255);
stroke(0);
makeBike(bx, by);
bx += .5;
makeCar(cx, cy);
cx += 1;
makeTrain(tx, ty);
tx += 1.5;
makeMotorcycle(mx, my);
mx += 2;
bikeDynamics.amp(0.5);
bikeDynamics.freq(.034);
bikeTone.amp(bikeDynamics);
carDynamics.amp(1);
carDynamics.freq(.06);
carTone.amp(carDynamics);
trainDynamics.amp(2);
trainDynamics.freq(.085);
trainTone.amp(trainDynamics);
motorcycleDynamics.amp(2);
motorcycleDynamics.freq(.12);
motorcycleTone.amp(motorcycleDynamics);
if (millis() > 4200 + soundStart) {
motorcycleTone.stop();
motorcycleDynamics.stop();
}
if (millis() > 6000 + soundStart) {
trainTone.stop();
trainDynamics.stop();
}
if (millis() > 8400 + soundStart) {
carTone.stop();
carDynamics.stop();
}
if (millis() > 15800 + soundStart) {
bikeTone.stop();
bikeDynamics.stop();
}
}
function makeBike(x, y) {
circle(x, y, 20);
circle(x + 40, y, 20);
line(x, y - 10, x + 40, y - 10);
line(x, y - 10, x, y - 20);
line(x + 40, y - 10, x + 40, y - 30);
line(x + 30, y - 25, x + 50, y - 35);
line(x - 10, y - 20, x + 10, y - 20);
}
function makeCar (x, y) {
rect(x - 20, y - 20, 40, 20);
rect(x - 40, y, 80, 20);
circle(x - 30, y + 30, 20);
circle(x + 30, y + 30, 20);
}
function makeTrain(x, y) {
rect(x, y, 20, 20);
rect(x, y + 20, 40, 20);
line(x, y + 30, x - 10, y + 30);
rect(x - 40, y + 20, 30, 20);
line(x - 40, y + 30, x - 50, y + 30);
rect(x - 80, y + 20, 30, 20);
circle(x + 5, y + 45, 10);
circle(x + 35, y + 45, 10);
circle(x - 15, y + 45, 10);
circle(x - 35, y + 45, 10);
circle(x - 55, y + 45, 10);
circle(x - 75, y + 45, 10);
}
function makeMotorcycle(x, y) {
rect(x, y, 40, 20);
triangle(x, y, x + 40, y, x + 40, y - 15);
triangle(x - 20, y, x, y + 15, x, y);
triangle(x + 40, y, x + 60, y, x + 40, y + 15);
circle(x - 10, y + 20, 20);
circle(x + 50, y + 20, 20);
}