JSFiddle - React, Tailwind, and code Playground
HTML
<H2>
Show Difference
</H2>
This page uses the camera to show differences over time and
summarize those changes as being in the left, right, or top
center of the area.
<BR>
Press Video to start using
the camera. It needs to be able to support 180x120 format.
Press Capture or click the video to display the difference,
or press Start to display one difference per second. Press
Stop to stop.
<P>
<input type="button" id="capture-button" value="Video">
<br><video autoplay id="my-video"></video>
<br><input type="button" id="screenshot-button" value="Capture">
<input type="button" id="start-button" value="Start">
<input type="button" id="stop-button" value="Stop">
<br><span id="results"></span>
<br><img id="my-image" src="">
<canvas style="display:none;"></canvas>
<script>
const thresh = 50
function byId(id) {return document.getElementById(id)}
const captureVideoButton = byId("capture-button")
const screenshotButton = byId("screenshot-button")
const startButton = byId("start-button")
const stopButton = byId("stop-button")
const img = byId('my-image');
const video = byId('my-video');
var interval
var saved
const canvas = document.createElement('canvas')
const constraints = { video: {width: {exact: 90}, height: {exact: 60} } }
captureVideoButton.onclick = function() {
navigator.mediaDevices.getUserMedia(constraints).
then((stream) => {video.srcObject = stream})
}
function arraySum(data) {
//https://jsperf.com/array-reduce-vs-foreach/2
let total = 0
for (let i=0, n=data.length; i<n; i++) {
total += data[i]
}
return total
}
screenshotButton.onclick = video.onclick = function() {
let w = canvas.width = video.videoWidth;
let h = canvas.height = video.videoHeight;
let leftwidth=Math.floor(w/3)
let rightwidth=Math.floor(w/3)
let centerwidth=w - leftwidth - rightwidth
let ctx = canvas.getContext('2d')
ctx.drawImage(video, 0, 0)
// Other browsers will fall back to image/png
if (!saved) saved = ctx.getImageData(0, 0, w, h)
let...