-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.html
265 lines (198 loc) · 6.21 KB
/
demo.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Guinda</title>
<style>
g-knob {
width: 74px;
height: 74px;
--body-color: #404040;
--range-color: #404040;
--value-color: #fff;
--pointer-off-color: #000;
--pointer-on-color: #fff;
}
g-fader {
width: 37px;
height: 185px;
--graphic: lines;
--body-color: #404040;
--value-color: #fff;
}
g-button {
height: 37px;
padding: 0 2em;
font-family: monospace;
border-radius: 0.2em;
color: #fff;
background-color: #404040;
--selected-color: #404040;
}
g-resize {
--graphic: lines;
--color: #000;
}
</style>
</head>
<body>
<h2><a href="https://github.com/lucianoiam/guinda" target="_blank">Guinda</a></h2>
<hr>
<div id="widgets">
<h4>Knob</h4>
<g-knob
min="0"
max="1.0"
value="0.25"
oninput="toast(this.value.toFixed(2))">
</g-knob>
<hr>
<h4>Fader</h4>
<g-fader
min="0"
max="1.0"
value="0.25"
oninput="toast(this.value.toFixed(2))">
</g-fader>
<hr>
<h4>Button</h4>
<g-button
mode="latch"
oninput="toast(this.value)">
TAP ME
</g-button>
<hr>
<h4>Resize Handle</h4>
<div id="resize-container">
<div class="hint">Parent</div>
<g-resize
minwidth="200"
minheight="100"
maxscale="2"
keepaspectratio="true"
oninput="resizeListener.call(this)">
</g-resize>
</div>
</div>
<pre id="log"></pre>
<!-- Event listeners -->
<script>
function resizeListener() {
const style = this.parentNode.style;
style.width = this.value.width + 'px';
style.height = this.value.height + 'px';
toast(Math.round(this.value.width) + 'x' + Math.round(this.value.height));
}
</script>
<!-- END OF RELEVANT DEMO SOURCE -->
<style>
body {
background: #000;
color: #fff;
font-family: sans-serif;
margin: 0 20px;
user-select: none;
-webkit-user-select: none;
}
a {
color: #fff;
}
pre {
white-space: pre-wrap;
}
hr {
opacity: 0.25;
}
g-resize line {
stroke: #000; /* lines version */
}
#log {
background-color: rgba(0, 0, 0, 0.75);
position: fixed;
top: 0;
left: 50%;
transform: translate(-50%, 0);
height: 3rem;
font-size: 3rem;
padding: 10px;
margin: 0;
border-radius: 5px;
visibility: hidden;
}
#resize-container {
box-sizing: border-box; /* make <g-resize> behave in a predictable way */
position: relative; /* support <g-resize> position:absolute */
border-radius: 5px;
background: #404040;
width: 300px;
height: 150px;
}
#resize-container > div {
font-size: 0.75rem;
padding: 5px;
}
.hint {
opacity: 0.2;
text-transform: uppercase;
}
</style>
<script>
// Code for self-updating demo
let toastTimeout = null;
const cssRuleList = document.styleSheets[0].cssRules;
const widgets = [...document.getElementById('widgets').querySelectorAll('*')]
.filter(el => el.nodeName.startsWith('G-'));
for (const widget of widgets) {
const isDirectChild = widget.parentNode.id == 'widgets';
const container = isDirectChild ? widget.parentNode : widget.parentNode.parentNode;
const insertPointElem = isDirectChild ? widget : widget.parentNode;
// Insert widget HTML before each widget
const widgetClone = widget.cloneNode(true);
widgetClone.removeAttribute('style');
const preWithHtml = document.createElement('pre');
preWithHtml.innerText = widgetClone.outerHTML.replace(/\s+/g, ' ').replace('> <', '><');
container.insertBefore(preWithHtml, insertPointElem);
// Insert widget CSS after each widget
for (const cssRule of cssRuleList) {
if (cssRule.selectorText.startsWith(widget.nodeName.toLowerCase())) {
const preWithCss = document.createElement('pre');
const rule = cssRule.cssText.replaceAll('{', '{\n').replaceAll(';', ';\n')
.replace(/ +/g,' ');
preWithCss.innerText = formatBlock(rule);
container.insertBefore(preWithCss, insertPointElem.nextSibling);
}
}
// Insert widget callback code after each widget
const listener = widget.nodeName.split('-').slice(1).map((s, i) => {
return i == 0 ? s.toLowerCase() : s[0] /* upper */ + s.slice(1).toLowerCase();
}).join('') + 'Listener'; // <g-knob> -> knobListener
if (listener in window) {
const preWithJs = document.createElement('pre');
preWithJs.innerText = formatBlock(window[listener].toString())
container.insertBefore(preWithJs, insertPointElem.nextSibling);
}
}
function formatBlock(block) {
const lines = block.split('\n');
if (lines.length < 2) {
return lines;
}
return lines[0] + '\n'
+ lines.slice(1, lines.length - 1).map(l => ' ' + l.trim()).join('\n') + '\n'
+ lines.slice(-1)[0].trim();
}
function toast(text) {
if (toastTimeout) {
clearTimeout(toastTimeout);
}
const log = document.getElementById('log');
log.innerText = text;
log.style.visibility = 'visible';
toastTimeout = setTimeout(() => log.style.visibility = 'hidden', 1000);
}
</script>
<!-- Keep script at the end so the self-updating demo can read the intact document html -->
<script src="guinda.js"></script>
</body>
</html>