/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
/* *Utilbaseclasstohelptestacapturedcanvaselement.Initializesthe *outputcanvas(usedfortestingthecolorofvideoelements),andoptionally *overridesthedefault`createAndAppendElement`element|width|and|height|.
*/ function CaptureStreamTestHelper(width, height) { if (width) { this.elemWidth = width;
} if (height) { this.elemHeight = height;
}
/* cout is used for `getPixel`; only needs to be big enough for one pixel */ this.cout = document.createElement("canvas"); this.cout.width = 1; this.cout.height = 1;
}
/* Request a frame from the stream played by |video|. */
requestFrame(video) {
info("Requesting frame from " + video.id);
video.srcObject.requestFrame();
},
/* *Returnsthepixelat(|offsetX|,|offsetY|)(fromtopleftcorner)of *|video|asanarrayofthepixel'scolorchannels:[R,G,B,A].
*/
getPixel(video, offsetX = 0, offsetY = 0) { // Avoids old values in case of a transparent image.
CaptureStreamTestHelper2D.prototype.clear.call(this, this.cout);
var ctxout = this.cout.getContext("2d");
ctxout.drawImage(
video,
offsetX, // source x coordinate
offsetY, // source y coordinate 1, // source width 1, // source height 0, // destination x coordinate 0, // destination y coordinate 1, // destination width 1
); // destination height return ctxout.getImageData(0, 0, 1, 1).data;
},
/* *Returnsapromisethatresolvesafter|time|msofplaybackorwhenthe *topleftpixelof|video|becomes|refColor|.Thetestisfailedifthe *timeisnotreached,orifthecancelpromiseresolves.
*/
async pixelMustNotBecome(
video,
refColor,
{ threshold = 0, time = 5000, infoString = "n/a" } = {}
) {
info( "Waiting for " +
video.id + " to time out after " +
time + "ms against [" +
refColor.data.join(",") + "] - " +
refColor.name
);
let timeout = new Promise(resolve => setTimeout(resolve, time));
let analysis = async () => {
await this.waitForPixel(
video,
px => this.isPixel(px, refColor, threshold),
{
offsetX: 0,
offsetY: 0,
width: 0,
height: 0,
}
); thrownew Error("Got color " + refColor.name + ". " + infoString);
};
await Promise.race([timeout, analysis()]);
ok(true, video.id + " " + infoString);
},
/* Create an element of type |type| with id |id| and append it to the body. */
createAndAppendElement(type, id) { var e = document.createElement(type);
e.id = id;
e.width = this.elemWidth;
e.height = this.elemHeight; if (type === "video") {
e.autoplay = true;
}
document.body.appendChild(e); return e;
},
};
/* Sub class holding 2D-Canvas specific helpers. */ function CaptureStreamTestHelper2D(width, height) {
CaptureStreamTestHelper.call(this, width, height);
}
/* Clear all drawn content on |canvas|. */
CaptureStreamTestHelper2D.prototype.clear = function (canvas) { var ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
};
/* Draw the color |color| to the source canvas |canvas|. Format [R,G,B,A]. */
CaptureStreamTestHelper2D.prototype.drawColor = function (
canvas,
color,
{
offsetX = 0,
offsetY = 0,
width = canvas.width / 2,
height = canvas.height / 2,
} = {}
) { var ctx = canvas.getContext("2d"); var rgba = color.data.slice(); // Copy to not overwrite the original array
rgba[3] = rgba[3] / 255.0; // Convert opacity to double in range [0,1]
info("Drawing color " + rgba.join(","));
ctx.fillStyle = "rgba(" + rgba.join(",") + ")";
// Only fill top left corner to test that output is not flipped or rotated.
ctx.fillRect(offsetX, offsetY, width, height);
};
/* Test that the given 2d canvas is NOT origin-clean. */
CaptureStreamTestHelper2D.prototype.testNotClean = function (canvas) { var ctx = canvas.getContext("2d"); var error = "OK"; try { var data = ctx.getImageData(0, 0, 1, 1);
} catch (e) {
error = e.name;
}
is(
error, "SecurityError", "Canvas '" + canvas.id + "' should not be origin-clean"
);
};
/* Sub class holding WebGL specific helpers. */ function CaptureStreamTestHelperWebGL(width, height) {
CaptureStreamTestHelper.call(this, width, height);
}
/* Set the (uniform) color location for future draw calls. */
CaptureStreamTestHelperWebGL.prototype.setFragmentColorLocation = function (
colorLocation
) { this.colorLocation = colorLocation;
};
/* Clear the given WebGL context with |color|. */
CaptureStreamTestHelperWebGL.prototype.clearColor = function (canvas, color) {
info("WebGL: clearColor(" + color.name + ")"); var gl = canvas.getContext("webgl"); var conv = color.data.map(i => i / 255.0);
gl.clearColor(conv[0], conv[1], conv[2], conv[3]);
gl.clear(gl.COLOR_BUFFER_BIT);
};
/* Set an already setFragmentColorLocation() to |color| and drawArrays() */
CaptureStreamTestHelperWebGL.prototype.drawColor = function (canvas, color) {
info("WebGL: drawArrays(" + color.name + ")"); var gl = canvas.getContext("webgl"); var conv = color.data.map(i => i / 255.0);
gl.uniform4f(this.colorLocation, conv[0], conv[1], conv[2], conv[3]);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
};
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet am 2026-06-09)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.