Skip to content

Commit 758e126

Browse files
committed
adding map() example #154
1 parent e017500 commit 758e126

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

content/examples_basics.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
<ex p5="true" file="OperatorPrecedence.html">Operator Precedence</ex>
7373
<ex p5="true" file="Distance1D.html">Distance 1D</ex>
7474
<ex p5="true" file="Distance2D.html">Distance 2D</ex>
75-
<ex file="Map.html">Map</ex>
75+
<ex p5="true" file="Map.html">Map</ex>
7676
<ex p5="true" file="Sine.html">Sine</ex>
7777
<ex p5="true" file="SineCosine.html">Sine and Cosine</ex>
7878
<ex p5="true" file="SineWave.html">Sine Wave</ex>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Map.
3+
*
4+
* Use the map() function to take any number and scale it to a new number
5+
* that is more useful for the project that you are working on. For example, use the
6+
* numbers from the mouse position to control the size or color of a shape.
7+
* In this example, the mouse’s x-coordinate (numbers between 0 and 360) are scaled to
8+
* new numbers to define the color and size of a circle.
9+
*/
10+
11+
function setup() {
12+
var canvas = createCanvas(640, 360);
13+
canvas.parent("p5container");
14+
noStroke();
15+
}
16+
17+
function draw() {
18+
background(0);
19+
// Scale the mouseX value from 0 to 640 to a range between 0 and 175
20+
var c = map(mouseX, 0, width, 0, 175);
21+
// Scale the mouseX value from 0 to 640 to a range between 40 and 300
22+
var d = map(mouseX, 0, width, 40, 300);
23+
fill(255, c, 0);
24+
ellipse(width/2, height/2, d, d);
25+
}

0 commit comments

Comments
 (0)