|
1 | | -/* |
| 1 | +/** |
2 | 2 | * @name Map |
3 | | - * @arialabel Red circle grows larger and turns more yellow as the user’s mouse moves right on the screen and does the opposite as the user’s mouse moves left |
4 | | - * @description Use the map() function to take any number and scale it to a |
5 | | - * new number that is more useful for the project that you are working on. |
6 | | - * For example, use the 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 new numbers |
8 | | - * to define the color and size of a circle. |
| 3 | + * @description The |
| 4 | + * <a href="https://p5js.org/reference/#/p5/map" target="_blank">map()</a> |
| 5 | + * function converts a value from one range to another. In this example, map |
| 6 | + * converts the cursor's horizontal position from a range of 0-720 to 0-360. |
| 7 | + * The resulting value become the circle's hue. Map also converts the cursor's |
| 8 | + * vertical position from a range of 0-400 to 20-300. The resulting value |
| 9 | + * becomes the circle's diameter. |
9 | 10 | */ |
10 | 11 | function setup() { |
11 | 12 | createCanvas(720, 400); |
| 13 | + colorMode(HSB); |
12 | 14 | noStroke(); |
| 15 | + textOutput(); |
13 | 16 | } |
14 | 17 |
|
15 | 18 | function draw() { |
16 | 19 | background(0); |
17 | | - // Scale the mouseX value from 0 to 720 to a range between 0 and 175 |
18 | | - let c = map(mouseX, 0, width, 0, 175); |
19 | | - // Scale the mouseX value from 0 to 720 to a range between 40 and 300 |
20 | | - let d = map(mouseX, 0, width, 40, 300); |
21 | | - fill(255, c, 0); |
22 | | - ellipse(width/2, height/2, d, d); |
| 20 | + |
| 21 | + // Scale the mouseX value from 0 to 720 to a range between 0 and 360 |
| 22 | + let circleHue = map(mouseX, 0, width, 0, 360); |
| 23 | + |
| 24 | + // Scale the mouseY value from 0 to 400 to a range between 20 and 300 |
| 25 | + let diameter = map(mouseY, 0, height, 20, 300); |
| 26 | + |
| 27 | + fill(circleHue, 80, 90); |
| 28 | + circle(width / 2, height / 2, diameter); |
23 | 29 | } |
0 commit comments