P5.js createCapture失败回调
P5.js createCapture failure callback
p5.js是否有回调函数'createCapture函数失败? (即当用户权限被拒绝或用户浏览器不支持摄像机流时)。
我注意到在 src 中有一个 success callback,但似乎找不到一个失败。
在浏览器控制台,p5也报‘DOMException: Permission denied’,不过,我想以更人性化的方式处理这个问题。
如果没有回调,使用 createCapture 处理媒体故障的最佳做法是什么,因为文档中似乎没有讨论它。
我相信您可以使用 try
和 catch
来检测何时出现错误。像这样:
try{
capture = createCapture(VIDEO);
}
catch(error){
// error handling here
}
好的,所以这个答案晚了一年多,但发布这个是因为它可能对其他陷入同一问题的人有用。而不是按照下面评论中的建议对捕获本身进行错误测试,或者可能重新处理 createCapture() (如果您认为应该更改,最好通过打开问题请求来完成)我建议测试捕获的像素数组,并且仅当它已被设置时然后继续执行您的脚本所做的任何事情。这可以像这样简单地完成:
//load pixel data of webcam capture
cap.loadPixels();
//all values in the pixel array start as zero so just test if they are
//greater than zero
if (cap.pixels[1] > 0)
{
//put the rest of your script here
}
下面是一个完整的例子:
var canvas;
var cap;
var xpos = 0;
function setup()
{
canvas = createCanvas(windowWidth, windowHeight);
canvas.style("display", "block");
background("#666666");
//create an instance of the webcam
cap = createCapture(VIDEO);
cap.size(640, 480);
cap.hide();
}
function draw()
{
//load pixel data of webcam capture
cap.loadPixels();
//if the first pixel's R value is set continue with the rest of the script
if (cap.pixels[1] > 0)
{
var w = cap.width;
var h = cap.height;
copy(cap, w/2, 0, 10, h, xpos, (height / 2) - (h / 2), 10, h);
xpos = xpos + 1;
if (xpos > width) xpos = 0;
}
}
p5.js是否有回调函数'createCapture函数失败? (即当用户权限被拒绝或用户浏览器不支持摄像机流时)。
我注意到在 src 中有一个 success callback,但似乎找不到一个失败。 在浏览器控制台,p5也报‘DOMException: Permission denied’,不过,我想以更人性化的方式处理这个问题。
如果没有回调,使用 createCapture 处理媒体故障的最佳做法是什么,因为文档中似乎没有讨论它。
我相信您可以使用 try
和 catch
来检测何时出现错误。像这样:
try{
capture = createCapture(VIDEO);
}
catch(error){
// error handling here
}
好的,所以这个答案晚了一年多,但发布这个是因为它可能对其他陷入同一问题的人有用。而不是按照下面评论中的建议对捕获本身进行错误测试,或者可能重新处理 createCapture() (如果您认为应该更改,最好通过打开问题请求来完成)我建议测试捕获的像素数组,并且仅当它已被设置时然后继续执行您的脚本所做的任何事情。这可以像这样简单地完成:
//load pixel data of webcam capture
cap.loadPixels();
//all values in the pixel array start as zero so just test if they are
//greater than zero
if (cap.pixels[1] > 0)
{
//put the rest of your script here
}
下面是一个完整的例子:
var canvas;
var cap;
var xpos = 0;
function setup()
{
canvas = createCanvas(windowWidth, windowHeight);
canvas.style("display", "block");
background("#666666");
//create an instance of the webcam
cap = createCapture(VIDEO);
cap.size(640, 480);
cap.hide();
}
function draw()
{
//load pixel data of webcam capture
cap.loadPixels();
//if the first pixel's R value is set continue with the rest of the script
if (cap.pixels[1] > 0)
{
var w = cap.width;
var h = cap.height;
copy(cap, w/2, 0, 10, h, xpos, (height / 2) - (h / 2), 10, h);
xpos = xpos + 1;
if (xpos > width) xpos = 0;
}
}