xxxxxxxxxx
var foo = new p5.Speech(); // this is a new p5.speech object bound to the variable foo
var a, b, c, d, e, f, g, h;
var color;
function setup() {
createCanvas(windowWidth, windowHeight);
background(255);
foo.onLoad = voicesReady; // run the voicesReady() function when we've loaded all our voices
foo.interrupt = true; // allow our speech synthesizer to interrupt itself
a = new myButton(500, 150, 50, 350, "C", ""); // create an instance of the class myButton
b = new myButton(550, 150, 50, 350, "D", ""); // create an instance of the class myButton
c = new myButton(600, 150, 50, 350, "E", ""); // create an instance of the class myButton
d = new myButton(650, 150, 50, 350, "F", ""); // create an instance of the class myButton
e = new myButton(700, 150, 50, 350, "G", ""); // create an instance of the class myButton
f = new myButton(750, 150, 50, 350, "A", ""); // create an instance of the class myButton
g = new myButton(800, 150, 50, 350, "B", ""); // create an instance of the class myButton
h = new myButton(850, 150, 50, 350, "C", ""); // create an instance of the class myButton
}
function draw(){
}
function draw() {
background('gray');
a.draw();
b.draw();
c.draw();
d.draw();
e.draw();
f.draw();
g.draw();
h.draw();
fill(0);
rect(533,150,35,200);
rect(583,150,35,200);
rect(733,150,35,200);
rect(683,150,35,200);
rect(783,150,35,200);
rect(883,150,17,200);
}
function mousePressed() {
a.click();
b.click();
c.click();
d.click();
e.click();
f.click();
g.click();
h.click();
if(b.click()){
foo.setPitch(150);
}
}
function voicesReady() {
foo.listVoices(); // print out all the voices
foo.setVoice("Google UK English Female");
}
// CLASSES: //
// this is our class (ECMA6)
class myButton {
// this is the code that runs when you make one:
constructor(x=50,y=50,w=100,h=50,clickphrase="clicked", hoverphrase="hover") {
foo.speak('play the piano');
// binding class properties to parameters in the constructor:
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.clickphrase = clickphrase;
this.hoverphrase = hoverphrase;
this.hover = false;
this.color = 'white';
}
// this is a draw() method:
draw() {
fill(this.color);
rect(this.x, this.y, this.w, this.h);
// hover test:
if(mouseX>this.x&&mouseY>this.y&&mouseX<this.x+this.w&&mouseY<this.y+this.h)
{
if(this.hover==false) {
foo.speak(this.hoverphrase);
this.hover = true;
this.color = 'red';
}
}
else {
this.hover=false;
this.color = 'white';
}
}
// this a click() method:
click() {
// hit test:
if(mouseX>this.x&&mouseY>this.y&&mouseX<this.x+this.w&&mouseY<this.y+this.h)
{
foo.speak(this.clickphrase);
}
}
}