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

245
Visualizações
I can't figure out how to get these color variables to work in the draw function

I have been trying to use built-in functions in p5.js but I can't figure out what I am doing wrong, the col variable won't assign to moon.brightness rather the moon is always white when I preview it, I assigned my variables col and dor before the setup function and called them in the setup function before I initialised the Moon variable, at a total loss here, new to using p5.js so any help about where I am going wrong would be appreciated

var groundHeight;
var mountain1;
var mountain2;

var tree;

var moon;
var sun;
var darkness;
var brightness;
var col;
var dor;

function setup() {
  cnv = createCanvas(800, 600)
  //set the groundHeight proportional to the canvas size
  groundHeight = (height / 3) * 2;

  //initalise the mountain objects with properties to help draw them to the canvas
  mountain1 = {
    x: 400,
    y: groundHeight,
    height: 320,
    width: 230
  };
  mountain2 = {
    x: 550,
    y: groundHeight,
    height: 200,
    width: 130
  };

  //initalise the tree object
  tree = {
    x: 150,
    y: groundHeight + 20,
    trunkWidth: 40,
    trunkHeight: 150,
    canopyWidth: 120,
    canopyHeight: 100
  };

  //initalise the sun
  sun = {
    x: 150,
    y: 70,
    diameter: 80

  };

  col = (150, 200, 255)
  dor = (255, 255, 255)
  //TASK: intialise a moon object with an extra property for brightness
  moon = {
    brightness: col,
    x: 700,
    y: 70,
    diameter: 80
  };


  //set the initial darkness
  darkness = {
    x: 800,
    y: 600,
    light: col
  };
}

function draw() {
  //TASK: update the values for the moons brightness, the sun's position and the darkness.
  //You can either map this to the mouse's location (i.e. the futher left the mouse is the more daylight) or you can just change the values gradually over time.

  //draw the sky
  background(150, 200, 255);
  noStroke();

  //draw the sun
  fill(255, 255, 0);
  ellipse(sun.x, sun.y, sun.diameter);
  sun.y = map(mouseX, 0, 800, 70, 630)

  fill(moon.brightness)
  ellipse(moon.x, moon.y, moon.diameter);
  moon.brightness = map(mouseX, 0, 800, col, dor);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>

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

0

There are two issues here:

  1. In JavaScript the expression (1, 2, 3) does not create a an array, or a vector, or a tuple, or anything that could be used to represent a color. The comma operator evaluates the expression on the left, and the regardless of the value of that expression, it evaluates an expression on the right.
  2. The p5.js map() function does not work with colors, it only works with numbers. If you're working with colors you need to use lerpColor()

var groundHeight;
var mountain1;
var mountain2;

var tree;

var moon;
var sun;
var col;
var dor;

function setup() {
  cnv = createCanvas(800, 600)
  //set the groundHeight proportional to the canvas size
  groundHeight = (height / 3) * 2;

  //initalise the mountain objects with properties to help draw them to the canvas
  mountain1 = {
    x: 400,
    y: groundHeight,
    height: 320,
    width: 230
  };
  mountain2 = {
    x: 550,
    y: groundHeight,
    height: 200,
    width: 130
  };

  //initalise the tree object
  tree = {
    x: 150,
    y: groundHeight + 20,
    trunkWidth: 40,
    trunkHeight: 150,
    canopyWidth: 120,
    canopyHeight: 100
  };

  //initalise the sun
  sun = {
    x: 150,
    y: 70,
    diameter: 80
  };

  // FIXED: Use the color function to create a p5.Color object from R G and B components
  col = color(150, 200, 255)
  dor = color(255, 255, 255)
  //TASK: intialise a moon object with an extra property for brightness
  moon = {
    brightness: col,
    x: 700,
    y: 70,
    diameter: 80
  };
}

function draw() {
  //TASK: update the values for the moons brightness, the sun's position and the darkness.
  //You can either map this to the mouse's location (i.e. the futher left the mouse is the more daylight) or you can just change the values gradually over time.

  scale
  //draw the sky
  background(150, 200, 255);
  noStroke();

  //draw the sun
  fill(255, 255, 0);
  ellipse(sun.x, sun.y, sun.diameter);
  sun.y = map(mouseX, 0, 800, 70, 630)

  fill(moon.brightness)
  ellipse(moon.x, moon.y, moon.diameter);
  // FIXED: when interpolating between two colors, use lerpColor instead of map
  moon.brightness = lerpColor(col, dor, mouseX / width);
}
html, body {
margin: 0;
padding: 0;
}

canvas {
  transform: scale(0.25);
  transform-origin: top left;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>

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