Submodule 18.2: Video in p5.js
Submodule 18.2: Video in p5.js
- Insert video
- play with video
- createCapture()
createCapture()
The function createCapture()
enables p5.js to connect to the default camera of your computer and thus display the result in the screen.
Inside this function, we should specify the type of media. In this case, this is video, thus we write createCapture(VIDEO)
If we want to display the video on the canvas we use the image()
function.
In order to hide the video created outside the canvas element we use nameofvideo.hide();
.
Example
Here is an example code where p5.js uses your webcam:
let camVideo;
function setup(){
createCanvas(600,800);
// make the connection to the webcam
camVideo = createCapture('VIDEO');
//set background color
background(244,98,44);
}
function draw(){
}
You can see the result of the above code here
Exercise
- Open your Visual Studio editor and the
p5yourName
folder. - Open the file
ex812.js
in your editor and save it asex1023.js
- Open the file
ex812.html
in your editor and save it asex1023.html
- In the
ex1023.html
file, update the link toex1023.js
from exersice812.js - Go to the
index.html
file and create, underModule 10
, alink
to theex1023.html
file with the title "createCapture()".
Modify the ex1023.js
file and use as a base the above example and the theory in order to hide the initial video that is outside the canvas and display the video only inside the canvas. You can see here an example.
let camVideo;
function setup(){
createCanvas(600,600);
// make the connection to the webcam
camVideo = createCapture('VIDEO');
background(244,98,44);
//hide initial video
camVideo.hide();
}
function draw(){
// display video on the canvas
image(camVideo,0,0);
}
Do a Git commit with the message "createCapture()".
- See more about the createCapture() function