-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
225 lines (206 loc) · 5.11 KB
/
script.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
let hydra, hydraCanvas;
hydraCanvas = document.createElement("canvas");
hydraCanvas.width = window.innerWidth;
hydraCanvas.height = window.innerHeight;
hydraCanvas.id = "hydraCanvas";
hydra = new Hydra({
canvas: hydraCanvas,
detectAudio: false,
enableStreamCapture: false,
width: window.innerWidth,
height: window.innerHeight,
});
document.body.appendChild(hydraCanvas);
const codeblocks = document.querySelectorAll("code");
let initialized = false;
/*
// Usage
const code = `
solid(0, 0, 0)
.layer(src(o0)
.scale(1.01).mask(
shape(99)
.scale(1.5, 1.5, () => window.innerWidth / window.innerHeight)))
.blend(
shape(99)
.scale(1.5, 1.5, () => window.innerWidth / window.innerHeight)
.mult(osc(20, 0.1, 2))
, 0.1)
.out()
`;
const highlighted = highlightHydraCode(code);
document.body.innerHTML = `<pre>${highlighted}</pre>`;*/
for (const cb of codeblocks) {
if (initialized == false) {
eval(cb.textContent);
initialized = true;
}
var observer = new IntersectionObserver(function (entries) {
if (entries[0].isIntersecting === true) {
let code = cb.innerText
.split('\n')
.filter(line => !line.trim().startsWith('//'))
.map(line => line.trim())
.join('')
.replace(/\s+/g, ' ') // first convert all whitespace sequences to single spaces
.replace(/\s*([{,();}])\s*/g, '$1') // remove spaces around brackets/punctuation
.replace(/\s+/g, '') // remove remaining spaces, except...
.replace(/return(\w)/, 'return $1'); // add back space after return
render(o0);
setTimeout(() => {
eval(code)
}, 60);
}
}, { threshold: [0.7] });
observer.observe(cb);
}
// Mostly generated using Claude AI
function highlightHydraCode(code) {
// Define method groups with their respective colors
const methodGroups = {
source: {
methods: [
"noise",
"voronoi",
"osc",
"shape",
"gradient",
"src",
"solid",
"prev"
],
color: "#ee866d" // Orange
},
geometry: {
methods: [
"rotate",
"scale",
"pixelate",
"repeat",
"repeatX",
"repeatY",
"kaleid",
"scroll",
"scrollX",
"scrollY"
],
color: "#fbe77a" // Yellow
},
color: {
methods: [
"posterize",
"shift",
"invert",
"contrast",
"brightness",
"luma",
"thresh",
"color",
"saturate",
"hue",
"colorama",
"sum",
"r",
"g",
"b",
"a"
],
color: "#c3fd7c" // Light Green
},
blend: {
methods: ["add", "sub", "layer", "blend", "mult", "diff", "mask"],
color: "#8ff78a" // Blue
},
modulate: {
methods: [
"modulateRepeat",
"modulateRepeatX",
"modulateRepeatY",
"modulateKaleid",
"modulateScrollX",
"modulateScrollY",
"modulate",
"modulateScale",
"modulatePixelate",
"modulateRotate",
"modulateHue"
],
color: "#92fce6" // Sea form green
},
externalSources: {
methods: [
"initCam",
"initImage",
"initVideo",
"init",
"initStream",
"initScreen"
],
color: "#77b0f7" // Light blue
},
synthSettings: {
methods: [
"render",
"update",
"setResolution",
"hush",
"setFunction",
"speed",
"bpm",
"width",
"height",
"time",
"mouse"
],
color: "#7b67f7" // Purple
},
array: {
methods: ["fast", "smooth", "ease", "offset", "fit"],
color: "#d36cf3" // Pink/Purple
},
audio: {
methods: [
"fft",
"setSmooth",
"setCutoff",
"setBins",
"setScale",
"hide",
"show"
],
color: "#ed70b1" // Pink
},
// Other words we want to highgliht
other: {
methods: ["window", "o0", "o1", "o2"],
color: "#7ad" // Blue
}
};
// Create a map of all methods to their colors for easier lookup
const methodColorMap = {};
Object.values(methodGroups).forEach((group) => {
group.methods.forEach((method) => {
methodColorMap[method] = group.color;
});
});
// Helper function to escape special characters in string for regex
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
// Create regex pattern for all methods
const methodPattern = Object.keys(methodColorMap)
.sort((a, b) => b.length - a.length) // Sort by length to match longer methods first
.map((method) => escapeRegExp(method))
.join("|");
// Replace methods with colored spans
let highlightedCode = code.replace(
new RegExp(`\\b(${methodPattern})\\b`, "g"),
(match) => `<span style="color: ${methodColorMap[match]}">${match}</span>`
);
// Highlight numbers
highlightedCode = highlightedCode.replace(
/\b(\d*\.?\d+)\b/g,
'<span style="color: #c7a3ff">$1</span>' // Light purple for numbers
);
return highlightedCode;
}