-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
145 lines (129 loc) · 2.85 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
var circles;
var spots;
var minRadius = 10;
var hasFinishedText = false;
function preload() {
img = loadImage("assets/20.png");
}
function setup() {
console.log(img);
createCanvas(img.width, img.height);
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));
}
}
}
console.log(img.width);
console.log(img.height);
console.log("pixels", img.pixels.length);
console.log("spots", spots.length);
console.log(density)
}
function draw() {
background(255);
// frameRate(20)
var total = 10;
var count = 0;
var attempts = 0;
if(!hasFinishedText) {
while (count < total) {
var newC = newTextCircle();
if (newC !== null) {
circles.push(newC);
count++;
}
attempts++;
if (attempts > 10000) {
hasFinishedText = true;
break;
}
}
} else {
while (count < total) {
var newC = newBackgroundCircle();
if (newC !== null) {
circles.push(newC);
count++;
}
attempts++;
if(attempts > 1000) {
minRadius = 8;
}
if (attempts > 2000) {
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 - minRadius/2 < distance) {
circle.growing = false;
break;
}
}
}
}
}
circle.show();
circle.grow();
}
}
function newTextCircle() {
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 + minRadius) {
valid = false;
break;
}
}
if (valid) {
return new TextCircle(x, y);
} else {
return null;
}
}
function newBackgroundCircle() {
var x = int(random(width));
var y = int(random(height));
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 + minRadius) {
valid = false;
break;
}
}
if (valid) {
return new BackgroundCircle(x, y);
} else {
return null;
}
}