diff --git a/src/core/p5.Graphics.js b/src/core/p5.Graphics.js index 9fa18e6090..81de1235a6 100644 --- a/src/core/p5.Graphics.js +++ b/src/core/p5.Graphics.js @@ -205,6 +205,48 @@ p5.Graphics = class extends p5.Element { * * @method createFramebuffer * @return {p5.Framebuffer} + * @example + * + *
+ * let framebuffer;
+ * let hue = 0;
+ * let trail = [];
+ * //hover mouse to see effect.
+ *
+ * function setup() {
+ * createCanvas(150, 150, WEBGL);
+ * colorMode(HSB, 360, 100, 100, 100);
+ * framebuffer = createFramebuffer({
+ * format: p5.prototype.UNSIGNED_BYTE,
+ * width: 150,
+ * height: 150,
+ * density: 1
+ * });
+ * frameRate(60);
+ * }
+ *
+ * function draw() {
+ * framebuffer.begin();
+ * blendMode(ADD);
+ * background(0, 0, 0, 5);
+ * noStroke();
+ *
+ * for (let i = trail.length - 1; i >= 0; i--) {
+ * let alpha = map(i, 0, trail.length - 1, 10, 0);
+ * fill(hue, 80, 80, alpha);
+ * ellipse(trail[i].x, trail[i].y, 25, 25);
+ * }
+ * framebuffer.end();
+ * image(framebuffer, -width / 2, -height / 2, width, height);
+ * hue = (hue + 1) % 360;
+ * trail.push(createVector(mouseX - width / 2, mouseY - height / 2));
+ * if (trail.length > 20) {
+ * trail.splice(0, 1);
+ * }
+ * }
+ *
+ *
+ * let myAudio;
+ * let reverb;
+ *
+ * function preload() {
+ * myAudio = loadSound('assets/beat.mp3');
+ * }
+ * function setup() {
+ * createCanvas(150, 150);
+ * textAlign(CENTER);
+ *
+ * // Create a reverb effect
+ * reverb = new p5.Reverb();
+ *
+ * // Connect the audio element to the reverb effect
+ * myAudio.connect(reverb);
+ *
+ * // Connect the reverb effect to the main output
+ * reverb.connect();
+ *
+ * myAudio.play();
+ * }
+ *
+ * function draw() {
+ * background(111, 143, 175);
+ * }
+ *
+ *
+ * let sound;
+ * function preload() {
+ * sound = loadSound('assets/beat.mp3');
+ * }
+ * function setup() {
+ * createCanvas(150, 150);
+ * playSound();
+ * }
+ *
+ * function draw() {
+ * background(159, 43, 104);
+ * }
+ * function mouseClicked() {
+ * // Disconnect the sound on mouse click
+ * disconnectSound();
+ * }
+ * function playSound() {
+ * sound.play();
+ * }
+ * function disconnectSound() {
+ * sound.disconnect();
+ * }
+ *
+ *
+ * let defaultDensity = 1;
+ * let highDensity = 4;
+ * let currentDensity = defaultDensity;
+ *
+ * function setup() {
+ * createCanvas(150, 150);
+ * pixelDensity(defaultDensity);
+ * draw();
+ * }
+ *
+ * function keyPressed() {
+ * // Toggle between default and high pixel densities on key press
+ * if (currentDensity === defaultDensity) {
+ * pixelDensity(highDensity);
+ * currentDensity = highDensity;
+ * }
+ * else {
+ * pixelDensity(defaultDensity);
+ * currentDensity = defaultDensity;
+ * }
+ * draw();
+ * }
+ *
+ * function draw() {
+ * background(230);
+ * stroke(0);
+ * strokeWeight(2);
+ * fill(0);
+ * textSize(32);
+ * textAlign(CENTER, CENTER);
+ * text('Hi.', width / 2, height / 2);
+ * fill(0);
+ * textSize(16);
+ * textAlign(RIGHT, BOTTOM);
+ * text(`Pixel Density: ${currentDensity}`, width - 10, height - 10);
+ * }
+ *
+ *
+ *
+ * function setup() {
+ * createCanvas(150, 150);
+ * myStar();
+ * }
+ *
+ * function myStar() {
+ * beginShape();
+ *
+ * for (let i = 0; i < 10; i++) {
+ * let angle = TWO_PI * i / 10;
+ * let radius = i % 2 === 0 ? 35 : 17.5;
+ * let x = 75 + cos(angle) * radius;
+ * let y = 75 + sin(angle) * radius;
+ * vertex(x, y);
+ * }
+ * endShape(CLOSE);
+ * }
+ *
+ * function draw() {
+ * background(220);
+ * noStroke();
+ * fill(222, 49, 99);
+ * myStar();
+ * }
+ *
+ *
+ * let pg;
+ *
+ * function setup() {
+ * createCanvas(150, 150);
+ * pixelDensity(4);
+ * pg = createGraphics(75, 75);
+ * pg.pixelDensity(1);
+ * }
+ *
+ * function draw() {
+ * background(220);
+ * fill(255, 0, 0);
+ * ellipse(25, 25, 40, 40);
+ * pg.background(255);
+ * pg.fill(0, 0, 255);
+ * pg.ellipse(50, 50, 40, 40);
+ * image(pg, 75, 75);
+ * }
+ *
+ *
+ *
+ * let framebuffer;
+ * function setup() {
+ * createCanvas(100, 100, WEBGL);
+ * framebuffer = createFramebuffer();
+ * noStroke();
+ * }
+ * function draw() {
+ * framebuffer.begin();
+ * background(255);
+ * translate(0, 5 * sin(frameCount * 0.01), 0);
+ * rotateX(frameCount * 0.01);
+ * rotateY(frameCount * 0.01);
+ * fill(255, 0, 0);
+ * torus(30);
+ * framebuffer.end();
+ *
+ * background(100);
+ * image(framebuffer, -50, -50, 25, 25);
+ * image(framebuffer, 0, 0, 35, 35);
+ * }
+ *
+ *
+ *
+ * let customGeometry;
+ * class CustomGeometry {
+ * constructor(size) {
+ * this.vertices = [
+ * createVector(0, -size / 2, 0),
+ * createVector(size / 2, size / 2, size / 2),
+ * createVector(-size / 2, size / 2, size / 2),
+ * createVector(-size / 2, size / 2, -size / 2),
+ * createVector(size / 2, size / 2, -size / 2)
+ * ];
+ * this.faces = [];
+ * this.computeFaces();
+ * }
+ *
+ * computeFaces() {
+ * this.faces = [
+ * [0, 1, 2], // Bottom face
+ * [0, 2, 3], // Left face
+ * [0, 3, 4], // Front face
+ * [0, 4, 1], // Right face
+ * [1, 4, 3], // Back face
+ * [1, 3, 2] // Base face
+ * ];
+ * }
+ * }
+ * function setup() {
+ * createCanvas(150, 150, WEBGL);
+ * customGeometry = new CustomGeometry(50);
+ * customGeometry.computeFaces();
+ * }
+ *
+ * function draw() {
+ * background(150);
+ * rotateX(frameCount * 0.01);
+ * rotateY(frameCount * 0.01);
+ *
+ * fill(255);
+ * stroke(0);
+ * beginShape(TRIANGLES);
+ * for (let face of customGeometry.faces) {
+ * for (let vertexIndex of face) {
+ * let vertexPosition = customGeometry.vertices[vertexIndex];
+ * vertex(vertexPosition.x, vertexPosition.y, vertexPosition.z);
+ * }
+ * }
+ * endShape();
+ * }
+ *
+ *
+ *
+ * let myObject;
+ *
+ * class object {
+ * constructor() {
+ * this.vertices = [
+ * createVector(-15, -15, 0),
+ * createVector(15, -15, 0),
+ * createVector(15, 15, 0),
+ * createVector(-15, 15, 0)
+ * ];
+ *
+ * this.normalize();
+ * }
+ *
+ * normalize() {
+ * for (let v of this.vertices) {
+ * v.normalize();
+ * }
+ * }
+ *
+ * display() {
+ * beginShape();
+ * for (let i = 0; i < this.vertices.length; i++) {
+ * vertex(this.vertices[i].x * 50, this.vertices[i].y * 50,
+ * this.vertices[i].z * 50);
+ * }
+ * endShape(CLOSE);
+ * }
+ * }
+ *
+ * function setup() {
+ * createCanvas(150, 150, WEBGL);
+ * myObject = new object();
+ * }
+ * function draw() {
+ * background(220);
+ * rotateX(frameCount * 0.01);
+ * rotateY(frameCount * 0.01);
+ * myObject.display();
+ * }
+ *
+ *