regl-web-components 0.0.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "regl-web-components",
3
- "version": "0.0.1",
3
+ "version": "0.2.0",
4
4
  "author": {
5
5
  "name": "rubén rodríguez",
6
6
  "url": "https://rubenrodriguez.me"
@@ -11,13 +11,21 @@
11
11
  },
12
12
  "exports": {
13
13
  ".": "./dist/all.js",
14
+ "./cjs": "./dist/all.cjs",
14
15
  "./define": "./dist/all-define.js",
16
+ "./define.cjs": "./dist/all-define.cjs",
15
17
  "./regl-element": "./dist/regl-element.js",
18
+ "./regl-element.cjs": "./dist/regl-element.cjs",
16
19
  "./regl-element/define": "./dist/regl-element-define.js",
20
+ "./regl-element/define.cjs": "./dist/regl-element-define.cjs",
17
21
  "./regl-multi-element": "./dist/regl-multi-element.js",
22
+ "./regl-multi-element.cjs": "./dist/regl-multi-element.cjs",
18
23
  "./regl-multi-element/define": "./dist/regl-multi-element-define.js",
24
+ "./regl-multi-element/define.cjs": "./dist/regl-multi-element-define.cjs",
19
25
  "./regl-multi-root": "./dist/regl-multi-root.js",
20
- "./regl-multi-root/define": "./dist/regl-multi-root-define.js"
26
+ "./regl-multi-root.cjs": "./dist/regl-multi-root.cjs",
27
+ "./regl-multi-root/define": "./dist/regl-multi-root-define.js",
28
+ "./regl-multi-root/define.cjs": "./dist/regl-multi-root-define.cjs"
21
29
  },
