Open In App

p5.js spotLight() Method

The spotLight() function in p5.js is used to create a spotlight with a given color, position, direction of light, angle and concentration. A scene can have a maximum of 5 spotlights active at one time. The coordinates of the spotlight can be set according to the below-given diagram.

Syntax: This method can be used with multiple parameters depending on the syntax.



spotLight(v1, v2, v3, x, y, z, rx, ry, rz, [angle], [conc])
spotLight(color, position, direction, [angle], [conc])
spotLight(v1, v2, v3, position, direction, [angle], [conc])
spotLight(color, x, y, z, direction, [angle], [conc])
spotLight(color, position, rx, ry, rz, [angle], [conc])
spotLight(v1, v2, v3, x, y, z, direction, [angle], [conc])
spotLight(v1, v2, v3, position, rx, ry, rz, [angle], [conc])
spotLight(color, x, y, z, rx, ry, rz, [angle], [conc])

Parameters: This function accept fifteen parameters as mentioned above and described below:

Below examples illustrates the spotLight() function in p5.js:



Example 1: This example show the spotlight at one specific position.




function setup() {
   
  // Creating canvas with width
  // and height of 100
  createCanvas(100, 100, WEBGL);
}
 
function draw() {
 
  // Setting background colour
  // to black
  background(0);
   
  // Setting the spotlight
  spotLight(0, 250, 0, 20, 20,
            100, 0, 0, -1, Math.PI );
  noStroke();
   
  // Drawing a sphere to show
  // the spotlight
  sphere(40);
}

 

 

Output:

 

 

Example 2: This example illustrates how to change the spotlight position based on the mouse movement.

 




function setup() {
   
  // Creating canvas with width
  // and height to 100
  createCanvas(100, 100, WEBGL);
}
 
function draw() {
  // Setting background colour
  // to black
  background(0);
   
  // Getting position based
  // on mouse movement
  let locX = mouseX - width / 2;
  let locY = mouseY - height / 2;
   
  // Setting the spotlight
  spotLight(0, 250, 0, locX, locY,
            100, 0, 0, -1, Math.PI );
  noStroke();
   
  // Drawing a sphere to show
  // the spotlight
  sphere(40);
}

 

 

Output:

 

Reference:https://p5js.org/reference/#/p5/spotLight
 


Article Tags :