Hi community,
I’ve watched Handling DOM Events with Callbacks of Coding Train.
In this vidéo, Daniel Shiffman created a button
element in sketch.js
, using a mousePressed() function to control the randomness of background color.
Instead of creating a dom element in sketch.js
, i’d like to link the function to a preexisting element in the index.html
. Il dosen’t work as expected, I kept getting the warning message button.mousePressed is not a function
. Can anyone help me ? Many thanks!
link to editor p5.js
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/addons/p5.sound.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="utf-8" />
</head>
<body>
<button id="click"><p>I am the dysfunctional button </p></button>
<script src="sketch.js"></script>
</body>
</html>
sketch.js
let bgc;
let button =document.getElementById('click');
const t =(sketch)=> {
sketch.setup=()=>{
sketch.createCanvas(400, 400);
bgc = sketch.color(30);
button.mousePressed(sketch.changebgc);
};
sketch.changebgc=()=>{
bgc = sketch.color(sketch.random(0,225));
};
sketch.draw=()=>{
sketch.background(bgc);
sketch.fill(220);
};
};
let bg = new p5(t);