-
Notifications
You must be signed in to change notification settings - Fork 53
/
part17k_motion_detection_scaled.html
128 lines (98 loc) · 2.73 KB
/
part17k_motion_detection_scaled.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<!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 scalefactor = 40;
var settings = QuickSettings.create();
var particles = [];
ctx.background(0);
function setup(){
video.width = w/scalefactor;
video.height = h/scalefactor;
}
settings.addRange("threshold", 5, 100, threshold, 1, function(value) {
threshold = value;
});
function draw(){
ctx.background(0, 0.05);
var motion = motionDetection();
for (var i = 0; i < motion.length; i++) {
var m = motion[i];
addParticle(m);
}
moveParticles();
drawParticles();
}
function motionDetection(){
var motion = [];
// draw the video and get its pixels
ctx.drawImage(video, 0, 0, video.width, video.height);
var data = ctx.getImageData(0, 0, video.width, video.height).data;
// we can now loop over all the pixels of the video
for (var y = 0; y < video.height; y++) {
for (var x = 0; x < video.width; x++) {
var pos = (x + y * video.width) * 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) {
// push the x, y and rgb values into the motion array
// but multiply the x and y values bck up by scalefactor
// to get their actual screen position
motion.push({x: x * scalefactor, y: y * scalefactor, r: r, g: g, b: b});
}
old[pos] = { red: r, green: g, blue: b};
}
}
return motion;
}
function addParticle(m){
var particle = {
x: m.x + random(-sample_size/2, sample_size/2),
y: m.y,
me: particles.length,
speed_x: random(-1, 1),
speed_y: random(-6, 2),
sz: 10,
c: rgb(m.r, m.g, m.b),
}
particles.push(particle);
}
function moveParticles(){
for (var i = particles.length-1; i > 0; i--) {
var p = particles[i];
p.x += p.speed_x;
p.y += p.speed_y;
p.speed_y -= 0.2;
p.sz += 0.4;
if(p.x > w || p.x < 0 || p.y > w || p.y < 0) {
particles.splice(p.me, 1);
//i--;
};
}
}
function drawParticles(){
for (var i = 0; i < particles.length; i++) {
var p = particles[i];
ctx.fillStyle = p.c;
ctx.fillEllipse(p.x, p.y, p.sz, p.sz);
}
}
</script>
</body>
</html>