Skip to content

prossel/WebGL-VideoRecorder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 

Repository files navigation

WEBGL VideoRecorder for p5.js

This repository hosts a JavaScript class to record a video from a p5.js sketch / canvas using WEBGL.

Note: The canvas must be created with the WEBGL parameter as in the Getting Started section below: createCanvas(400, 400, WEBGL);

Live demo

See the live demo page to test it.

Click the button to start, then stop recording and view or download the recorded video.

WebM video file format

The script will produce a WebM video file (*.webm).

It can be converted to the more popular MP4 file format with some video tools or online services such as https://cloudconvert.com/webm-to-mp4

Basic use

  • Add the library in the <head> section of index.html

    <script src="https://prossel.github.io/WebGL-VideoRecorder/js/VideoRecorder.js"></script>
  • Add a call to VideoRecorder.addButton(); in your setup() function.

See Getting started section below for more details.

Getting started

  1. Create a new sketch on https://editor.p5js.org/

  2. Replace the content of sketch.js with this code

    function setup() {
        createCanvas(400, 400, WEBGL);
    }
    
    function draw() {
        background(220);
        rotate(-frameCount * 0.01, createVector(1, 1, 0));
        box(200);
    }
  3. Note that the createCanvas function is using the WEBGL parameter.

  4. Click the play button to see the cube rotate

  5. Now we will add the video record feature

  6. Click the > arrow to see all files of the sketch

  7. Select index.html and add VideoRecorder line in the <head> section, after the p5.js scripts.

    <head>
        <script src="... /p5.js/ ... "></script>
        <script src="... /p5.js/ ... "></script>
    
        <script src="https://prossel.github.io/WebGL-VideoRecorder/js/VideoRecorder.js"></script>
    </head>
  8. Select sketch.js and add VideoRecorder.addButton(); in setup:

    function setup() {
        createCanvas(400, 400, WEBGL);
        VideoRecorder.addButton();
    }
  9. Click the play button. See the Record video button ?

  10. Click it, wait a few seconds and click again to stop.

  11. The recorded video is added at the bottom of the page with a download link.

  12. If the video does not have the same size as the canvas, see Double size video below

Final result: https://editor.p5js.org/prossel/sketches/HjKjai1gG

Advanced use

Control the timing

You can also start / stop the recording from the code. You only need to call VideoRecorder.record(); and VideoRecorder.stop(); when appropriate.

Live demo: https://editor.p5js.org/prossel/full/imTyCpxQT

For example to record the first 60 frames of an animation:

function setup() {
    
    // ...

    VideoRecorder.addButton();
}

function draw() {

    // ...

    // Start recording at frame 1
    if (frameCount == 1) {
        VideoRecorder.record();
    }

    // Stop video recording automatically after 60 frames
    if (frameCount == 60) {
        VideoRecorder.stop();
    }
}

Double size video

When using a high resolution display, such as Retina, the video may have more pixels than the canvas size.

When you want the video to have exactly the same size as the canvas, whatever the display resolution, the pixel density may be forced on the canvas using pixelDensity(1). The video will have the same size.

function setup() { 

    createCanvas(1920, 1080, WEBGL);
    pixelDensity(1);

}

High resolution video

The video has the size of the canvas or the double if using a high resolution screen, such as Retina display.

If you want a specific size, use is to create the canvas. If the canvas is then too big to fit in the window, you may want to scale it with this code with CSS.

To avoid a different size with high resolution display, use pixelDensity(1) to force video and canvas to have the same size.

Live demo: https://editor.p5js.org/prossel/full/ps04GUidy

function setup() { 

    createCanvas(1920, 1080, WEBGL);
    pixelDensity(1);

    // Reduce size of preview
    let scalePreview = 0.25;
    let cnv = document.getElementById('defaultCanvas0');
    cnv.style.width = round(width * scalePreview) + "px";
    cnv.style.height = round(height * scalePreview) + "px";

    VideoRecorder.addButton();
}