!7 cmodule WebCamDemo > DynPrintLog { !include #1029545 // API for Eleu O html(IWebRequest request) { //ret "URI: " + request.uri(); ret hsansserif() + hfullcenter(p("WebCam Preview") + hfulltag video("", autoplay := true, id := "webcamPreview", style := "width: 600px; height: 400px; border: 5px solid red;") + p("Snapshot " + hbutton("Grab", onclick := "snapshot()")) + hfulltag canvas("", id := "snapshotCanvas", width := 300, height := 200, style := "border: 3px solid green;") ) + hscript([[ var video = document.querySelector("#webcamPreview"); if (navigator.mediaDevices.getUserMedia) { navigator.mediaDevices.getUserMedia({ video: true }) .then(function (stream) { video.srcObject = stream; getVideoSize(); }) .catch(function (err0r) { console.log("Something went wrong: " + err0r); }); }; async function getVideoSize() { await new Promise(resolve => video.onloadedmetadata = resolve); console.log(`video size: ${video.videoWidth}x${video.videoHeight}`); } var snapshotW, snapshotH; function snapshot() { var canvas = document.getElementById("snapshotCanvas"); var w = video.videoWidth, h = video.videoHeight; var w2 = canvas.width, h2 = canvas.height; if (w/h > w2/h2) h2 = h*w2/w; else w2 = w*h2/h; snapshotW = w2; snapshotH = h2; canvas.getContext("2d").drawImage(video, 0, 0, w2, h2); avgBrightness(); } function avgBrightness() { var imageData = canvas.getContext("2d").getImageData(0, 0, snapshotW, snapshotH); var pixels = imageData.data; for (var i = 0, il = 10/*pixels.length*/; i < il; i++) { console.log(pixels[i]); } } ]]); } }