Example 1
1 2 3 4 5 6 7 8 9 10 11 12 |
<html> <head> <meta charset="UTF-8"> <title>p5.js vers 0.5.2, Edit index.html to Change This Title</title> <script src="http://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/p5.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.dom.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.sound.js"></script> <script src="ex1.js" type="text/javascript"></script> </head> <body> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// ex1.js - put canvas in a variable and move it around (load ex1.html) var myCanvas; function setup() { myCanvas = createCanvas(80, 80); } function draw() { background(255); myCanvas.position(noise(millis() / 2000) * 200, noise(10 + millis() / 2000) * 200); fill("red"); rect(0, 0, 50, 50); } |
Example 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<html> <head> <meta charset="UTF-8"> <title>p5.js vers 0.5.2, Edit index.html to Change This Title</title> <script src="http://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/p5.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.dom.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.sound.js"></script> <script src="ex2.js" type="text/javascript"></script> </head> <body> Mankind has always been fascinated by mechanical simulations of human activity. A characteristic of Western thought is to understand our minds, bodies, and our world in mechanistic terms. Music especially has been viewed as a mathematical and mechanical process. As a result, musicians, scientists, and engineers have devised all sorts of mechanisms and systems for composing and performing music. I will review some or these activities their evolution over the last 2000 years, ending with some recent findings on how a human audience relates to humanoid robot performers and how music can be composed by computers. </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// ex2.js - rotating box in a canvas over HTML text (load ex2.html) var myCanvas; function setup() { myCanvas = createCanvas(80, 80); } function draw() { background(255); myCanvas.position(noise(millis() / 2000) * 200, noise(10 + millis() / 2000) * 200); fill("red"); translate(40, 40); rotate(millis() / 1000); rect(-25, -25, 50, 50); pop(); } |
Example 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<html> <head> <meta charset="UTF-8"> <title>p5.js vers 0.5.2, Edit index.html to Change This Title</title> <script src="http://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/p5.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.dom.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.sound.js"></script> <script src="ex3.js" type="text/javascript"></script> </head> <body> Mankind has always been fascinated by mechanical simulations of human activity. A characteristic of Western thought is to understand our minds, bodies, and our world in mechanistic terms. Music especially has been viewed as a mathematical and mechanical process. As a result, musicians, scientists, and engineers have devised all sorts of mechanisms and systems for composing and performing music. I will review some or these activities their evolution over the last 2000 years, ending with some recent findings on how a human audience relates to humanoid robot performers and how music can be composed by computers. <br><br> <div align="center" id="aPlaceForCanvas"></div> <ol> <li>Music was considered by Pythagoras to be a branch of mathematics closely related to Arithmetic</li> <li>Described by Plato in The Republic as the four arts: Arithmetic, Music, Geometry, Astronomy</li> <li>Later formalized as the Quadrivium</li> <li>Music theory has always had a musical aspect</li> <li>Today, music composing, printing, recording, synthesizers … all based on digital information processing</li> </ol> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// ex3.js - a little animated banner (load ex3.html) // Note: a little animated gif would have much less baggage var myCanvas; function setup() { myCanvas = createCanvas(400, 10); myCanvas.parent('aPlaceForCanvas'); } function draw() { background(255); stroke("red"); strokeWeight(2); var offset = int(millis() / 100) % 15; for (var i = -15; i < width; i = i + 15) { line(i + offset, 0, i + height + offset, height); } } |
Example 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<html> <head> <meta charset="UTF-8"> <title>p5.js vers 0.5.2, Edit index.html to Change This Title</title> <script src="http://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/p5.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.dom.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.sound.js"></script> <script src="ex4.js" type="text/javascript"></script> </head> <body> Mankind has always been fascinated by mechanical simulations of human activity. A characteristic of Western thought is to understand our minds, bodies, and our world in mechanistic terms. Music especially has been viewed as a mathematical and mechanical process. As a result, musicians, scientists, and engineers have devised all sorts of mechanisms and systems for composing and performing music. I will review some or these activities their evolution over the last 2000 years, ending with some recent findings on how a human audience relates to humanoid robot performers and how music can be composed by computers. <br><br> <div align="center" id="aPlaceForCanvas"></div> <ol> <li>Music was considered by Pythagoras to be a branch of mathematics closely related to Arithmetic</li> <li>Described by Plato in The Republic as the four arts: Arithmetic, Music, Geometry, Astronomy</li> <li>Later formalized as the Quadrivium</li> <li>Music theory has always had a musical aspect</li> <li>Today, music composing, printing, recording, synthesizers … all based on digital information processing</li> </ol> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// ex4.js - mouse-tracking eyes (load ex4.html) var myCanvas; function setup() { myCanvas = createCanvas(200, 100); myCanvas.parent('aPlaceForCanvas'); } function draw() { background(255); for (i = 0; i < 2; i++) { // 2 eyes var ex = i * 100; // eye x offset fill(255); var x = 45 + ex; // eye center x var y = 50; // eye center y ellipse(x, y, 80, 60); var dx = mouseX - x; // dx, dy goes from eye to mouse var dy = mouseY - y; var d = dist(x, y, mouseX, mouseY); // distance to mouse d = max(30, d); // avoid divide by zero dx = dx / d; // now dx, dy goes from 0 to 1 in direction of mouse dy = dy / d; dx = dx * 25; // scale dx, dy roughly to size of eye dy = dy * 15; fill(0); // draw the pupil ellipse(x + dx, y + dy, 30, 30); } } |
Example 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<html> <head> <meta charset="UTF-8"> <title>p5.js vers 0.5.2, Edit index.html to Change This Title</title> <script src="http://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/p5.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.dom.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.sound.js"></script> <script src="ex5.js" type="text/javascript"></script> </head> <body> Mankind has always been fascinated by mechanical simulations of human activity. A characteristic of Western thought is to understand our minds, bodies, and our world in mechanistic terms. Music especially has been viewed as a mathematical and mechanical process. As a result, musicians, scientists, and engineers have devised all sorts of mechanisms and systems for composing and performing music. I will review some or these activities their evolution over the last 2000 years, ending with some recent findings on how a human audience relates to humanoid robot performers and how music can be composed by computers. <br><br> <div align="center" id="aPlaceForCanvas"></div> <ol> <li>Music was considered by Pythagoras to be a branch of mathematics closely related to Arithmetic</li> <li>Described by Plato in The Republic as the four arts: Arithmetic, Music, Geometry, Astronomy</li> <li>Later formalized as the Quadrivium</li> <li>Music theory has always had a musical aspect</li> <li>Today, music composing, printing, recording, synthesizers … all based on digital information processing</li> </ol> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// ex5.js - creating a DIV element (load ex5.html) var myCanvas; function setup() { myText = createDiv("<img src='http://fla.fg-a.com/aliens/flying-alien.gif'><b><h2>All your base are belong to us!</h2>"); } function draw() { myText.position(noise(millis() / 2000) * 400 - 200, noise(10 + millis() / 2000) * 600 - 200); } |
Example 6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<html> <head> <meta charset="UTF-8"> <title>p5.js vers 0.5.2, Edit index.html to Change This Title</title> <script src="http://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/p5.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.dom.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.sound.js"></script> <script src="ex6.js" type="text/javascript"></script> </head> <body> Mankind has always been fascinated by mechanical simulations of human activity. A characteristic of Western thought is to understand our minds, bodies, and our world in mechanistic terms. Music especially has been viewed as a mathematical and mechanical process. As a result, musicians, scientists, and engineers have devised all sorts of mechanisms and systems for composing and performing music. I will review some or these activities their evolution over the last 2000 years, ending with some recent findings on how a human audience relates to humanoid robot performers and how music can be composed by computers. <br><br> <div id="aPlaceForText"></div> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// ex6.js - inserting computed text into HTML (load "ex6.html") var myCanvas; function setup() { myText = createDiv("Some Text"); myText.parent("aPlaceForText"); myText.style("font-size", "18pt"); } function draw() { var s = "The mouse is at location " + int(mouseX) + ", " + int(mouseY); myText.elt.innerHTML = s; } |
Example 7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<html> <head> <meta charset="UTF-8"> <title>Example 7 - onmouseover</title> <script src="http://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/p5.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.dom.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.sound.js"></script> <script src="ex7.js" type="text/javascript"></script> </head> <body> <big> <big> <big> <!-- other events are onchange, onmouseover, onclick, onkeypress --> <b onmouseover="apple()">Apple</b> <b onmouseover="duck()">Duck</b> <b onmouseover="car()">Car</b> </big> </big> </big> <br> <div id="picture-here"></div> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
// ex6.js - inserting computed text into HTML (load "ex6.html") // NOTE: to run this code, you'll probably need a local server AND // you will need to provide images: apple.jpg, duck.jpg, car.jpg var myCanvas; var appleImg; var duckImg; var carImg; var showImg; function preload() { appleImg = loadImage("apple.jpg") duckImg = loadImage("duck.jpg") carImg = loadImage("car.jpg") } function setup() { myCanvas = createCanvas(300, 300); myCanvas.parent("picture-here"); showImg = false; noLoop(); } function draw() { background(255); } function apple() { background(255); image(appleImg, 0, 0); } function duck() { background(255); image(duckImg, 0, 0); } function car() { background(255); image(carImg, 0, 0); } |
Perlin Noise Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
// noise.js - noise study var myCanvas; var loc = 0; function setup() { createCanvas(500, 300); } function draw() { background(250); stroke(0); strokeWeight(0.5); // draw the first graph of Perlin noise // the parameter to the noise function is the X coordinate scaled // by a scale factor from 0-1, thus we're accessing noise() using // ranges from 0-0 (mouse all the way left) to 0-width (mouse all // the way right) for (i = 0; i < width; i++) { line(i, 30 + 100 * noise(i * mouseX / width), i + 1, 30 + 100 * noise((i + 1) * mouseX / width)); } // the second graph is the same Perlin noise, but it is shifted by 100 pixels // by accessing the noise() function with a large enough offset, we access // different "random" values that are seemingly independent. for (i = 0; i < width; i++) { line(i, 180 + 100 * noise((i + 100) * mouseX / width), i + 1, 180 + 100 * noise((i + 101) * mouseX / width)); } // draw a cursor to show where we are accessing the function loc = (loc + 1) % width; stroke("red"); line(loc, 0, loc, height); noStroke(); // move blue balls according to the noise value a the red cursor fill("blue"); ellipse(30, 30 + 100 * noise(loc * mouseX / width), 30, 30); ellipse(30, 180 + 100 * noise((loc + 100) * mouseX / width), 30, 30); fill(0); textSize(20); text("Scale factor: " + nf(mouseX / width, 1, 2), 10, 30); } |
2D Perlin Noise Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// noise2.js - noise study var myCanvas; function setup() { createCanvas(600, 600); } var SCALE = 0.01; function draw() { background(250); fill("blue"); for (i = 0; i < 600; i += 30) { for (j = 0; j < 600; j += 30) { var d = noise(i * SCALE, j * SCALE) * 50; ellipse(i, j, d, d); } } } |