Now in 2019, for reasons of some update of P5.JS or browsers, the video.size function (number1, number2);
Does not work, breaking or code.
Here is the error:
undefined
p5.js: 61127 Uncaught IndexSizeError: Failed to execute ‘getImageData’ on ‘CanvasRenderingContext2D’: The source width is 0.
p5.js: 68816 Uncaught TypeError: Cannot assign to read only property ‘videoWidth’ of object '# ’
about: srcdoc: 36 Uncaught TypeError: Cannot read property ‘split’ of undefined
I am using the P5.JS editor.
Here is the same code, if uncommenting video.size will not work, giving the errors mentioned. You can also test on my own algorithm in the P5.JS editor: https://editor.p5js.org/willianxz/full/6Jeo9ogMk
var video;
var x = 0;
function setup () {
createCanvas (500, 400);
pixelDensity (1);
video = createCapture (VIDEO); //video.size(400,400);
background (51);
}
function draw () {
video.loadPixels ();
var w = video.width;
var h = video.height;
copy (video, w / 2, 0, 1, h, x, 0, 1, h);
x ++;
if (x> width) {x = 0;}
}
This is a way around the problem, but there are algorithms made where you need to use this video.size function.
To solve this question, I found a way around it better, it does not solve the problem that is actually a bug, but with this solution we can at least use this function.
As I understand it, somehow at the time of setup the video is not loaded, so I tried to load the video in preload function to finally use the video.size (size1, size2) function; However, this also does not work.
So I ran into a callback function technique and applied it to that question, so it was resolved.
function setup () {
createCanvas (640, 480);
background (0);
pixelDensity (1);
// Put the callback function when creating the video.
video = createCapture (VIDEO, callbackgo);
}
// It is only possible to change the size of the video when it is ready, so there is this callback function.
var go = false;
function callbackgo () {
go = true;
}
function draw () {
background (0);
// When the video is prepared, finally call the video.size function
if (go) {
video.size (320, 240);
}
// continue with your codes
//You can see an algorithm I made running like this:
//https://editor.p5js.org/willianxz/full/o5N5KY3fm
//Greetings from Brazil!
}