forked from epiqueras/epiqueras.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
executable file
·279 lines (259 loc) · 7.13 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
'use strict'
const dotSize = 2
const maxConnectionLength = 20
const cellWidth = 20
const cellHeight = 20
let input = '{hello}'
let numOfDots = 0
let time = 0
let shape
// Dot class
class Dot {
constructor(anchor, position, radius, direction, startAngle) {
this.size = dotSize
this.anchor = anchor
this.position = position
this.radius = radius
this.direction = direction
this.startAngle = startAngle
this.connections = []
this.transporting = false
this.transportDir
this.transportLoc = createVector(0, 0)
}
move() {
if (!this.transporting) {
this.position.x =
this.anchor.x +
cos(time * this.direction + this.startAngle) * this.radius
this.position.y =
this.anchor.y +
sin(time * this.direction + this.startAngle) * this.radius
} else {
if (this.position.dist(this.transportLoc) < 3) {
this.transporting = false
} else {
this.position.add(this.transportDir)
}
}
}
transport(newAnchor) {
this.anchor = newAnchor
this.transporting = true
this.transportLoc.x =
this.anchor.x + cos(time * this.direction + this.startAngle) * this.radius
this.transportLoc.y =
this.anchor.y + sin(time * this.direction + this.startAngle) * this.radius
this.transportDir = p5.Vector.sub(this.transportLoc, this.position)
this.transportDir.normalize()
this.transportDir.mult(4)
}
render() {
noStroke()
fill(255, 200)
ellipse(this.position.x, this.position.y, this.size)
}
run() {
this.render()
this.move()
}
}
class Shape {
constructor(width, height, x, y, string) {
this.dots = []
this.setUp(width, height, x, y, string)
}
createGrid() {
const cols = floor(this.graphic.width / cellWidth)
const rows = floor(this.graphic.height / cellHeight)
const dots = []
for (let col = 0; col <= cols; col++) {
dots.push([])
for (let row = 0; row <= rows; row++) {
dots[col].push([])
}
}
return { cols, rows, dots }
}
changeString(x, y, string = this.string) {
this.isChanging = true
const fontSize = this.graphic.width / sqrt(string.length + 10)
this.x = x
this.y = y + fontSize / 3.8
this.graphic.background(255)
this.graphic.noStroke()
this.graphic.fill(0)
this.graphic.textAlign(CENTER)
this.graphic.textSize(fontSize)
this.graphic.text(string, this.x, this.y)
this.graphic.loadPixels()
this.createDots()
this.isChanging = false
this.image = null
this.string = string
}
changeImage(image = this.image) {
this.isChanging = true
this.graphic.background(255)
image.resize(0, this.graphic.height / 2)
this.graphic.copy(
image,
0,
0,
image.width,
image.height,
this.graphic.width / 2 - image.width / 2,
this.graphic.height / 2 - image.height / 2,
image.width,
image.height
)
this.graphic.loadPixels()
this.createDots()
this.isChanging = false
this.string = null
this.image = image
}
setUp(width, height, x, y, string) {
this.graphic = createGraphics(width, height)
this.grid = this.createGrid()
if (string || this.string) this.changeString(x, y, string)
else this.changeImage()
}
createDots() {
const freeDots = this.dots
this.dots = []
for (let i = 0; i < numOfDots; i++) {
const x = floor(random(this.graphic.width))
const y = floor(random(this.graphic.height))
const pd = pixelDensity()
const index = 4 * pd * (x + y * this.graphic.width * pd)
const pixel = this.graphic.pixels[index]
if (pixel < 255) {
const anchor = createVector(x, y)
if (freeDots.length === 0) {
const radius = random(5, 10)
const startPosition = createVector(anchor.x + radius, anchor.y)
const startAngle = random(TWO_PI)
const prob = random()
let direction
if (prob < 0.5) {
direction = -1
} else {
direction = 1
}
const dot = new Dot(
anchor,
startPosition,
radius,
direction,
startAngle
)
this.dots.push(dot)
} else {
const dot = freeDots.pop()
dot.transport(anchor)
this.dots.push(dot)
}
}
}
}
static checkForConnect(dotA, dotB, numOfConnections) {
let newNumOfConnections = numOfConnections
const distance = dist(
dotA.position.x,
dotA.position.y,
dotB.position.x,
dotB.position.y
)
if (distance > 0 && distance < maxConnectionLength) {
const sat = map(newNumOfConnections, 0, 10, 0, 255)
stroke(sat)
line(dotA.position.x, dotA.position.y, dotB.position.x, dotB.position.y)
newNumOfConnections++
}
return newNumOfConnections
}
registerDots() {
this.grid = this.createGrid()
for (let i = 0; i < this.dots.length; i++) {
let col = floor(this.dots[i].position.x / cellWidth)
let row = floor(this.dots[i].position.y / cellHeight)
col = col < 0 ? 0 : col > this.grid.cols ? this.grid.cols : col
row = row < 0 ? 0 : row > this.grid.rows ? this.grid.rows : row
this.grid.dots[col][row].push(this.dots[i])
}
}
connectDots() {
let numOfConnections = 1
for (let col = 1; col < this.grid.cols - 1; col++) {
for (let row = 1; row < this.grid.rows - 1; row++) {
for (let i = 0; i < this.grid.dots[col][row].length; i++) {
// Check all surrounding cells
for (let addCol = -1; addCol <= 1; addCol++) {
for (let addRow = -1; addRow <= 1; addRow++) {
for (
let j = 0;
j < this.grid.dots[col + addCol][row + addRow].length;
j++
) {
if (!(addCol === 0 && addRow === 0) || i !== j) {
numOfConnections = Shape.checkForConnect(
this.grid.dots[col][row][i],
this.grid.dots[col + addCol][row + addRow][j],
numOfConnections
)
}
}
}
}
numOfConnections = 1
}
}
}
}
run() {
if (!this.isChanging) {
this.registerDots()
this.connectDots()
for (let i = 0; i < this.dots.length; i++) {
this.dots[i].run()
}
}
}
}
function gotFile(file) {
if (file.type === 'image') {
loadImage(file.data, image => shape.changeImage(image))
}
}
function setup() {
const canvas = createCanvas(windowWidth, windowHeight)
canvas.drop(gotFile)
background(20)
numOfDots = max(windowWidth, windowHeight) * 6
shape = new Shape(
windowWidth,
windowHeight,
windowWidth / 2,
windowHeight / 2,
input
)
input = ''
}
function draw() {
background(20)
shape.run()
time += 0.05
}
function keyTyped() {
if (keyCode === 13) {
shape.changeString(windowWidth / 2, windowHeight / 2, input)
input = ''
} else {
input += key
}
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight)
shape.setUp(windowWidth, windowHeight, windowWidth / 2, windowHeight / 2)
}