xxxxxxxxxx
var foo = new p5.Speech(); // this is a new p5.speech object bound to the variable foo
var a, b, c;
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(50, 50, 100, 50, "yikes", "hover"); // create an instance of the class myButton
b = new myButton(200, 100, 100, 100, "ouch", "go away"); // create an instance of the class myButton
c = new myButton(300, 400, 200, 200, "ummmmmm", "help"); // create an instance of the class myButton
}
function draw() {
background(255);
a.draw();
b.draw();
c.draw();
}
function mousePressed() {
a.click();
b.click();
c.click();
}
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('hi there');
// 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 = 'green';
}
// 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 = 'green';
}
}
// 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);
}
}
}