22
30
  "devDependencies": {
23
31
  "@types/web": "^0.0.297",
package/src/all-define.ts DELETED
@@ -1,7 +0,0 @@
1
- import { define as reDefine } from '@/src/regl-element'
2
- import { define as rmeDefine } from '@/src/regl-multi-element'
3
- import { define as rmrDefine } from '@/src/regl-multi-root'
4
-
5
- reDefine()
6
- rmeDefine()
7
- rmrDefine()
package/src/all.ts DELETED
@@ -1,3 +0,0 @@
1
- export { ReglElement } from '@/src/regl-element'
2
- export { ReglMultiElement } from '@/src/regl-multi-element'
3
- export { ReglMultiRoot } from '@/src/regl-multi-root'
@@ -1,142 +0,0 @@
1
- // fork of https://www.npmjs.com/package/deferred-regl
2
- import type { Regl } from 'regl'
3
-
4
- export interface DeferredRegl extends Regl {
5
- setRegl : (r : Regl) => void
6
- }
7
-
8
- export default deferredRegl
9
- export function deferredRegl () : DeferredRegl {
10
- var regl = null
11
- var queue = []
12
- var def = dfn('()')
13
- unset()
14
- def.setRegl = function (r) {
15
- regl = r
16
- if (!r) return unset()
17
- for (var i = 0; i < queue.length; i++) {
18
- queue[i](regl)
19
- }
20
- queue = null
21
- def.frame = r.frame
22
- def.draw = r.draw
23
- def.poll = r.poll
24
- def.clear = r.clear
25
- def.buffer = r.buffer
26
- def.texture = r.texture
27
- def.elements = r.elements
28
- def.framebuffer = r.framebuffer
29
- def.framebufferCube = r.framebufferCube
30
- def.renderbuffer = r.renderbuffer
31
- def.cube = r.cube
32
- def.read = r.read
33
- def.hasExtension = r.hasExtension
34
- def.limits = r.limits
35
- def.stats = r.limits
36
- def.now = r.now
37
- def.destroy = r.destroy
38
- def.on = r.on
39
- }
40
- return def
41
-
42
- function unset () {
43
- if (!queue) queue = []
44
- def.frame = function (cb) { queue.push(function (r) { r.frame(cb) }) }
45
- def.draw = function (cb) { queue.push(function (r) { r.draw(cb) }) }
46
- def.poll = function () { queue.push(function (r) { r.poll() }) }
47
- def.clear = function (opts) { queue.push(function (r) { r.clear(opts) }) }
48
- def.prop = function (key) {
49
- return function (context, props) {
50
- if (!falsy(props[key])) {
51
- return props[key]
52
- } else {
53
- // missing key could be speical case unrolled uniform prop
54
- // https://github.com/regl-project/regl/issues/258
55
- // https://github.com/regl-project/regl/issues/373
56
- var matches = key.match(/(?<prop>.+)\[(?<index>.+)\]/i)
57
- if (matches) {
58
- return props[matches.groups.prop][matches.groups.index]
59
- }
60
- }
61
- }
62
- }
63
- def.props = def.prop
64
- def.context = function (key) {
65
- return function (context, props) { return context[key] }
66
- }
67
- def['this'] = function (key) {
68
- return function (context, props) { return this[key] }
69
- }
70
- def.buffer = dfn('buffer')
71
- def.texture = dfn('texture')
72
- def.elements = dfn('elements')
73
- def.framebuffer = dfnx('framebuffer',['resize','use'])
74
- def.framebufferCube = dfn('framebufferCube')
75
- def.renderbuffer = dfn('renderbuffer')
76
- def.cube = dfn('cube')
77
- def.read = function () {}
78
- def.hasExtension = function () {}
79
- def.limits = function () {}
80
- def.stats = function () {}
81
- def.now = function () { return 0 }
82
- def.destroy = function () { queue.push(function (r) { r.destroy() }) }
83
- def.on = function (name, f) { queue.push(function (r) { r.on(name,f) }) }
84
- }
85
- function dfn (key) {
86
- return function (opts) {
87
- if (key === '()' && regl) return regl(opts)
88
- else if (regl) return regl[key](opts)
89
-
90
- var f = null
91
- if (key === '()') {
92
- queue.push(function (r) { f = r(opts) })
93
- } else {
94
- queue.push(function (r) { f = r[key](opts) })
95
- }
96
- return function () {
97
- var args = arguments
98
- if (!falsy(f)) {
99
- if (key === '()') f.apply(null,args)
100
- else return f
101
- } else {
102
- queue.push(function (r) { f.apply(null,args) })
103
- }
104
- }
105
- }
106
- }
107
- function dfnx (key, methods) {
108
- return function (opts) {
109
- if (key === '()' && regl) return regl(opts)
110
- else if (regl) return regl[key](opts)
111
-
112
- var f = null
113
- if (key === '()') {
114
- queue.push(function (r) { f = r(opts) })
115
- } else {
116
- queue.push(function (r) { f = r[key](opts) })
117
- }
118
- var r = function () {
119
- var args = arguments
120
- if (!falsy(f)) {
121
- if (key === '()') f.apply(null,args)
122
- else return f
123
- } else {
124
- queue.push(function (r) { f.apply(null,args) })
125
- }
126
- }
127
- for (var i = 0; i < methods.length; i++) {
128
- var m = methods[i]
129
- r[m] = function () {
130
- var args = arguments
131
- if (!falsy(f)) return f[m].apply(f,args)
132
- else queue.push(function () { f[m].apply(f,args) })
133
- }
134
- }
135
- return r
136
- }
137
- }
138
- }
139
-
140
- function falsy (x) {
141
- return x === null || x === undefined
142
- }
package/src/multi-regl.ts DELETED
@@ -1,283 +0,0 @@
1
- // fork of https://github.com/regl-project/multi-regl
2
- import { DefaultContext, InitializationOptions, Regl } from "regl"
3
-
4
- export type CreateSubContextInput = HTMLElement |
5
- { element : HTMLElement } |
6
- string |
7
- undefined
8
-
9
- export interface SubREGL extends Regl {
10
- container: HTMLElement,
11
- }
12
-
13
- export type SubContext = {
14
- tick: number,
15
- element: HTMLElement,
16
- callbacks: ((c: DefaultContext) => void)[],
17
- intersectionObserverEntry : null | IntersectionObserverEntry,
18
- }
19
-
20
- export type CreateSubContextFn = (i: CreateSubContextInput) => SubREGL
21
- export interface CreateSubContext {
22
- CreateSubContextFn,
23
- destroy: () => void,
24
- regl: Regl,
25
- subcontexts: SubContext[],
26
- }
27
-
28
- export type CreateMultiplexor = ((
29
- createRegl : (o: InitializationOptions) => Regl,
30
- canvas : HTMLElement,
31
- options : InitializationOptions
32
- ) => CreateSubContext)
33
-
34
- export default createMultiplexor
35
-
36
- export function createMultiplexor (createREGL, canvas, inputs) {
37
- var reglInput : InitializationOptions = {}
38
- if (inputs) {
39
- Object.keys(inputs).forEach(function (input) {
40
- reglInput[input] = inputs[input]
41
- })
42
- }
43
-
44
- var pixelRatio = reglInput.pixelRatio || window.devicePixelRatio
45
- reglInput.pixelRatio = pixelRatio
46
-
47
- var canvasStyle = canvas.style
48
- canvasStyle.position = 'var(--multi-regl-canvas-position, fixed)'
49
- canvasStyle.left = 'var(--multi-regl-canvas-left, 0px)'
50
- canvasStyle.top = 'var(--multi-regl-canvas-top, 0px)'
51
- canvasStyle.width = 'var(--multi-regl-canvas-width, 100%)'
52
- canvasStyle.height = 'var(--multi-regl-canvas-height, 100%)'
53
- canvasStyle['pointer-events'] = 'var(--multi-regl-canvas-pointer-events, none)'
54
- canvasStyle['touch-action'] = 'var(--multi-regl-canvas-touch-action, none)'
55
- canvasStyle['z-index'] = 'var(--multi-regl-canvas-z-index, 1000)'
56
-
57
- var subcontexts : SubContext[] = []
58
-
59
- const intersectionObserver = new IntersectionObserver(processIntersectionObserverEntries)
60
-
61
- function processIntersectionObserverEntries (entries) {
62
- for (const entry of entries) {
63
- const subcontext = subcontexts.find(sc => sc.element === entry.target)
64
- if (!subcontext) continue
65
- subcontext.intersectionObserverEntry = entry
66
- }
67
- }
68
-
69
- function resize () {
70
- canvas.width = pixelRatio * window.innerWidth
71
- canvas.height = pixelRatio * window.innerHeight
72
- if (!setRAF && regl) regl.draw(frame)
73
- }
74
-
75
- resize()
76
-
77
- window.addEventListener('resize', resize, false)
78
- window.addEventListener('scroll', function () {
79
- if (!setRAF) regl.draw(frame)
80
- }, false)
81
-
82
- reglInput.canvas = canvas
83
- delete reglInput.gl
84
- delete reglInput.container
85
-
86
- var regl = createREGL(reglInput)
87
-
88
- function addSubcontext (sc) {
89
- subcontexts.push(sc)
90
- intersectionObserver.observe(sc.element)
91
- }
92
-
93
- function removeSubcontext (sc) {
94
- subcontexts.splice(subcontexts.indexOf(sc), 1)
95
- intersectionObserver.unobserve(sc.element)
96
- }
97
-
98
- var viewBox = {
99
- x: 0,
100
- y: 0,
101
- width: 0,
102
- height: 0
103
- }
104
-
105
- function createSubContext (input : CreateSubContextInput) : SubREGL {
106
- var element
107
- if (typeof input === 'object' && input) {
108
- if (typeof (input as HTMLElement).getBoundingClientRect === 'function') {
109
- element = input
110
- } else if (input.element) {
111
- element = input.element
112
- }
113
- } else if (typeof input === 'string') {
114
- element = document.querySelector(element)
115
- }
116
- if (!element) {
117
- element = document.body
118
- }
119
-
120
- var subcontext : SubContext = {
121
- tick: 0,
122
- element: element,
123
- callbacks: [],
124
- intersectionObserverEntry: null,
125
- }
126
-
127
- addSubcontext(subcontext)
128
-
129
- function wrapBox (boxDesc) {
130
- return boxDesc
131
- }
132
-
133
- var scheduled = false
134
- function schedule (cb) {
135
- subcontext.callbacks.push(cb)
136
- if (!scheduled) {
137
- scheduled = true
138
- window.requestAnimationFrame(draw)
139
- }
140
- function draw () {
141
- regl.draw(frame)
142
- scheduled = false
143
- }
144
- }
145
-
146
- function subREGL (options) : SubREGL {
147
- if ('viewport' in options) {
148
- options.viewport = wrapBox(options.viewport)
149
- }
150
- if ('scissor' in options) {
151
- if ('box' in options) {
152
- options.scissor.box = wrapBox(options.scissor.box)
153
- }
154
- if ('enable' in options) {
155
- options.scissor.box = true
156
- }
157
- }
158
- var draw = regl.apply(regl, Array.prototype.slice.call(arguments))
159
- if (!setRAF) {
160
- return function () {
161
- if (setRAF) return draw.apply(this, arguments)
162
- var args = arguments
163
- schedule(function () {
164
- draw.apply(null, args)
165
- })
166
- }
167
- } else return draw
168
- }
169
-
170
- Object.keys(regl).forEach(function (option) {
171
- if (option === 'clear') {
172
- subREGL.clear = function (opts) {
173
- if (opts && opts.framebuffer) {
174
- if (setRAF) return clear.apply(this,arguments)
175
- var args = arguments
176
- schedule(function () {
177
- regl.clear.apply(null, args)
178
- })
179
- } else {
180
- if (setRAF) return regl[option].apply(this, arguments)
181
- subcontext.callbacks = []
182
- var args = arguments
183
- schedule(function () {
184
- regl.clear.apply(null, args)
185
- })
186
- }
187
- }
188
- } else if (option === 'draw') {
189
- subREGL.draw = function () {
190
- if (setRAF) return regl[option].apply(this, arguments)
191
- var args = arguments
192
- schedule(function () {
193
- regl.draw.apply(null, args)
194
- })
195
- }
196
- } else subREGL[option] = regl[option]
197
- })
198
-
199
- subREGL.frame = function subFrame (cb) {
200
- setRAF = true
201
- subcontext.callbacks.push(cb)
202
- regl.frame(frame)
203
- return {
204
- cancel: function () {
205
- subcontext.callbacks.splice(subcontext.callbacks.indexOf(cb), 1)
206
- }
207
- }
208
- }
209
-
210
- subREGL.destroy = function () {
211
- removeSubcontext(subcontext)
212
- }
213
-
214
- subREGL.container = element
215
-
216
- return subREGL
217
- }
218
-
219
- createSubContext.destroy = function () {
220
- regl.destroy()
221
- intersectionObserver.disconnect()
222
- }
223
-
224
- createSubContext.regl = regl
225
-
226
- var setViewport = regl({
227
- context: {
228
- tick: regl.prop('subcontext.tick')
229
- },
230
-
231
- viewport: regl.prop('viewbox'),
232
-
233
- scissor: {
234
- enable: true,
235
- box: regl.prop('scissorbox')
236
- }
237
- })
238
-
239
- function executeCallbacks (context, props) {
240
- var callbacks = props.subcontext.callbacks
241
- for (var i = 0; i < callbacks.length; ++i) {
242
- callbacks[i](context)
243
- }
244
- }
245
-
246
- var setRAF = false
247
- function frame (context) {
248
- regl.clear({
249
- color: [0, 0, 0, 0]
250
- })
251
-
252
- var width = window.innerWidth
253
- var height = window.innerHeight
254
-
255
- var pixelRatio = context.pixelRatio
256
- for (var i = 0; i < subcontexts.length; ++i) {
257
- var sc = subcontexts[i]
258
-
259
- if (!sc.intersectionObserverEntry?.isIntersecting) {
260
- continue
261
- }
262
-
263
- var rect = sc.element.getBoundingClientRect()
264
-
265
- viewBox.x = pixelRatio * (rect.left)
266
- viewBox.y = pixelRatio * (height - rect.bottom)
267
- viewBox.width = pixelRatio * (rect.right - rect.left)
268
- viewBox.height = pixelRatio * (rect.bottom - rect.top)
269
-
270
- setViewport({
271
- subcontext: sc,
272
- viewbox: viewBox,
273
- scissorbox: viewBox
274
- }, executeCallbacks)
275
-
276
- sc.tick += 1
277
- }
278
- }
279
-
280
- createSubContext.subcontexts = subcontexts
281
-
282
- return createSubContext
283
- }
@@ -1,2 +0,0 @@
1
- import { define } from '@/src/regl-element'
2
- define()
@@ -1,31 +0,0 @@
1
- import createRegl from 'regl'
2
- import type {
3
- InitializationOptions,
4
- Regl,
5
- } from 'regl'
6
- import reglWcInitializationOptions from '@/src/regl-wc-initialization-options'
7
-
8
- export class ReglElement extends HTMLElement {
9
- regl : Regl
10
-
11
- get reglInitializationOptions () : InitializationOptions {
12
- return {}
13
- }
14
-
15
- createRegl () : void {
16
- const options = reglWcInitializationOptions(this)
17
- this.regl = createRegl(options)
18
- }
19
- connectedCallback () : void {
20
- this.createRegl()
21
- }
22
- disconnectedCallback () {
23
- this?.regl?.destroy()
24
- }
25
- }
26
-
27
- export const define = (tag='regl-element') => {
28
- if (!customElements.get(tag)) {
29
- customElements.define(tag, ReglElemnt)
30
- }
31
- }
@@ -1,2 +0,0 @@
1
- import { define } from '@/src/regl-multi-element'
2
- define()
@@ -1,69 +0,0 @@
1
- import defregl from '@/src/deferred-regl'
2
- import { MULTI_ROOT_EVENTS } from "@/src/regl-multi-root"
3
-
4
- import type { DeferredRegl } from '@/src/deferred-regl'
5
- import type {
6
- CreateSubContext,
7
- SubREGL,
8
- } from '@/src/multi-regl'
9
- import type {
10
- MreglReadyEvent,
11
- ReglMultiRootElement,
12
- } from '@/src/regl-multi-root'
13
-
14
- export class ReglMultiElement extends HTMLElement {
15
- regl : DeferredRegl
16
-
17
- _mregl : null | CreateSubContext
18
- _regl : null | SubREGL
19
- _root : null | ReglMultiRootElement
20
- _rootTag : string
21
-
22
- constructor ({ rootTag='regl-multi-root' }={}) {
23
- super()
24
- this._mregl = null
25
- this._root = null
26
- this._rootTag = rootTag
27
- this.regl = defregl()
28
- }
29
-
30
- _setMregl (mregl) {
31
- this._mregl = mregl
32
- this._regl = mregl(this)
33
- this.regl.setRegl(this._regl)
34
- }
35
-
36
- connectedCallback () {
37
- const rootId = this.getAttribute('root') || ''
38
- this._root = document.getElementById(rootId) ||
39
- document.querySelector(this._rootTag)
40
-
41
- if (!this._root){
42
- throw new Error('\`root\` attribute must be set to the ID of the regl-multi-root element.')
43
- }
44
-
45
- if (this._root?.mregl) {
46
- // console.log('connected-set')
47
- this._setMregl(this._root.mregl)
48
- }
49
- else {
50
- this._root.addEventListener(MULTI_ROOT_EVENTS.MREGL_READY, (event : MreglReadyEveent) => {
51
- // console.log('evented-set')
52
- this._setMregl(event.detail.mregl)
53
- })
54
- }
55
- }
56
-
57
- disconnectedCallback() {
58
- this._regl?.destroy()
59
- this._regl = null
60
- this.regl.setRegl(null)
61
- this._root = null
62
- }
63
- }
64
-
65
- export function define (tag='regl-multi-element') {
66
- if (!customElements.get(tag)) {
67
- customElements.define(tag, ReglMultiElement)
68
- }
69
- }
@@ -1,2 +0,0 @@
1
- import { define } from "@/src/regl-multi-root"
2
- define()
@@ -1,80 +0,0 @@
1
- import createRegl from 'regl'
2
- import mregl from '@/src/multi-regl'
3
- import reglWcInitializationOptions from '@/src/regl-wc-initialization-options'
4
-
5
- import type {
6
- InitializationOptions,
7
- Regl,
8
- } from 'regl'
9
- import type {
10
- CreateSubContext,
11
- CreateSubContextInput,
12
- SubREGL,
13
- } from '@/src/multi-regl'
14
-
15
- export const MULTI_ROOT_EVENTS = {
16
- MREGL_READY: 'multi-regl-root:mregl-ready'
17
- }
18
-
19
- export type MreglReadyEvent = CustomEvent<{ mregl: CreateSubContext }>
20
-
21
- export interface ReglMultiRootElement extends HTMLElement {
22
- mregl : null | CreateSubContext
23
- }
24
-
25
- export class ReglMultiRoot extends HTMLElement {
26
- canvas : null | HTMLCanvasElement
27
-
28
- _createRegl : (o : InitializationOptions) => Regl
29
- _opts : InitializationOptions
30
- mregl : null | CreateSubContext
31
-
32
- get reglInitializationOptions () : InitializationOptions {
33
- return {}
34
- }
35
-
36
- constructor (_createRegl=createRegl, opts={}) {
37
- super()
38
- this.mregl = null
39
- this._createRegl = _createRegl
40
- this._opts = opts
41
- }
42
-
43
- createMRegl () {
44
- const options = {
45
- ...this._opts,
46
- ...reglWcInitializationOptions(this),
47
- }
48
- if (options?.canvas) {
49
- this.canvas = options.canvas
50
- }
51
- else {
52
- this.canvas = document.createElement('canvas')
53
- this.appendChild(this.canvas)
54
- }
55
- this.mregl = mregl(this._createRegl, this.canvas, options)
56
- this.dispatchEvent(new CustomEvent(MULTI_ROOT_EVENTS.MREGL_READY, {
57
- detail: {
58
- mregl: this.mregl,
59
- },
60
- bubbles: true,
61
- composed: true,
62
- }))
63
- }
64
-
65
- connectedCallback () {
66
- this.createMRegl()
67
- }
68
-
69
- disconnectedCallback() {
70
- this.mregl?.destroy()
71
- this.mregl = null
72
- this.canvas = null
73
- }
74
- }
75
-
76
- export function define (tag='regl-multi-root') {
77
- if (!customElements.get(tag)) {
78
- customElements.define(tag, ReglMultiRoot)
79
- }
80
- }