Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<video id="camStream"></video>
<canvas id="canvas"></canvas>
<canvas id="canvasFinal"></canvas>
<button id="btnInit">Init</button> <button id="btnStop">Stop</button>
<script type="text/javascript" src="js/MotionDetector.js"></script>
</body>
</html>
7 changes: 4 additions & 3 deletions js/MotionDetector-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 30 additions & 10 deletions js/MotionDetector.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*********************************************************************
* #### JS Motion Visualiser ####
* Coded by Jason Mayes. www.jasonmayes.com
* Please keep this disclaimer with my code if you use it anywhere.
* Please keep this disclaimer with my code if you use it anywhere.
* Thanks. :-)
* Got feedback or questions, ask here:
* Github: https://github.com/jasonmayes/JS-Motion-Detection/
Expand Down Expand Up @@ -29,16 +29,17 @@ var MotionDetector = (function() {
var localStream = null;
var imgData = null;
var imgDataPrev = [];
var timing = null;



function success(stream) {
localStream = stream;
// Create a new object URL to use as the video's source.
video.src = (window.URL && window.URL.createObjectURL(stream)) || stream;
video.play();
}


function handleError(error) {
console.error(error);
}
Expand Down Expand Up @@ -79,25 +80,44 @@ var MotionDetector = (function() {
imgData.data[x + 2] = blended;
imgData.data[x + 3] = 255;
}
x += 4;
x += 4;
}
ctxFinal.putImageData(imgData, 0, 0);
}
}


function init_() {
if (navigator.getUserMedia) {
if (navigator.getUserMedia) {
navigator.getUserMedia({video:true}, success, handleError);
} else {
} else {
console.error('Your browser does not support getUserMedia');
}
window.setInterval(snapshot, 32);
timing = window.setInterval(snapshot, 32);
}

function stop_() {
clearInterval(timing);
localStream.getTracks().forEach( (track) => {
track.stop();
});
}

return {
init: init_
init: init_,
stop: stop_
};
})();

MotionDetector.init();
(function registerClickHandler () {
// Implement the click handler here for button of class 'remove'
var btnStop = document.getElementById('btnStop');
btnStop.onclick = function(e) {
MotionDetector.stop();
}

var btnInit = document.getElementById('btnInit');
btnInit.onclick = function(e) {
MotionDetector.init();
}
})();