-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.js
More file actions
85 lines (73 loc) · 2.2 KB
/
process.js
File metadata and controls
85 lines (73 loc) · 2.2 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
$(document).ready(function () {
var xx = [],
$buffer = $('#buffer'), buffer = $buffer[0],
$canvas = $('#canvas'), canvas = $canvas[0],
img = new Image(),
src, paper,
bctx = buffer.getContext('2d'),
cctx = canvas.getContext('2d'),
spec = {}, step, scale,
imgs = ['kings.gif'];
window.location.hash.substr(1).split('&').forEach(function (params) {
var param = params.split('=');
spec[param[0]] = Number(param[1]);
});
step = spec.step || 6;
scale = spec.scale || 1;
console.log(step, scale);
function isRed(color) {
return Number(color[0]) > 127 && Number(color[1]) < 127 && Number(color[2]) < 127;
}
function grayscale (color) {
var gr = (Math.round(color[0] * 0.21) + Math.round(color[1] * 0.71) + Math.round(color[2] * 0.07));
if(! Math.floor(Math.random() * 36) || gr > 235) {
return "rgba(255, 255, 255, 1)";
} else {
return "rgba(0, 0, 0, 1)";
}
}
function imgLoad () {
var j = 0, w = img.width, h = img.height;
for(var i = 0; i < w; i++) {
if(Math.floor(Math.random() * 9)) {
xx.push(i);
}
}
$buffer.attr({width: w, height: h});
bctx.drawImage(img, 0, 0, w, h);
h *= scale;
w *= scale;
$canvas.attr({width: w, height: h});
cctx.fillStyle = '#ffffff';
cctx.fillRect(0, 0, w, h);
loop = setInterval(function () {
var x = 0, l = xx.length, i, p, rbga;
if(j < h) {
for(; x < l; x += step) {
i = xx[x];
if(i < w) {
p = bctx.getImageData(i, j, step, step).data;
if(isRed(p)) {
cctx.fillStyle = "rgba(255, 0, 0,1)";
} else {
cctx.fillStyle = grayscale(p);
}
unit = 2 + Math.floor(Math.random() * 38);
cctx.fillRect(i * scale, j * scale, 1, unit);
}
}
j += step;
console.log('loop: ' + j);
} else {
clearInterval(loop);
console.log('parsed');
$canvas.remove();
$buffer.remove();
src = canvas.toDataURL("image/png");
document.write('<img src="'+src+'"/>');
}
}, 40);
}
img.onload = imgLoad;
img.src = imgs.shift();
});