diff --git a/src/core/environment.js b/src/core/environment.js index ccc9f6d252..4b84fb8c2e 100644 --- a/src/core/environment.js +++ b/src/core/environment.js @@ -19,28 +19,35 @@ const _windowPrint = window.print; let windowPrintDisabled = false; /** - * The print() function writes to the console area of - * your browser. This function is often helpful for looking at the data a program - * is producing. This function creates a new line of text for each call to - * the function. Individual elements can be separated with quotes ("") and joined - * with the addition operator (+). + * Displays text in the web browser's console. * - * Note that calling print() without any arguments invokes the window.print() - * function which opens the browser's print dialog. To print a blank line - * to console you can write print('\n'). + * `print()` is helpful for printing values while debugging. Each call to + * `print()` creates a new line of text. + * + * Note: Call `print('\n')` to print a blank line. Calling `print()` without + * an argument opens the browser's dialog for printing documents. * * @method print - * @param {Any} contents any combination of Number, String, Object, Boolean, - * Array to print + * @param {Any} contents content to print to the console. * @example - *
- * let x = 10;
- * print('The value of x is ' + x);
- * // prints "The value of x is 10"
- *
+ * function setup() {
+ * // Prints "hello, world" to the console.
+ * print('hello, world');
+ * }
+ *
+ *
+ * function setup() {
+ * let name = 'ada';
+ * // Prints "hello, ada" to the console.
+ * print(`hello, ${name}`);
+ * }
+ *
+ *
+ *
+ *
* function setup() {
+ * background(200);
+ *
+ * // Display the value of
+ * // frameCount.
+ * textSize(30);
+ * textAlign(CENTER, CENTER);
+ * text(frameCount, 50, 50);
+ *
+ * describe('The number 0 written in black in the middle of a gray square.');
+ * }
+ *
+ *
+ *
+ *
+ *
+ * function setup() {
+ * // Set the frameRate to 30.
* frameRate(30);
+ *
* textSize(30);
- * textAlign(CENTER);
+ * textAlign(CENTER, CENTER);
* }
*
* function draw() {
* background(200);
- * text(frameCount, width / 2, height / 2);
- * }
- *
*
- * @alt
- * numbers rapidly counting upward with frame count set to 30.
+ * // Display the value of
+ * // frameCount.
+ * text(frameCount, 50, 50);
+ *
+ * describe('A number written in black in the middle of a gray square. Its value increases rapidly.');
+ * }
+ *
+ *
- * let rectX = 0;
- * let fr = 30; //starting FPS
- * let clr;
+ *
+ *
+ * let x = 0;
+ * let speed = 0.05;
*
- * function setup() {
- * background(200);
- * frameRate(fr); // Attempt to refresh at starting FPS
- * clr = color(255, 0, 0);
+ * function setup() {
+ * // Set the frameRate to 30.
+ * frameRate(30);
* }
*
* function draw() {
* background(200);
- * rectX = rectX + 1 * (deltaTime / 50); // Move Rectangle in relation to deltaTime
- *
- * if (rectX >= width) {
- * // If you go off screen.
- * if (fr === 30) {
- * clr = color(0, 0, 255);
- * fr = 10;
- * frameRate(fr); // make frameRate 10 FPS
- * } else {
- * clr = color(255, 0, 0);
- * fr = 30;
- * frameRate(fr); // make frameRate 30 FPS
- * }
- * rectX = 0;
+ *
+ * // Use deltaTime to calculate
+ * // a change in position.
+ * let deltaX = speed * deltaTime;
+ *
+ * // Update the x variable.
+ * x += deltaX;
+ *
+ * // Reset x to 0 if it's
+ * // greater than 100.
+ * if (x > 100) {
+ * x = 0;
* }
- * fill(clr);
- * rect(rectX, 40, 20, 20);
- * }
- *
*
- * @alt
- * red rect moves left to right, followed by blue rect moving at the same speed
- * with a lower frame rate. Loops.
+ * // Use x to set the circle's
+ * // position.
+ * circle(x, 50, 20);
+ *
+ * describe('A white circle moves from left to right on a gray background. It reappears on the left side when it reaches the right side.');
+ * }
+ *
+ *
- * // To demonstrate, put two windows side by side.
- * // Click on the window that the p5 sketch isn't in!
- * function draw() {
- * background(200);
- * noStroke();
- * fill(0, 200, 0);
- * ellipse(25, 25, 50, 50);
+ *
+ *
+ * // Open this example in two separate browser
+ * // windows placed side-by-side to demonstrate.
*
- * if (!focused) {
- // or "if (focused === false)"
- * stroke(200, 0, 0);
- * line(0, 0, 100, 100);
- * line(100, 0, 0, 100);
+ * function draw() {
+ * // Change the background color
+ * // when the browser window
+ * // goes in/out of focus.
+ * if (focused === true) {
+ * background(0, 255, 0);
+ * } else {
+ * background(255, 0, 0);
* }
- * }
- *
*
- * @alt
- * green 50×50 ellipse at top left. Red X covers canvas when page focus changes
+ * describe('A square changes color from green to red when the browser window is out of focus.');
+ * }
+ *
+ *
- * // Move the mouse across the quadrants
- * // to see the cursor change
+ *
+ *
* function draw() {
- * line(width / 2, 0, width / 2, height);
- * line(0, height / 2, width, height / 2);
+ * background(200);
+ *
+ * // Set the cursor to crosshairs: +
+ * cursor(CROSS);
+ *
+ * describe('A gray square. The cursor appears as crosshairs.');
+ * }
+ *
+ *
+ *
+ *
+ *
+ * function draw() {
+ * background(200);
+ *
+ * // Divide the canvas into quadrants.
+ * line(50, 0, 50, 100);
+ * line(0, 50, 100, 50);
+ *
+ * // Change cursor based on mouse position.
* if (mouseX < 50 && mouseY < 50) {
* cursor(CROSS);
* } else if (mouseX > 50 && mouseY < 50) {
@@ -198,12 +245,29 @@ p5.prototype.focused = document.hasFocus();
* } else {
* cursor('grab');
* }
+ *
+ * describe('A gray square divided into quadrants. The cursor image changes when the mouse moves to each quadrant.');
* }
- *
+ *
+ *
+ * function draw() {
+ * background(200);
+ *
+ * // Change the cursor's active spot
+ * // when the mouse is pressed.
+ * if (mouseIsPressed === true) {
+ * cursor('https://avatars0.githubusercontent.com/u/1617169?s=16', 8, 8);
+ * } else {
+ * cursor('https://avatars0.githubusercontent.com/u/1617169?s=16');
+ * }
+ *
+ * describe('An image of three purple curves follows the mouse. The image shifts when the mouse is pressed.');
+ * }
+ *
+ *
+ * function draw() {
+ * background(200);
*
- *
- * let rectX = 0;
- * let fr = 30; //starting FPS
- * let clr;
+ * // Set the x variable based
+ * // on the current frameCount.
+ * let x = frameCount % 100;
*
- * function setup() {
- * background(200);
- * frameRate(fr); // Attempt to refresh at starting FPS
- * clr = color(255, 0, 0);
+ * // If the mouse is pressed,
+ * // decrease the frame rate.
+ * if (mouseIsPressed === true) {
+ * frameRate(10);
+ * } else {
+ * frameRate(60);
+ * }
+ *
+ * // Use x to set the circle's
+ * // position.
+ * circle(x, 50, 20);
+ *
+ * describe('A white circle on a gray background. The circle moves from left to right in a loop. It slows down when the mouse is pressed.');
* }
+ *
+ *
*
+ *
+ *
* function draw() {
* background(200);
- * rectX += 1; // Move Rectangle
- *
- * if (rectX >= width) {
- * // If you go off screen.
- * if (fr === 30) {
- * clr = color(0, 0, 255);
- * fr = 10;
- * frameRate(fr); // make frameRate 10 FPS
- * } else {
- * clr = color(255, 0, 0);
- * fr = 30;
- * frameRate(fr); // make frameRate 30 FPS
+ *
+ * // If the mouse is pressed, do lots
+ * // of math to slow down drawing.
+ * if (mouseIsPressed === true) {
+ * for (let i = 0; i < 1000000; i += 1) {
+ * random();
* }
- * rectX = 0;
* }
- * fill(clr);
- * rect(rectX, 40, 20, 20);
- * }
- *
*
- * @alt
- * blue rect moves left to right, followed by red rect moving faster. Loops.
+ * // Get the current frame rate
+ * // and display it.
+ * let fps = frameRate();
+ * text(fps, 50, 50);
+ *
+ * describe('A number written in black written on a gray background. The number decreases when the mouse is pressed.');
+ * }
+ *
+ *
+ *
+ *
* function draw() {
+ * background(200);
+ *
+ * // Set the frame rate to 20.
* frameRate(20);
- * text(getTargetFrameRate(), width / 2, height / 2);
+ *
+ * // Get the target frame rate and
+ * // display it.
+ * let fps = getTargetFrameRate();
+ * text(fps, 43, 54);
+ *
+ * describe('The number 20 written in black on a gray background.');
* }
- *
+ *
+ *
+ *
+ *
* function setup() {
+ * // Hide the cursor.
* noCursor();
* }
*
* function draw() {
* background(200);
- * ellipse(mouseX, mouseY, 10, 10);
- * }
- *
*
- * @alt
- * cursor becomes 10×10 white ellipse the moves with mouse x and y.
+ * circle(mouseX, mouseY, 10);
+ *
+ * describe('A white circle on a gray background. The circle follows the mouse as it moves. The cursor is hidden.');
+ * }
+ *
+ *
- * let myFont;
+ *
+ *
+ * function setup() {
+ * background(200);
+ *
+ * // Display the current WebGL version.
+ * text(webglVersion, 42, 54);
+ *
+ * describe('The text "p2d" written in black on a gray background.');
+ * }
+ *
+ *
+ *
+ *
+ *
+ * let font;
+ *
* function preload() {
- * myFont = loadFont('assets/inconsolata.otf');
+ * // Load a font to use.
+ * font = loadFont('assets/inconsolata.otf');
* }
+ *
* function setup() {
+ * // Create a canvas using WEBGL mode.
* createCanvas(100, 50, WEBGL);
- * // Uncomment the following line to see the behavior change in WebGL 1:
- * // setAttributes({ version: 1 })
- *
- * const graphic = createGraphics(30, 30);
- * graphic.background(255);
- * graphic.noStroke();
- * graphic.fill(200);
- * graphic.rect(0, 0, graphic.width/2, graphic.height/2);
- * graphic.rect(
- * graphic.width/2, graphic.height/2,
- * graphic.width/2, graphic.height/2
- * );
- *
- * noStroke();
- * translate(-width/2, -height/2);
- * textureWrap(REPEAT);
- * texture(graphic);
- * beginShape(QUADS);
- * vertex(0, 0, 0, 0, 0);
- * vertex(width, 0, 0, width, 0);
- * vertex(width, height, 0, width, height);
- * vertex(0, height, 0, 0, height);
- * endShape();
- *
- * textFont(myFont);
- * textAlign(CENTER, CENTER);
- * textSize(30);
+ * background(200);
+ *
+ * // Display the current WebGL version.
* fill(0);
- * text('WebGL' + (webglVersion === WEBGL2 ? 2 : 1), 0, 0, width, height);
+ * textFont(font);
+ * text(webglVersion, -15, 5);
+ *
+ * describe('The text "webgl2" written in black on a gray background.');
* }
- *
+ *
+ *
+ * let font;
+ *
+ * function preload() {
+ * // Load a font to use.
+ * font = loadFont('assets/inconsolata.otf');
+ * }
+ *
+ * function setup() {
+ * // Create a canvas using WEBGL mode.
+ * createCanvas(100, 50, WEBGL);
+ *
+ * // Set WebGL to version 1.
+ * setAttributes({ version: 1 });
+ *
+ * background(200);
+ *
+ * // Display the current WebGL version.
+ * fill(0);
+ * textFont(font);
+ * text(webglVersion, -14, 5);
+ *
+ * describe('The text "webgl" written in black on a gray background.');
+ * }
+ *
+ *
- * createCanvas(displayWidth, displayHeight);
- *
+ * function setup() {
+ * // Set the canvas' width and height
+ * // using the display's dimensions.
+ * createCanvas(displayWidth, displayHeight);
+ *
+ * background(200);
+ *
+ * describe('A gray canvas that is the same size as the display.');
+ * }
+ *
+ *
- * createCanvas(displayWidth, displayHeight);
- *
+ * function setup() {
+ * // Set the canvas' width and height
+ * // using the display's dimensions.
+ * createCanvas(displayWidth, displayHeight);
+ *
+ * background(200);
+ *
+ * describe('A gray canvas that is the same size as the display.');
+ * }
+ *
+ *
- * createCanvas(windowWidth, windowHeight);
- *
+ * function setup() {
+ * // Set the canvas' width and height
+ * // using the browser's dimensions.
+ * createCanvas(windowWidth, windowHeight);
+ *
+ * background(200);
+ *
+ * describe('A gray canvas that takes up the entire browser window.');
+ * }
+ *
+ *
- * createCanvas(windowWidth, windowHeight);
- *
+ * function setup() {
+ * // Set the canvas' width and height
+ * // using the browser's dimensions.
+ * createCanvas(windowWidth, windowHeight);
+ *
+ * background(200);
+ *
+ * describe('A gray canvas that takes up the entire browser window.');
+ * }
+ *
+ *
+ *
+ *
* function setup() {
* createCanvas(windowWidth, windowHeight);
* }
*
* function draw() {
- * background(0, 100, 200);
+ * background(200);
+ *
+ * describe('A gray canvas that takes up the entire browser window. It changes size to match the browser window.');
* }
*
+ * // Resize the canvas when the
+ * // browser's size changes.
* function windowResized() {
* resizeCanvas(windowWidth, windowHeight);
* }
- *
+ *
+ *
+ * function setup() {
+ * createCanvas(windowWidth, windowHeight);
+ * }
+ *
+ * function draw() {
+ * background(200);
+ *
+ * describe('A gray canvas that takes up the entire browser window. It changes size to match the browser window.');
+ * }
+ *
+ * function windowResized(event) {
+ * // Resize the canvas when the
+ * // browser's size changes.
+ * resizeCanvas(windowWidth, windowHeight);
+ *
+ * // Print the resize event to the console for debugging.
+ * print(event);
+ * }
+ *
+ *
+ * function setup() {
+ * background(200);
+ *
+ * // Display the canvas' width.
+ * text(width, 42, 54);
+ *
+ * describe('The number 100 written in black on a gray square.');
+ * }
+ *
+ *
+ * function setup() {
+ * createCanvas(50, 100);
+ *
+ * background(200);
+ *
+ * // Display the canvas' width.
+ * text(width, 21, 54);
+ *
+ * describe('The number 50 written in black on a gray rectangle.');
+ * }
+ *
+ *
+ * function setup() {
+ * createCanvas(100, 100);
+ *
+ * background(200);
+ *
+ * // Display the canvas' width.
+ * text(width, 42, 54);
+ *
+ * describe('The number 100 written in black on a gray square. When the mouse is pressed, the square becomes a rectangle and the number becomes 50.');
+ * }
+ *
+ * // If the mouse is pressed, reisze
+ * // the canvas and display its new
+ * // width.
+ * function mousePressed() {
+ * if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
+ * resizeCanvas(50, 100);
+ * background(200);
+ * text(width, 42, 27);
+ * }
+ * }
+ *
+ *
+ * function setup() {
+ * background(200);
+ *
+ * // Display the canvas' height.
+ * text(height, 42, 54);
+ *
+ * describe('The number 100 written in black on a gray square.');
+ * }
+ *
+ *
+ * function setup() {
+ * createCanvas(100, 50);
+ *
+ * background(200);
+ *
+ * // Display the canvas' height.
+ * text(height, 21, 54);
+ *
+ * describe('The number 50 written in black on a gray rectangle.');
+ * }
+ *
+ *
+ * function setup() {
+ * createCanvas(100, 100);
+ *
+ * background(200);
+ *
+ * // Display the canvas' height.
+ * text(height, 42, 54);
+ *
+ * describe('The number 100 written in black on a gray square. When the mouse is pressed, the square becomes a rectangle and the number becomes 50.');
+ * }
+ *
+ * // If the mouse is pressed, reisze
+ * // the canvas and display its new
+ * // height.
+ * function mousePressed() {
+ * if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
+ * resizeCanvas(100, 50);
+ * background(200);
+ * text(height, 42, 27);
+ * }
+ * }
+ *
+ *
- * // Clicking in the box toggles fullscreen on and off.
* function setup() {
* background(200);
+ *
+ * describe('A gray canvas that switches between default and full-screen display when clicked.');
* }
+ *
+ * // If the mouse is pressed,
+ * // toggle full-screen mode.
* function mousePressed() {
- * if (mouseX > 0 && mouseX < 100 && mouseY > 0 && mouseY < 100) {
+ * if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
* let fs = fullscreen();
* fullscreen(!fs);
* }
* }
*
*
* function setup() {
+ * // Set the pixel density to 1.
* pixelDensity(1);
+ *
+ * // Create a canvas and draw
+ * // a circle.
* createCanvas(100, 100);
* background(200);
- * ellipse(width / 2, height / 2, 50, 50);
+ * circle(50, 50, 70);
+ *
+ * describe('A fuzzy white circle on a gray canvas.');
* }
*
*
* function setup() {
- * pixelDensity(3.0);
+ * // Set the pixel density to 3.
+ * pixelDensity(3);
+ *
+ * // Create a canvas, paint the
+ * // background, and draw a
+ * // circle.
* createCanvas(100, 100);
* background(200);
- * ellipse(width / 2, height / 2, 50, 50);
+ * circle(50, 50, 70);
+ *
+ * describe('A sharp white circle on a gray canvas.');
* }
*
*
* function setup() {
- * let density = displayDensity();
- * pixelDensity(density);
+ * // Set the pixel density to 1.
+ * pixelDensity(1);
+ *
+ * // Create a canvas and draw
+ * // a circle.
* createCanvas(100, 100);
* background(200);
- * ellipse(width / 2, height / 2, 50, 50);
+ * circle(50, 50, 70);
+ *
+ * describe('A fuzzy white circle drawn on a gray background. The circle becomes sharper when the mouse is pressed.');
+ * }
+ *
+ * function mousePressed() {
+ * // Get the current display density.
+ * let d = displayDensity();
+ *
+ * // Use the display density to set
+ * // the sketch's pixel density.
+ * pixelDensity(d);
+ *
+ * // Paint the background and
+ * // draw a circle.
+ * background(200);
+ * circle(50, 50, 70);
* }
*
*
- * let url;
- * let x = 100;
- *
* function setup() {
- * fill(0);
- * noStroke();
- * url = getURL();
- * }
- *
- * function draw() {
* background(200);
- * text(url, x, height / 2);
- * x--;
+ *
+ * // Get the sketch's URL
+ * // and display it.
+ * let url = getURL();
+ * textWrap(CHAR);
+ * text(url, 0, 40, 100);
+ *
+ * describe('The URL "https://p5js.org/reference/#/p5/getURL" written in black on a gray background.');
* }
*
*
+ *
+ *
* function setup() {
- * let urlPath = getURLPath();
- * for (let i = 0; i < urlPath.length; i++) {
- * text(urlPath[i], 10, i * 20 + 20);
- * }
- * }
- *
+ * background(200);
*
- * @alt
- * This example does not render anything.
+ * // Get the sketch's URL path
+ * // and display the first
+ * // part.
+ * let path = getURLPath();
+ * text(path[0], 25, 54);
+ *
+ * describe('The word "reference" written in black on a gray background.');
+ * }
+ *
+ *
- * // Example: http://p5js.org?year=2014&month=May&day=15
+ * // Imagine this sketch is hosted at the following URL:
+ * // https://p5js.org?year=2014&month=May&day=15
*
* function setup() {
+ * background(200);
+ *
+ * // Get the sketch's URL
+ * // parameters and display
+ * // them.
* let params = getURLParams();
* text(params.day, 10, 20);
* text(params.month, 10, 40);
* text(params.year, 10, 60);
+ *
+ * describe('The text "15", "May", and "2014" written in black on separate lines.');
* }
*
*