-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcartoon.html
More file actions
executable file
·165 lines (134 loc) · 5.46 KB
/
cartoon.html
File metadata and controls
executable file
·165 lines (134 loc) · 5.46 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Video Frames</title>
<link rel="stylesheet" href="style.css">
<style>
#movieclip {
display: none;
}
</style>
</head>
<body>
<header>Avoid cross domain security error. $ http-server -p 8000 </header>
<canvas id="canvas" width="600" height="360"></canvas>
<input id="convolution_additional_value" type="range" value="70" min="45" max="300" step="1"/>
<input id="edge_weight" type="range" value="1" min="0" max="5" step="0.1"/>
<video id="movieclip" width="640" height="360" autoplay>
<source src="./assets/movieclip.mp4" type="video/mp4"/>
<source src="./assets/movieclip.webm" type="video/webm"/>
<source src="./assets/movieclip.ogv" type="video/ogg"/>
</video>
<script src="utils.js"></script>
<script src="Stats.js"></script>
<script src="camera.js"></script>
<script>
window.onload = function () {
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
copyCanvas = createCopyCanvas(canvas),
copyContext = copyCanvas.getContext('2d'),
video = document.getElementById('movieclip'),
stats = Stat.initStats();
function createCopyCanvas (canvas) {
var copy = document.createElement('canvas');
copy.width = canvas.width;
copy.height = canvas.height;
return copy;
}
function edge2 (imageData, copiedImageData, convolutionAdditionalValue, edgeWeight) {
var pixels = imageData.data,
pixelsCopy = copiedImageData.data,
h = imageData.height,
w = imageData.width,
w4 = w * 4,
pixel = w4 + 4, // Start at (1,1)
hm1 = h - 1,
wm1 = w - 1;
for (var y = 1; y < hm1; ++y) {
// Prepare initial cached values for current row
var centerRow = pixel - 4;
var priorRow = centerRow - w4;
var nextRow = centerRow + w4;
var r1 = - pixelsCopy[priorRow] - pixelsCopy[centerRow] - pixelsCopy[nextRow];
var g1 = - pixelsCopy[++priorRow] - pixelsCopy[++centerRow] - pixelsCopy[++nextRow];
var b1 = - pixelsCopy[++priorRow] - pixelsCopy[++centerRow] - pixelsCopy[++nextRow];
var rp = pixelsCopy[priorRow += 2];
var gp = pixelsCopy[++priorRow];
var bp = pixelsCopy[++priorRow];
var rc = pixelsCopy[centerRow += 2];
var gc = pixelsCopy[++centerRow];
var bc = pixelsCopy[++centerRow];
var rn = pixelsCopy[nextRow += 2];
var gn = pixelsCopy[++nextRow];
var bn = pixelsCopy[++nextRow];
var r2 = - rp - rc - rn;
var g2 = - gp - gc - gn;
var b2 = - bp - bc - bn;
// Main convolution loop
for (var x = 1; x < wm1; ++x) {
centerRow = pixel + 4;
priorRow = centerRow - w4;
nextRow = centerRow + w4;
var r = convolutionAdditionalValue + r1 - rp - (rc * -8) - rn;
var g = convolutionAdditionalValue + g1 - gp - (gc * -8) - gn;
var b = convolutionAdditionalValue + b1 - bp - (bc * -8) - bn;
r1 = r2;
g1 = g2;
b1 = b2;
rp = pixelsCopy[ priorRow];
gp = pixelsCopy[++priorRow];
bp = pixelsCopy[++priorRow];
rc = pixelsCopy[ centerRow];
gc = pixelsCopy[++centerRow];
bc = pixelsCopy[++centerRow];
rn = pixelsCopy[ nextRow];
gn = pixelsCopy[++nextRow];
bn = pixelsCopy[++nextRow];
r += (r2 = - rp - rc - rn);
g += (g2 = - gp - gc - gn);
b += (b2 = - bp - bc - bn);
if (r > 255) r = 255;
if (g > 255) g = 255;
if (b > 255) b = 255;
if (r < 0) r = 0;
if (g < 0) g = 0;
if (b < 0) b = 0;
pixels[pixel] = 80 * edgeWeight;
pixels[++pixel] = 80 * edgeWeight;
pixels[++pixel] = 80 * edgeWeight;
if (r > 30 && g > 30 & b > 30) pixels[pixel+1] = 0; // alpha
pixel+=2;
}
pixel += 8;
}
}
Camera.createPlayButton(video);
(function drawFrame () {
stats.begin();
window.requestAnimationFrame(drawFrame, canvas);
copyContext.drawImage(video, 0, 0);
var imageData = copyContext.getImageData(0, 0, canvas.width, canvas.height),
copiedImageData = copyContext.getImageData(0, 0, canvas.width, canvas.height),
convolutionAdditionalValue = parseInt(document.getElementById('convolution_additional_value').value, 10) || 0,
edgeWeight = parseFloat(document.getElementById('edge_weight').value, 10) || 0.0;
edge2(imageData, copiedImageData, convolutionAdditionalValue, edgeWeight);
context.clearRect(0,0,canvas.width,canvas.height);
context.drawImage(
copyCanvas,
0, 0, canvas.width, canvas.height,
0, 0, canvas.width, canvas.height
);
copyContext.putImageData(imageData, 0, 0);
context.drawImage(
copyCanvas,
0, 0, canvas.width, canvas.height,
0, 0, canvas.width, canvas.height
);
stats.end();
}());
};
</script>
</body>
</html>