-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlineStudy5.mjs
More file actions
107 lines (94 loc) · 2.06 KB
/
lineStudy5.mjs
File metadata and controls
107 lines (94 loc) · 2.06 KB
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
// https://www.youtube.com/watch?v=lNKFhaOQJys&t=259s
import ControlPanel, {
Range,
createBlendMode,
} from '../lib/ControlPanel/index.mjs'
export default function lines(p) {
const [w, h] = [500, 500]
const metadata = {
name: 'lineStudy5',
}
const controlPanel = new ControlPanel({
p,
id: metadata.name,
controls: {
nLines: new Range({
name: 'nLines',
value: 40,
min: 1,
max: 100,
}),
range: new Range({
name: 'range',
value: 5,
min: 0,
max: 100,
}),
segmentLength: new Range({
name: 'segmentLength',
value: 1,
min: 1,
max: 100,
}),
strokeWeight: new Range({
name: 'strokeWeight',
value: 2,
min: 1,
max: 20,
}),
blendMode: createBlendMode(),
},
})
function setup() {
controlPanel.init()
const canvas = p.createCanvas(w, h)
p.colorMode(p.HSB, 1)
p.noStroke()
p.noLoop()
return {
canvas,
}
}
function draw() {
const { nLines, segmentLength, strokeWeight, blendMode, range } =
controlPanel.values()
p.blendMode(p[blendMode])
p.background(1, 0.02, 1)
p.fill(0)
p.stroke(0)
p.strokeWeight(strokeWeight)
const n = Math.floor(h / nLines)
const pad = 8
for (let y = n; y < h - n; y += n) {
drawLine({
x: pad,
y,
length: w - pad * 2,
segmentLength,
range,
})
}
}
function drawLine({ x: lineX, y: lineY, length, segmentLength, range }) {
let prevX = lineX
let prevY = lineY
const r = p.map(lineY, 0, h, 0, range)
const nSegments = Math.floor(length / segmentLength)
const adjustedSegmentLength = length / nSegments
for (let i = 1; i <= nSegments; i++) {
const x = lineX + i * adjustedSegmentLength
const y = lineY + p.random(-r, r)
p.line(prevX, prevY, x, y)
prevX = x
prevY = y
}
}
return {
setup,
draw,
destroy() {
controlPanel.destroy()
},
metadata,
}
}