-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
184 lines (169 loc) · 4.45 KB
/
sketch.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
document.addEventListener(
"touchmove",
function(n) {
n.preventDefault();
},
{
passive: false
}
);
let state = -1;
let doubleClick,
ts = [];
let points = [];
let k = 50;
let kMoyen = [];
let kPoints = [];
let dis = [];
let colors = [];
let pixelPos = [];
let img;
let count = 0;
let [Video, vScale] = ["", 3];
let kScale = 60;
function preload() {
img = loadImage("assets/3.jpg");
}
function setup() {
pixelDensity(1.0);
createCanvas(windowWidth, windowHeight);
if (height > width) {
vScale = 9;
kScale = 150;
}
Video = createCapture(Video);
Video.loop();
Video.hide();
Video.size(width / vScale, height / vScale);
// count=0, pixelPos=[];
img.resize(width, height);
// img.loadPixels();
// for (let y = 0; y < height; y++) {
// for (let x = 0; x < width; x++) {
// let index = (width * y + x) * 4;
// let lumi = img.pixels[index] + img.pixels[index + 1] + img.pixels[index + 2] + img.pixels[index + 3];
// if (lumi < 800) {
// count++;
// }
// }
// }
// for (let y = 0; y < height; y++) {
// for (let x = 0; x < width; x++) {
// let index = (width * y + x) * 4;
// let lumi = img.pixels[index] + img.pixels[index + 1] + img.pixels[index + 2] + img.pixels[index + 3];
// if (lumi < 800 && Math.random() > 1 - (constrain(width * height * 0.02, 7000, 20000) / count)) {
// let ee = createVector(x, y);
// pixelPos.indexOf(ee) === -1 ? pixelPos.push(ee) : '';
// }
// }
// }
// noCursor();
textSize(30);
for (let i = 0; i < 10000; i++) {
let dump = createVector(
random(0.3 * width, 0.7 * width),
random(0.3 * height, 0.7 * height)
);
points.push(dump);
}
// points = pixelPos;
// points = [];
kMoyen = [...points].slice(0, k + 1);
for (let i = 0; i < k + 1; i++) {
kMoyen[k] = createVector(
random(width / 2, width / 2.1),
random(width / 2, width / 2.1)
);
}
for (let i = 0; i < kMoyen.length + 1; i++) {
kPoints[i] = [];
colors[i] = [random(100, 255), random(50, 255), random(100, 255), 200];
}
}
function aver(arr) {
if (arr.length === 0) {
return createVector(0, 0);
}
let [xs, ys] = [[], []];
arr.forEach(obj => {
xs.push(obj.x);
ys.push(obj.y);
});
let x = xs.reduce((a, b) => a + b) / xs.length;
let y = ys.reduce((a, b) => a + b) / ys.length;
return createVector(x, y);
}
function draw() {
points = [];
Video.loadPixels();
loadPixels();
let bris = [];
for (let y = 0; y < Video.height; y++) {
for (let x = 0; x < Video.width; x++) {
let index = (Video.width - x + 1 + y * Video.width) * 4;
let r = Video.pixels[index + 0];
let g = Video.pixels[index + 1];
let b = Video.pixels[index + 2];
let bright = (r + g + b) / 3;
bris.push(bright);
}
}
bris.sort((a, b) => a > b);
let median = bris[Math.floor(bris.length / 2.5)];
for (let y = 0; y < Video.height; y++) {
for (let x = 0; x < Video.width; x++) {
var index = (Video.width - x + 1 + y * Video.width) * 4;
let r = Video.pixels[index + 0];
let g = Video.pixels[index + 1];
let b = Video.pixels[index + 2];
let bright = (r + g + b) / 3;
let ee = createVector(x * vScale, y * vScale);
ee.r = r;
ee.g = g;
ee.b = b;
// face¬ window
// bright < 220 && bright > 90 ? points.push(ee) : "";
// only black
bright < 90 ? points.push(ee) : "";
}
}
kMoyen[k + 1] = createVector(mouseX, mouseY);
background(0);
noStroke();
for (let i = 0; i < points.length; i++) {
for (let t = 0; t < kPoints.length; t++) {
dis[t] = p5.Vector.dist(points[i], kMoyen[t]);
}
let t = Math.min(...dis);
let n = dis.indexOf(t);
kPoints[n].push(points[i]);
}
for (let i = 0; i < kPoints.length; i++) {
kMoyen[i] = aver(kPoints[i]);
}
// display
for (let i = 0; i < kPoints.length; i++) {
fill(colors[i]);
kPoints[i].forEach(a => {
ellipse(
a.x + Math.random(),
a.y - Math.random(),
height > width ? 4 : 2,
height > width ? 4 + Math.random() * 3 : 2
);
});
fill(colors[i][0], colors[i][1], colors[i][2], 100);
i === kMoyen.length - 1 ? fill(200, 0, 180, 200) : "";
ellipse(
kMoyen[i].x,
kMoyen[i].y,
(Math.sqrt(kPoints[i].length) / k) * kScale
);
}
for (let i = 0; i < kMoyen.length; i++) {
kPoints[i] = [];
}
}
document.touchmove = function(n) {
n.preventDefault();
};