-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
351 lines (349 loc) · 12.3 KB
/
index.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
</dev/null #><!-- Shell script for starting local http server
npx live-server --port=80
exit#-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>​</title>
<style>
body { margin: 0; background: #000; }
canvas {
position: fixed;
transform-origin: bottom left;
bottom: 0; left: 0;
}
#layer {
font-size: 50px; /*No px yet*/
position: fixed;
top: 0; left: 0;
color: white; white-space: pre-wrap;
padding: 2px; z-index: 10; transform-origin: 0 0;
font-family: 'Monocraft', Menlo, Consolas, monospace;
}
</style>
<link rel="icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAAtJREFUGFdjYAACAAAFAAGq1chRAAAAAElFTkSuQmCC" id="icon">
</head>
<div id="layer"></div>
<script>
class BitField extends Array{
set(int){
while((int >> 5) >= this.length)this.push(0)
this[int >> 5] |= 1 << (int & 31)
}
unset(int){
let i = this.length
if((int >> 5) >= i)return
this[int >> 5] &= ~(1 << (int & 31))
while(i && !this[--i])this.pop()
}
toggle(int){
let i = this.length
while((int >> 5) >= i)this.push(0)
this[int >> 5] ^= 1 << (int & 31)
while(i && !this[--i])this.pop()
}
has(int){
if((int >> 5) >= this.length)return false
return !!(this[int >> 5] & (1 << (int & 31)))
}
}
const {sqrt, cbrt, sin, cos, abs, min, max, PI, E, atan2, floor, ceil, round, log10, sign, random} = Math
const PI2 = PI * 2
let D = 1 / devicePixelRatio
document.width = document.height = 0; document.mx = innerWidth * devicePixelRatio / 2; document.my = innerHeight * devicePixelRatio / 2
function getM(mat){
return `matrix(${D * mat.a},${D * mat.b},${D * mat.c},${D * mat.d},${D * mat.e},${D * mat.f/* - visualViewport.height * visualViewport.scale*/})`
}
onresize = () => {
VIEW.width = round(visualViewport.width * visualViewport.scale * devicePixelRatio)
VIEW.height = round(visualViewport.height * visualViewport.scale * devicePixelRatio)
layer.style.width = VIEW.width + 'px'
layer.style.height = VIEW.height + 'px'
layer.style.transform = `scale(${1 / devicePixelRatio})`
D = 1 / devicePixelRatio
for(const c of VIEW.canvases) c.canvas.style.transform = getM(c.matrix)
}
const {prototype} = class extends CanvasRenderingContext2D{
transformBox(a, b, c, d, e, f){
this.matrix.a = a; this.matrix.b = b; this.matrix.c = c; this.matrix.d = d; this.matrix.e = e; this.matrix.f = f
this.canvas.style.transform = getM(this.matrix);
[this.mx, this.my] = getLocalCoords(this, VIEW.mx, VIEW.my)
}
resize(w, h, a = true){
this.canvas.width = w
this.canvas.height = h
this.imageSmoothingEnabled = a
}
camTransform(x = 0, y = 0, zoom = 1, rot = 0, _px = px){
const w2 = this.canvas.width / 2, h2 = this.canvas.height / 2
let cosr = cos(rot), sinr = sin(rot)
this.xs = (w2 * abs(cosr) + h2 * abs(sinr)) * zoom / _px
this.ys = (w2 * abs(sinr) + h2 * abs(cosr)) * zoom / _px
cosr = _px * cosr / zoom; sinr = _px * sinr / zoom
this.setTransform(cosr, sinr, -sinr, cosr, this.canvas.width / 2 - cosr * x - sinr * y, this.canvas.height / 2 - sinr * x - cosr * y)
}
get width(){return this.canvas.width}
get height(){return this.canvas.height}
}
Object.defineProperty(prototype, Symbol.toStringTag, {value: 'Canvas', enumerable: false})
const VIEW = document
VIEW.canvases = new Set()
VIEW.audio = new AudioContext()
AudioNode.prototype.to = function(a){
a = a.destination || a
let n = this
while(n){
n.disconnect()
n.connect(a)
n = n.last
}
return a
}
AudioNode.prototype.end = function(){
let n = this
while(n){
n.stop(0)
n.disconnect()
n = n.last
}
}
const _can = document.createElement('canvas').getContext('2d')
Image.prototype.resize = function(sx, sy, sw, sh, tw, th, q = VIEW.queue){
_can.canvas.width = tw
_can.canvas.height = th
_can.drawImage(this, sx, sy, sw, sh, 0, 0, tw, th)
//Workaround for tainted canvases
return _can.canvas
const img = new Image()
img.onload = q.add()
_can.canvas.toBlob(blob => img.src = URL.createObjectURL(blob))
return img
}
AudioBufferSourceNode.prototype.then = function(sound, l, d, s, e){
if(this.willEndAt == Infinity){console.warn('Called .then() on a looping AudioBufferSourceNode, which can never end.'); return this}
let sn = sound(l, d + this.willEndAt - VIEW.audio.currentTime, s, e)
sn.last = this
return sn
}
Object.defineProperty(GainNode.prototype, 'volume', {get(){return this.gain.value},set(a){this.gain.value=a},enumerable:false})
Object.defineProperties(Document.prototype, {
background: {get(){return this.body.style.background},set(a){this.body.style.background = a}},
canvas: {value(){
let c = this.createElement('canvas').getContext('2d')
Object.setPrototypeOf(c, prototype)
c.fps = -1
c.lastRender = 0
c.render = Function.prototype
c.canvas.width = VIEW.width; c.canvas.height = VIEW.height
c.mx = VIEW.mx; c.my = VIEW.my; c.xs = c.ys = 0
c.canvas.style.transform = getM(c.matrix = new DOMMatrix([1, 0, 0, 1, 0, 0]))
this.body.append(c.canvas)
this.canvases.add(c)
return c
}},
add: {value(c){
if(c.canvas.ownerDocument) c.canvas.ownerDocument.canvases.delete(c)
this.body.append(c.canvas)
this.canvases.add(c)
}},
remove: {value(c){
c.canvas.remove()
this.canvases.delete(c)
}},
icon: {get(){return this.head.children.icon.href}, set(a){this.head.children.icon.href = a}},
pointerlock(){
},
pointer: {get(){return this.body.style.cursor}, set(s){this.body.style.cursor = s}},
onlock: {value: Function.prototype},
onunlock: {value: Function.prototype},
loadVideo: {value(src, q = this.queue){
let v = this.createElement('video')
v.src = src
const cb = q.add()
v.oncanplaythrough = e => (cb(e), v.oncanplaythrough = null)
v.addEventListener('error', cb)
v.load()
return v
}},
loadImage: {value(src, q = this.queue){
let i = new Image()
const cb = q.add()
i.addEventListener('load', cb)
i.addEventListener('error', cb)
i.src = src
return i
}},
loadAudio: {value(src, q = this.queue){
const cb = q.add()
let buffer
const source = VIEW.audio.createBufferSource()
fetch(src).then(a => a.arrayBuffer()).then(a => VIEW.audio.decodeAudioData(a, buf => (buffer = buf, cb()))).catch(cb)
return (loop = false, delay = 0, start = 0, end = NaN) => {
const source = VIEW.audio.createBufferSource()
source.buffer = buffer
source.connect(VIEW.audio.destination)
source.loop = loop
const t = VIEW.audio.currentTime
if(loop){
source.loopStart = start
if(end == end)source.loopEnd = end
source.start(t + delay, start)
}else{
source.start(t + delay, start)
if(end == end)source.stop(t + delay + end - start)
}
source.willEndAt = loop ? Infinity : t + (end == end ? end : source.buffer.duration) - start
return source
}
}},
async:{value(){
return this.queue.async()
}},
loadFont: {value(name, a, q = this.queue){
const f = new FontFace(name, 'url("'+encodeURI(a)+'")')
q.promise(f.loaded)
document.fonts.add(f)
f.load()
return f
}}
})
class Queue{
cbs = []
ecbs = []
left = 0
done = 0
error = 0
_cb = e => {if(e && (e.type == 'error' || e instanceof Error)){for(const cb of this.ecbs)cb(e);this.ecbs.length=this.cbs.length=0;this.error++;this.left--;return};this.done++;if(!--this.left){for(const cb of this.cbs)cb();this.cbs.length=this.ecbs.length=0}}
add(){
this.left++
return this._cb
}
promise(pr){
this.left++
pr.then(this._cb, this._cb)
}
async(){if(!this.left)return Promise.resolve(); return new Promise((r,c)=>{this.cbs.push(r);this.ecbs.push(c)})}
callback(cb, ecb = Function.prototype){
if(!this.left)return cb()
this.cbs.push(cb)
if(ecb)this.ecbs.push(ecb)
}
}
VIEW.queue = new Queue()
VIEW.running = true
VIEW.buttons = new BitField()
VIEW.mxa = VIEW.mya = VIEW.wxa = VIEW.wya = 0
const LBUTTON = 0, RBUTTON = 2, MBUTTON = 1
VIEW.body.addEventListener('mousedown', e => (VIEW.buttons.set(e.button), e.preventDefault()))
VIEW.body.addEventListener('mouseup', e => (VIEW.buttons.unset(e.button), e.preventDefault()))
VIEW.body.addEventListener('keydown', e => (e.repeat || VIEW.buttons.set(e.keyCode), e.preventDefault()))
VIEW.body.addEventListener('keyup', e => (e.repeat || VIEW.buttons.unset(e.keyCode), e.preventDefault()))
const
KEY_A = 65, KEY_B = 66, KEY_C = 67, KEY_D = 68, KEY_E = 69, KEY_F = 70,
KEY_G = 71, KEY_H = 72, KEY_I = 73, KEY_J = 74, KEY_K = 75, KEY_L = 76,
KEY_M = 77, KEY_N = 78, KEY_O = 79, KEY_P = 80, KEY_Q = 81, KEY_R = 82,
KEY_S = 83, KEY_T = 84, KEY_U = 85, KEY_V = 86, KEY_W = 87, KEY_X = 88,
KEY_Y = 89, KEY_Z = 90, KEY_0 = 48, KEY_1 = 49, KEY_2 = 50, KEY_3 = 51,
KEY_4 = 52, KEY_5 = 53, KEY_6 = 54, KEY_7 = 55, KEY_8 = 56, KEY_9 = 57,
KEY_SPACE = 32, KEY_SYMBOL = 192, KEY_TAB = 9, KEY_BACKSPACE = 8, KEY_SHIFT = 16,
KEY_CONTROL = 17, KEY_ALT = 18, KEY_ESC = 27, KEY_META = 91, KEY_METARIGHT = 93,
KEY_CAPSLOCK = 20
VIEW.body.addEventListener('contextmenu', e => e.preventDefault())
let _usesOffset = false
VIEW.body.addEventListener('mousemove', e => {
if(e.offsetX || e.offsetY)_usesOffset = true
//Accurate to true device pixel
const x = _usesOffset ? e.offsetX : e.pageX - e.target.offsetLeft
const y = _usesOffset ? e.offsetY : e.pageY - e.target.offsetTop
VIEW.mx = x; VIEW.my = y
VIEW.mxa += e.movementX * px
VIEW.mya += e.movemenyY * px
})
VIEW.body.addEventListener('wheel', e => {
VIEW.wxa += e.deltaX * px
VIEW.wya += e.deltaY * px
e.preventDefault()
}, {passive: false})
VIEW.body.addEventListener('mousewheel', e => {
e.preventDefault()
}, {passive: false})
VIEW.addEventListener('pointerlockchange', console.log)
let px = round(devicePixelRatio)
let last = 0
onfocus = () => last = Date.now()
document.currentScript.remove()
onresize()
onfocus()
</script>
<script type="module">
import * as debug from './index.js'
function getLocalCoords({matrix: {a, b, c, d, e, f}}, mx, my){
//inverse + translatePoint
const u = a * d - b * c
const x = (mx * d - my * c + c * f - d * e) / u
const y = (my * a - mx * b - a * f - b * e) / u
return [x, y]
}
function updateControls(can){
[can.mx, can.my] = getLocalCoords(can, VIEW.mx, VIEW.my)
}
globalThis.T = 0
function frame(){
const now = Date.now()
T = performance.now() / 1000
px = round(devicePixelRatio)
for(const c of VIEW.canvases){
if(!VIEW.running)continue
if(c.fps < 0 || !c.lastRender){
c.lastRender = now
try{
updateControls(c)
c.render(min(0.1, (now - last)/1000))
}catch(e){
if(!c.exception)console.error('Error occurred while rendering canvas:\n%o', c.exception = e)
}
continue
}
const age = now - c.lastRender, ms = 1000 / c.fps
if(age < ms)continue
const dt = age - (age % ms)
try{
updateControls(c)
c.render(min(ms/200, dt/1000))
}catch(e){
if(!c.exception)console.error('Error occurred while rendering canvas:\n%o', c.exception = e)
}
c.lastRender += dt
}
last = now
let content = debug.default ? [debug.default] : []
for(const k in debug)if(k != 'default')content.push(k + ': ' + info(debug[k]))
layer.textContent = content.join('\n')
requestAnimationFrame(frame)
}
globalThis.info = function info(v){
switch(typeof v){
case "boolean": return v ? ' true' : 'false'
case "undefined": return ''
case "bigint": v = Number(v)
case "number": //Also for bigint
if(v === Infinity)return ' infinity'
if(v === -Infinity)return '-infinity'
if(v === NaN)return ' (NaN)'
let exp = floor(log10(abs(v)))
if(exp >= 9-(v<0))return (v/10**exp).toFixed(5 - (v<0) - (exp>=100) - (exp >= 10)) + 'e' + exp
else if(exp < -7 + (v<0))return (v/10**exp).toFixed(4 - (v<0) - (exp<=-100) - (exp <= -10)) + 'e' + exp
else return exp<0 ? v.toFixed(7 - (v<0)) : v.toPrecision(8 - (v<0))
case "string": return JSON.stringify(v)
case "object":
if(!v)return 'null'
let tag = v[Symbol.toStringTag] || 'Obj'
return `<${tag}>`
default: return '<unknown>'
}
}
frame()
</script>
</html>