-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
121 lines (107 loc) · 2.55 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
var circles;
var spots;
var img;
//p5.js starter function
function preload() {
img = loadImage('files/file.png');
}
//var newimg = documentgetElementbyId("image");
//another important function of p5.js
function setup() {
createCanvas(1920 , 1080);
var density = displayDensity();
pixelDensity(1);
img.loadPixels();
spots = [];
circles = [];
for (var x = 0; x < img.width; x++) {
for (var y = 0; y < img.height; y++) {
var index = x + y * img.width;
var c = img.pixels[index * 4];
var b = brightness([c]);
if (b > 1) {
spots.push(createVector(x, y)); //createVector: p5.js function to create a 2d or 3d vector according to passed parameters.
}
}
}
console.log(img.width);
console.log(img.height);
console.log('pixels', img.pixels.length);
console.log('spots', spots.length);
console.log(density);
}
//p5.js draw function
function draw() {
background(0);
// frameRate(20)
var total = 10;
var count = 0;
var attempts = 0;
while (count < total) {
var newC = newCircle();
if (newC !== null) {
circles.push(newC);
count++;
}
attempts++;
if (attempts > 1000) {
noLoop();
console.log('finished');
break;
}
}
for (var i = 0; i < circles.length; i++) {
var circle = circles[i];
if (circle.growing) {
if (circle.edges()) {
circle.growing = false;
} else {
for (var j = 0; j < circles.length; j++) {
var other = circles[j];
if (circle !== other) {
var d = dist(circle.x, circle.y, other.x, other.y);
var distance = circle.r + other.r;
if (d - 5 < distance) {
circle.growing = false;
break;
}
}
}
}
}
circle.show();
circle.grow();
}
}
function newCircle() {
var r = int(random(0, spots.length));
var spot = spots[r];
var x = spot.x;
var y = spot.y;
var valid = true;
for (var i = 0; i < circles.length; i++) {
var circle = circles[i];
var d = dist(x, y, circle.x, circle.y);
if (d < circle.r) {
valid = false;
break;
}
}
if (valid) {
return new Circle(x, y);
} else {
return null;
}
}
/*function processNewFile(file_list){
let file = file_list[0];
console.log(URL.createObjectURL(file))
img = loadImage(URL.createObjectURL(file));
setup();
}*/
function processNewFile(){
img = fileInput;
setup();
}
const fileInput = document.getElementById('image');
fileInput.addEventListener('change', (e) => processNewFile(e.target.files));