How to style a rect.js in p5.js?

Member

by ryleigh , in category: Javascript , 3 months ago

How to style a rect.js in p5.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by cali_green , 3 months ago

@ryleigh 

In p5.js, you can style a rect using the following properties:

  1. fill(): Sets the color inside the shape
  2. stroke(): Sets the color of the shape's outline
  3. strokeWeight(): Sets the thickness of the shape's outline


Here's an example of how you can style a rect in p5.js:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
function setup() {
  createCanvas(400, 400);
}

function draw() {
  background(220);

  // Styling the rect
  fill('red'); // Set the fill color to red
  stroke('blue'); // Set the outline color to blue
  strokeWeight(2); // Set the thickness of the outline

  // Drawing the rect
  rect(50, 50, 100, 100);
}


In this example, we first set the fill color to red using the fill() function, the outline color to blue using the stroke() function, and specify the thickness of the outline to 2 using the strokeWeight() function. Finally, we draw a rect with a size of 100x100 at coordinates (50, 50) on the canvas.


You can experiment with different colors and stroke weights to style the rect according to your preferences.