// Example 11 - Random // globals var a,b,c,d,choices; function setup() { createCanvas(640, 480); // no stroke, green fill noStroke(); textSize(18); choices = ["A","AB","BA","C"]; setRandom(); } // Each frame print values for random values function draw() { background(192); text("random(-5,5) = " + a, 0.25*width, 0.2*height); text("random(5) = " + b, 0.25*width, 0.4*height); text("random() = " + c, 0.25*width, 0.6*height); text("random(choices) = " + d, 0.25*width, 0.8*height); } // call setRandom whenever mouse is clicked function mousePressed() { setRandom(); } // new values for the random variables function setRandom(){ a = random(-5,5); b = random(5); c = random(); d = random(choices); }
p5.js Tutorials
Friday, June 30, 2017
Example 11 - Random
Example 10 - Text
// Example 10 - Text function setup() { createCanvas(640, 480); // gray background background(192); // black stroke of weight 2, green fill stroke(0); strokeWeight(2); fill(0, 255, 0) // Size of text, center in text box textSize(64); textAlign(CENTER, CENTER); // text box size of width/2, height/2 // top left anchor at width/4, height/4 text("Hello World!!!", 0.25*width, 0.25*height, 0.5*width, 0.5*height); }
Output:
Example 9 - Point
// Example 9 - Point function setup() { createCanvas(640, 480); // gray background background(192); // magenta stroke of 50 stroke(255, 0, 255); strokeWeight(50); // top-left point(0.25*width, 0.25*height); // top-right point(0.75*width, 0.25*height); // bottom-left point(0.25*width, 0.75*height); // bottom-right point(0.75*width, 0.75*height); }
Output:
Example 8 - Arc
// Example 8 - Arc function setup() { createCanvas(640, 480); // gray background background(192); // magenta stroke of 5, green fill stroke(255, 0, 255); strokeWeight(5); fill(0, 255, 0); // top-left - default mode arc(0.25*width, 0.25*height, 200, 100, 0, 1.5*PI); // top-right - OPEN mode arc(0.75*width, 0.25*height, 200, 100, 0, 1.5*PI, OPEN); // bottom-left - CHORD mode arc(0.25*width, 0.75*height, 200, 100, 0, 1.5*PI, CHORD); // bottom-right - PIE mode arc(0.75*width, 0.75*height, 200, 100, 0, 1.5*PI, PIE); }
Output:
Example 7 - Line
// Example 7 - Line function setup() { createCanvas(640, 480); // gray background background(128); // magenta stroke of 5 stroke(255, 0, 255); strokeWeight(5); // 2 horizontal lines line(10, 10, width-10, 10); line(10, height-10, width-10, height-10); // 3 vertical lines line(10, 10, 10, height-10); line(width/2, 10, width/2, height-10); line(width-10, 10, width-10, height-10); }
Output:
Example 6 - Ellipse
// Example 6 - Ellipse function setup() { createCanvas(640, 480); // yellow background background(255, 255, 0); // no stroke, fill blue noStroke(); fill(0, 0, 255); // ellipse ellipse(width/2, height/2, 100, 50); }
Output:
Thursday, June 29, 2017
Example 5 - Color and noFill
// Example 5 - Color and noFill function setup() { var border = 100; createCanvas(640, 480); // red = 255, no green, no blue background(255, 0, 0); // red = green = blue = 64 (gray) stroke(64); strokeWeight(8); noFill(); // rect border from the sides rect(border, border, width - 2 * border, height - 2 * border); }
Output:
Subscribe to:
Posts (Atom)