简体   繁体   English

如何更改在 P5.js 中创建的按钮的颜色?

[英]How can i change the colour of the button which i have created in P5.js?

i want to change the background colour of the button before and after clicking but the code doesn't seems to work properly?我想在单击前后更改按钮的背景颜色,但代码似乎无法正常工作? i even cannot change the background colour of the canvas while clicking.is there any way to change the colour of the canvas using bg colour我什至无法在单击时更改 canvas 的背景颜色。有没有办法使用 bg 颜色更改 canvas 的颜色

ar playAnim=false;
var pic1;
var pic2;
var pic3;
let button;
let posX=0
let posY=0
const rightwall=350;
const height=600;
function preload(){
  pic1=loadImage("5.png")
  pic2=loadImage("iron.jpg")
  pic3=loadImage("slag.jpg")
}
function setup(){
  createCanvas(600,600);
  background("blue");
  button=createButton("CLICK ME") 
  button.position(200,250);
  button.mousePressed(changeBG);
  
  noLoop();
}
function changeBG() {
  let val = random(65);
  background(val);
  loop();
  playAnim=true;
  draw();
  posX=0;
  posY=0;
  // background will be overwritten with 220
}

function draw() {
  background(220);
  text(mouseX + "," + mouseY, 20, 20);

 
  img1=image(pic1, 300, 30, 150, 200)
  img2=image(pic2, posX, 70, 100, 100)
  img3=image(pic3,posX, posY-300,150, 200)
  if (playAnim) {
  posX=constrain(posX+1,0,rightwall-30)
  posY=constrain(posX-1,posY,height-50)
  }
} 

You should use:你应该使用:

if(mouseIsPressed) {
  /* Your code here */
}

You can visit codecademy p5.js Interaction可以访问 codecademy p5.js 交互

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM