Permalink
Browse files

refactored with while loops instead of for loops, refactored desatura…

…te to include lightlevel check, added webcamSwiperOn boolean (obvious reasons), changed the eventemitting variable name(minor) for readibilty
  • Loading branch information...
1 parent 733a9ae commit 8f438c9c31e42738ef9d9ab5971bae640f4108be @alonisser alonisser committed Oct 6, 2012
Showing with 34 additions and 19 deletions.
  1. +34 −19 webcam-swiper-0.1.js
View
@@ -12,6 +12,7 @@ function initializeWebcamSwiper() {
navigator.getUserMedia({video: true}, function (stream) {
window.webcamSwiperStream = stream;
+ window.webcamSwiperOn = true;
// Create a video element and set its source to the stream from the webcam
var videoElement = document.createElement("video");
@@ -69,12 +70,13 @@ function initializeWebcamSwiper() {
greyscaleCtx.drawImage(videoElement, 0, 0, horizontalResolution, verticalResolution, 0, 0, canvasWidth, canvasHeight);
// Desaturate it
- currentImageData = deSaturate(greyscaleCtx.getImageData(0, 0, canvasWidth, canvasHeight));
+ var deSaturated = deSaturate(greyscaleCtx.getImageData(0, 0, canvasWidth, canvasHeight));
+ currentImageData = deSaturated[0];
// Make adjustments for light level and system speed
if (scanCount % 50 === 0) {
// Calibrate based on the current light level, if we haven't already
- lightLevel = getLightLevel(currentImageData);
+ lightLevel = deSaturated[1];
if (lightLevel > 0 && lightLevel <= 1) {
PIXEL_CHANGE_THRESHOLD = 25;
FRAME_THRESHOLD = 3000;
@@ -135,21 +137,25 @@ function initializeWebcamSwiper() {
}
function fireSwipeEvent(eventName) {
- var swipeLeftEvent = document.createEvent("UIEvents");
- swipeLeftEvent.initEvent(eventName, false, false);
- document.getElementsByTagName("body")[0].dispatchEvent(swipeLeftEvent);
+ var swipeEvent = document.createEvent("UIEvents");
+ swipeEvent.initEvent(eventName, false, false);
+ document.getElementsByTagName("body")[0].dispatchEvent(swipeEvent);
}
function getMotionWeight (previous, current) {
var motionWeight = 0;
var previousData = previous.data;
var currentData = current.data;
var dataLength = previousData.length;
- for (var i = 0; i < dataLength; i += 4) {
+ var i = 0;
+ while (i < dataLength) {
if (Math.abs(previousData[i] - currentData[i]) > PIXEL_CHANGE_THRESHOLD) {
motionWeight += ((i / 4) % canvasWidth) - (canvasWidth / 2);
+
}
+ i += 4;
}
+
return motionWeight;
}
@@ -161,30 +167,38 @@ function initializeWebcamSwiper() {
// Iterate through each pixel, desaturating it
var dataLength = theData.length;
- for (var i = 0; i < dataLength; i += 4) {
+ var i = 0;
+ var lightLevel;
+
+ while ( i < dataLength) {
// To find the desaturated value, average the brightness of the red, green, and blue values
var average = (theData[i] + theData[i + 1] + theData[i + 2]) / 3;
newData[i] = newData[i+1] = newData[i+2] = average;
// Fully opaque
newData[i+3] = 255;
+ lightLevel += newData[i]; //combining the light level in the samefunction
+ i += 4;
+
}
- return newImageData;
+ return [newImageData, lightLevel/dataLength];
}
// Will return the average intensity of all pixels. Used for calibrating sensitivity based on room light level.
- function getLightLevel (imageData) {
- var theData = imageData.data;
- var dataLength = theData.length;
-
- var value = 0;
- for (var i = 0; i < dataLength; i += 4) {
- value += theData[i];
- }
-
- return value / theData.length;
- }
+ // function getLightLevel (imageData) {
+ // var theData = imageData.data;
+ // var dataLength = theData.length;
+ // var value = 0;
+ // var i = 0;
+
+ // while (i < dataLength) {
+ // value += theData[i];
+ // i += 4
+ // }
+
+ // return value / dataLength;
+ // }
});
});
}
@@ -198,4 +212,5 @@ function destroyWebcamSwiper() {
window.webcamSwiperStream.stop();
window.webcamSwiperStream = undefined;
}
+ window.webcamSwiperOn = false;
}

0 comments on commit 8f438c9

Please sign in to comment.