-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpatchwork.js
120 lines (100 loc) · 2.56 KB
/
patchwork.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
// Using const to avoid accidental override by patches
// patches should be imported from external file
const patches = [patch01, patch02, patch03, patch01]
const columns = Math.round(Math.sqrt(patches.length))
let rows = Math.round(patches.length / columns)
if (columns * rows < patches.length) rows++
// Standard scale
const RESOLUTION = 2048
// Detect dimensions
const DIM = Math.min(window.innerWidth, window.innerHeight)
const UNIT = DIM / RESOLUTION
const patchWidth = DIM / columns
const patchHeight = DIM / rows
// Global scaler based on resolution
const scale = (x) => x * UNIT
// Shared palettes
const PALETTES = [
["#fff", "#000"],
["#32936f", "#395e66", "#387d7a", "#26a96c", "#2bc016"],
["#f02d3a", "#273043", "#9197ae", "#eff6ee", "#dd0426"]
]
// Shared hash
let { hash } = tokenData
let seed = parseInt(hash.slice(0, 16), 16)
// Shared PRNG class
class Random {
constructor(seed) {
this.seed = seed
}
random_dec() {
// RNG TBD
return Math.random()
}
random_num(a, b) {
return a + (b - a) * this.random_dec()
}
random_int(a, b) {
return Math.floor(this.random_num(a, b + 1))
}
random_element(array) {
return array[this.random_int(0, array.length - 1)]
}
// Add gaussian?
}
// Global RNG
const RandomShared = new Random(seed)
// Global variables
const PAL = RandomShared.random_element(PALETTES)
const BG = "#000000"
let patchGrid = []
setup = () => {
createCanvas(DIM, DIM)
background(BG)
frameRate(30)
// initialize patches
let row = 0
let column = 0
for (let patch of patches) {
if (!patchGrid[row]) {
patchGrid[row] = []
}
patchGrid[row].push(
patch(patchWidth, patchHeight, PAL, new Random(seed), [row, column])
)
column++
if (column === columns) {
column = 0
row++
}
}
}
// To be removed
const drawGrid = () => {
// Computing section coordinates
noFill()
stroke(0, 255, 0, 150)
strokeWeight(scale(5))
for (let column = 0; column <= columns; column++) {
line(patchWidth * column, 0, patchWidth * column, height)
}
for (let row = 0; row <= rows; row++) {
line(0, patchHeight * row, width, patchHeight * row)
}
}
draw = () => {
// draw each patch in the grid
for (let row = 0; row < patchGrid.length; row++) {
for (let column = 0; column < patchGrid[row].length; column++) {
// isolate drawing styles & transformations
push()
// move drawing area to patch
translate(column * patchWidth, row * patchHeight)
// draw patch
patchGrid[row][column]()
pop()
}
}
// To be removed
drawGrid()
}