-
Notifications
You must be signed in to change notification settings - Fork 18
/
eyeball.js
73 lines (53 loc) · 1.65 KB
/
eyeball.js
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
"use strict";
var Poisson = require('./');
var PNG = require('pngjs-nozlib').PNG;
function outputPng (sampling, drawFunc) {
var png = new PNG({
width: sampling.shape[0],
height: sampling.shape[1],
colortype: 6,
inputHasAlpha: true,
filterType: 4
});
var pngData = new Uint8Array(png.data);
drawFunc(sampling, pngData);
for (var i = 3; i < pngData.length; i+=4) {
pngData[i] = 255;
}
png.data = pngData;
png.pack().pipe(process.stdout);
}
var dimensions = process.argv.length > 2 ? parseInt(process.argv[2], 10) : 2;
if (dimensions === 3) {
var sampling = new Poisson({
shape: [800, 400, 200],
minDistance: 20,
maxDistance: 25,
tries: 20
});
sampling.fill();
outputPng(sampling, function (sampling, pngData) {
sampling.getAllPoints().forEach(function (point) {
var pixelIndex = (Math.ceil(point[0]) + Math.ceil(point[1]) * sampling.shape[0]) * 4;
var intensity = 255 - 255 * Math.pow((1 + point[2]) / sampling.shape[2], 1.5) | 0;
pngData[pixelIndex] = pngData[pixelIndex + 1] = pngData[pixelIndex + 2] = intensity;
});
});
} else {
var sampling = new Poisson({
shape: [600, 600],
minDistance: 8,
maxDistance: 8,
tries: 20
});
sampling.fill();
outputPng(sampling, function (sampling, pngData) {
var i = 0;
sampling.getAllPoints().forEach(function (point) {
i++;
var pixelIndex = (Math.floor(point[0]) + Math.floor(point[1]) * sampling.shape[0]) * 4;
var intensity = 50 + (Math.cos(i / 50) + 1) / 2 * 205 | 0;
pngData[pixelIndex] = pngData[pixelIndex + 1] = pngData[pixelIndex + 2] = intensity;
});
});
}