-
Notifications
You must be signed in to change notification settings - Fork 53
/
part17f_motion_detection.html
70 lines (51 loc) · 1.64 KB
/
part17f_motion_detection.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<!DOCTYPE html>
<html>
<title>Creative Coding Yea!</title>
<head>
<meta charset="utf-8">
<script language="javascript" src="js/creative_coding.js"></script>
<script language="javascript" src="js/canvas.js"></script>
<script language="javascript" src="js/video.js"></script>
<script language="javascript" src="js/quicksettings.min.js"></script>
<link rel="stylesheet" href="css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen" />
</head>
<body>
<script>
var ctx = createCanvas("canvas1");
var ctx2 = createHiddenCanvas("canvas2");
var sample_size = 20;
var old = [];
var threshold = 40;
var settings = QuickSettings.create();
//settings.setGlobalChangeHandler(draw);
settings.addRange("sample_size", 5, 200, sample_size, 1, function(value) {
sample_size = value;
});
settings.addRange("threshold", 5, 100, threshold, 1, function(value) {
threshold = value;
});
function draw(){
ctx2.drawImage(video, 0, 0, w, h);
var data = ctx2.getImageData(0, 0, w, h).data;
ctx.background(0, 0.05);
for (var y = 0; y < h; y+= sample_size) {
for (var x = 0; x < w; x+= sample_size) {
var pos = (x + y * w) * 4;
var r = data[pos];
var g = data[pos+1];
var b = data[pos+2];
if(old[pos] && Math.abs(old[pos].red - r) > threshold) {
var bright = brightness(r, g, b, sample_size);
ctx.fillStyle = rgb(r, g, b);
ctx.fillEllipse(x, y, bright, bright);
// ctx.fillStyle = rgb(0);
// ctx.fillEllipse(x, y, sample_size/2, sample_size/2);
}
old[pos] = { red: r, green: g, blue: b};
}
}
}
</script>
</body>
</html>