WEEK 14 - P5.JS: Wen-Bin Jian Department of Electrophysics, National Chiao Tung University
WEEK 14 - P5.JS: Wen-Bin Jian Department of Electrophysics, National Chiao Tung University
JS
WEN-BIN JIAN
DEPARTMENT OF ELECTROPHYSICS, NATIONAL CHIAO TUNG UNIVERSITY
OUTLINE
• Features:
• Free to download and open source
• Interactive programs with 2D, 3D or PDF output
• OpenGL integration for accelerated 3D
• For GNU/Linux, Mac OS X, and Windows
• Over 100 libraries extend the core software
• Well documented, with many books available
• Processing is a software for learning object-oriented programming.
Ref1:
INSTALL P5.JS
• Download p5.zip from https://p5js.org/download/support.html
• Files:
• p5.js – full uncompressed version
• p5.min.js – compressed version
• addons\p5.dom.js & addons\p5.sound.js
IC_W1401.html
• In the HTMl file var x=0, y=0, vx=2, vy=4;
• <style> body{ padding:0; margin:0; } </style> function setup(){
createCanvas(800,600);
• <script src="p5/p5.js"></script> <script src="sketch1.js"></script> }
• In the sketch1.js file function draw(){
rect(0,0,799,599);
• function setup(){ rect(x,y,50,50);
• createCanvas(800,600); // prepare a canvas with a size of 800 x 600 pixels x+=vx;
y+=vy;
• background(255); // fill the canvas with white color, used to clear screen if(x<=0 || x>=750) vx=-vx;
• rect(0,0,799,599); // draw the boundary rectangle of the canvas} if(y<=0 || y>=550) vy=-vy;
}
• function draw(){} // this is a redraw function that updates 60 times in a second
START UP A P5 SKETCH IN A CONTAINER(DIV)
• p5 is an object that shows a canvas using for drawing 2D and 3D images, IC_W1402.html
and for animation. <html>
<head>
<script src="p5/p5.js"></script>
• Load the p5 library in the head of your html file: </head>
<body>
• <script src="p5/p5.js"></script>
<div id="p5_container"></div>
• You can start to overwrite the functions of the p5 object.
<script>
• The first step is to rewrite the “setup” function. function setup()
{
• Use createCanvas(width, height) to create the element of p5 canvas. var cvs = createCanvas(640, 480);
cvs.parent("p5_container");
background(225, 225, 225);
• Put the html element inside a container such as a div element. }
</script>
</body>
</html>
THE COORDINATE OF THE CANVAS
• createCanvas(800,600); 800
• The size of the canvas is: width: 800 pixels, height 600 pixels. (0,0)
• One pixel is one physical point, a dot, in the picture. 600
• The most upper left position is x=0 and y=0 (pixel). (799,599)
• The most lower right position is x=799 and y=599 (pixel).
• Color: either black white (BW) or full colors with red, green, and blue (RGB).
IC_W1403.html
• background(0) - set the full canvas to black color; background(255) set to white
• stroke(BW) or stroke(R, G, B) to set the color of the drawing pen
• clear() – clear all pixels, make canvas transparent, like background() & rect()
IC_W1404.html
• background(BW) – use black/white level color to brush the canvas
• background(R, G, B) – clear the canvas and paint all with the RGB color
• rect(x,y,w,h) – if you set fill(255), the canvas will be painted in white color
• A pen is used to draw point & boundary of shapes.
• noStroke() – no drawing pens
• strokeWeight(px) – thickness of the drawing pen
• stroke(BW), stroke(R, G, B) – set the color of the drawing pen,
OUTLINE
𝑥 𝑥
𝑥
𝑥 𝑥
translate rotate 𝑦 scale
𝑦 𝑦
𝑦
𝑦
TRANSFORM – SHEAR, MATRIX
DRAW IMAGES
1. Now you can write a game of projectile motion. Prepare some images or drawing on
the p5 canvas and a fire button on html window. The direction of the projectile is
changing from 30 to 90 degree. The player can press the button to fire the projectile to
the right. There will be some gifts on the right. When the gifts are hit by the projectile,
the player can win something that is shown on the p5 canvas.