playgrdstar
creative coding space
2 min readSep 9, 2018

--

Using CCapture.js library with p5.js and three.js

A screen capture app could be used to capture visualisations in a browser.

But we may wish to either have it programmatically captured sometimes by using code. There are a number of options but the CCapture.js library works well, and provides for a number of output options.

There is also another advantage — using a screen capture app means that any stuttering on the screen would also be captured. Using CCapture.js means that we are actually capturing each frame generated by our p5.js or three.js code directly, sans the stuttering.

It’s slightly easier in p5.js than three.js.

In p5.js, we just have to instantiate the Capture instance.

var capturer = new CCapture({
format: 'jpg',
name: 'name'
});
var canvas;

Assign a canvas variable to whatever we are generating in the setup function.

var p5Canvas = createCanvas(800, 800);
canvas = p5Canvas.canvas;

And ask the capturer instance to capture it.

capturer.capture(canvas);

A more complete gist is available here.

And now in three.js.

We similarly declare an instance of it first. This is identical to what we did above, just with more options.

var recorder = new CCapture({
verbose: false,
display: true,
framerate: 30,
quality: 100,
format: 'jpg',
// workersPath: './libs/',
timeLimit: 0,
frameLimit: 0,
autoSaveTime: 0
});

Instead of capturing the canvas, we have to capture the renderer DOM element. This is the same DOM element that we usually append to the HTML page.

renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );

...

recorder.capture(renderer.domElement);

A more complete gist is available here.

--

--

playgrdstar
creative coding space

ming // gary ang // illustration, coding, writing // portfolio >> playgrd.com | writings >> quaintitative.com