Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

299
Visualizações
how can i take an input from the user in p5.js code?

i want to take an input from the user and after the input the code must run... I want to change the button "click me" as input from the user the code must run after getting input from the user.

var 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("slagmetal.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;
}

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)
  }
  

 // posX=constrain(posX+1,0,rightwall-30)
 // posY=constrain(posX-1,posY,height-50)
}

i want to take an input at "click me" button and after that the code must run. the code consists of two moving images and after taking an input from user the code must run.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There no need to add event listeners , as per documentation of P5.js , on button.mousePressed(changeBG); the function will run , you can add logic of moving images inside the function of changeBg , function draw() call the function inside changeBG method body.

var 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("slagmetal.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();
  // 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)
  }
  

 // posX=constrain(posX+1,0,rightwall-30)
 // posY=constrain(posX-1,posY,height-50)
}

about 4 years ago · Juan Pablo Isaza Relatório

0

Try do this

if (key == //you enter the input that user will type) {
  // enter your code
}

Go to https://www.codecademy.com/courses/learn-p5js/lessons/p5js-interaction/exercises/p5js-using-key-input for any further info

My code is only for 1 key

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda