-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLightboard.js
More file actions
147 lines (143 loc) · 4.44 KB
/
Lightboard.js
File metadata and controls
147 lines (143 loc) · 4.44 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var Lightboard = function() {
var width = Lightboard.VIDEO_WIDTH;
var height = Lightboard.VIDEO_HEIGHT;
var video = document.createElement("video");
var canvas = document.getElementById("video_canvas");
canvas.width = width;
canvas.height = height;
var context = canvas.getContext("2d");
context.translate(width, 0);
context.scale(-1, 1);
var lightboard = document.getElementById("lightboard");
var lightboard_context = lightboard.getContext("2d");
var cursor = document.getElementById("cursor");
var cursor_context = cursor.getContext("2d");
var me = this;
var videoLoop = function() {
context.drawImage(video, 0, 0, width, height);
var point = me.findCursor(
context.getImageData(0, 0, width, height).data);
cursor_context.clearRect(0, 0, width, height);
if (point) {
cursor_context.beginPath();
cursor_context.arc(point.x, point.y, me.size, 0, 2*Math.PI);
cursor_context.closePath();
cursor_context.fillStyle = "#aaa";
cursor_context.fill();
lightboard_context.lineWidth = 2*me.size;
if (me.mode === "draw") {
lightboard_context.strokeStyle = me.color;
if (me.last_point) {
lightboard_context.lineTo(point.x, point.y);
lightboard_context.stroke();
} else {
lightboard_context.beginPath();
lightboard_context.moveTo(point.x, point.y);
}
me.last_point = point;
} else if (me.mode === "erase") {
lightboard_context.strokeStyle = "#fff";
if (me.last_point) {
lightboard_context.lineTo(point.x, point.y);
lightboard_context.stroke();
} else {
lightboard_context.beginPath();
lightboard_context.moveTo(point.x, point.y);
}
me.last_point = point;
} else {
me.last_point = null;
}
}
setTimeout(videoLoop, 30);
};
getUserMedia({
video: true,
audio: false
}, function(stream) {
video.src = window.URL.createObjectURL(stream);
video.play();
videoLoop();
}, function(error) {
console.warn("getUserMedia failed: " + error);
});
};
Lightboard.VIDEO_WIDTH = 640;
Lightboard.VIDEO_HEIGHT = 480;
Lightboard.BLOCK_WIDTH = 2;
Lightboard.BLOCK_HEIGHT = 2;
Lightboard.BRIGHTNESS_THRESHOLD = 252;
Lightboard.LIGHT_SIZE = 300;
/**
* @returns point
* 0 <= point.x < 640
* 0 <= point.y < 480
*/
Lightboard.prototype.findCursor = function(img_data) {
var num_lines = Lightboard.VIDEO_HEIGHT / Lightboard.BLOCK_HEIGHT >>> 0;
var num_cols = Lightboard.VIDEO_WIDTH / Lightboard.BLOCK_WIDTH >>> 0;
var i, j, x, y;
var B = [], V = [];
for (i = 0; i < num_lines; ++i) {
B.push([]), V.push([]);
for (j = 0; j < num_cols; ++j) {
V[i].push(false);
var brightness_sum = 0;
for (y = i*Lightboard.BLOCK_HEIGHT;
y < (i+1)*Lightboard.BLOCK_HEIGHT; ++y) {
for (x = j*Lightboard.BLOCK_WIDTH;
x < (j+1)*Lightboard.BLOCK_WIDTH; ++x) {
var pixel_idx = (y*Lightboard.VIDEO_WIDTH + x)*4;
var r = img_data[pixel_idx];
var g = img_data[pixel_idx+1];
var b = img_data[pixel_idx+2];
brightness_sum += (3*r+4*g+b) >>> 3;
}
}
B[i][j] = brightness_sum /
(Lightboard.BLOCK_HEIGHT*Lightboard.BLOCK_WIDTH) >>> 0;
}
}
var max_size = 0;
var point = null;
for (i = 0; i < num_lines; ++i) {
for (j = 0; j < num_cols; ++j) {
var block_size = 0;
var min_x = j, max_x = j, min_y = i, max_y = i;
var search_stack = [{y: i, x: j}];
while (search_stack.length) {
var pos = search_stack.pop();
x = pos.x;
y = pos.y;
if (x >= num_cols || y >= num_lines ||
x < 0 || y < 0 || V[y][x] ||
B[y][x] <= Lightboard.BRIGHTNESS_THRESHOLD) {
continue;
}
if (x > max_x) max_x = x;
if (x < min_x) min_x = x;
if (y > max_y) max_y = y;
if (y < min_y) min_y = y;
V[y][x] = true;
block_size++;
search_stack.push({y: y+1, x: x});
search_stack.push({y: y, x: x+1});
search_stack.push({y: y-1, x: x});
search_stack.push({y: y, x: x-1});
}
if (block_size > max_size &&
block_size >= Lightboard.LIGHT_SIZE) {
max_size = block_size;
point = {
x: max_x + min_x,
y: max_y + min_y
};
}
}
}
console.log(max_size);
if (point) {
console.log(point);
}
return point;
};