Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

246
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!