-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
78 lines (63 loc) · 1.61 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
'use strict'
const $drawDots = qs('#drawDots')
const $distance = qs('#distance')
const $count = qs('#count')
const $randomSpeed = qs('#randomSpeed')
const $speed = qs('#speed')
const $randomR = qs('#randomR')
const $r = qs('#r')
const $color = qs('#color')
let dots
let bdist
let isDraw
let fillColor = '#ffffff'
function setupCanvas() {
let canvas
if (windowWidth < 500) {
canvas = createCanvas(windowWidth, 500)
pixelDensity(1)
} else {
canvas = createCanvas(500, 500)
}
const canvasOriginalParent = canvas.parent()
canvas.parent('canvasWrapper')
canvasOriginalParent.remove()
smooth(2)
}
function setup() {
setupCanvas()
qs('input[type="submit"]').addEventListener('click', restart)
$randomSpeed.addEventListener('click', function (e) {
$speed.disabled = e.target.checked
})
$randomR.addEventListener('click', function (e) {
$r.disabled = e.target.checked
})
restart()
}
function restart() {
fillColor = $color.value
stroke(fillColor)
isDraw = $drawDots.checked
bdist = $distance.valueAsNumber
dots = new Array($count.valueAsNumber)
for (let i = 0; i < dots.length; i++) {
const dot = new Dot()
if (!$randomSpeed.checked) dot.speed = $speed.valueAsNumber
if (!$randomR.checked) dot.r = $r.valueAsNumber
dots[i] = dot
}
}
function draw() {
background(0)
for (let i = 0; i < dots.length; i++) {
for (let j = 0; j < i; j++) {
const d = dist(dots[i].x, dots[i].y, dots[j].x, dots[j].y)
if (d < bdist) {
line(dots[i].x, dots[i].y, dots[j].x, dots[j].y)
}
}
dots[i].update()
if (isDraw) dots[i].draw()
}
}