-
Notifications
You must be signed in to change notification settings - Fork 7
/
multi.js
175 lines (141 loc) · 3.91 KB
/
multi.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
const createREGL = require('regl')
module.exports = function createMultiplexor (inputs) {
var reglInput = {}
if (inputs) {
Object.keys(inputs).forEach(function (input) {
reglInput[input] = inputs[input]
})
}
var pixelRatio = reglInput.pixelRatio || window.devicePixelRatio
reglInput.pixelRatio = pixelRatio
var canvas = document.createElement('canvas')
var canvasStyle = canvas.style
canvasStyle.position = 'fixed'
canvasStyle.left =
canvasStyle.top = '0px'
canvasStyle.width =
canvasStyle.height = '100%'
canvasStyle['pointer-events'] = 'none'
canvasStyle['touch-action'] = 'none'
canvasStyle['z-index'] = '1000'
function resize () {
canvas.width = pixelRatio * window.innerWidth
canvas.height = pixelRatio * window.innerHeight
}
resize()
window.addEventListener('resize', resize, false)
document.body.appendChild(canvas)
reglInput.canvas = canvas
delete reglInput.gl
delete reglInput.container
var regl = createREGL(reglInput)
var subcontexts = []
var viewBox = {
x: 0,
y: 0,
width: 0,
height: 0
}
function createSubContext (input) {
var element
if (typeof input === 'object' && input) {
if (typeof input.getBoundingClientRect === 'function') {
element = input
} else if (input.element) {
element = input.element
}
} else if (typeof input === 'string') {
element = document.querySelector(element)
}
if (!element) {
element = document.body
}
var subcontext = {
tick: 0,
element: element,
callbacks: []
}
subcontexts.push(subcontext)
function wrapBox (boxDesc) {
return boxDesc
}
function subREGL (options) {
if ('viewport' in options) {
options.viewport = wrapBox(options.viewport)
}
if ('scissor' in options) {
if ('box' in options) {
options.scissor.box = wrapBox(options.scissor.box)
}
if ('enable' in options) {
options.scissor.box = true
}
}
return regl.apply(regl, Array.prototype.slice.call(arguments))
}
Object.keys(regl).forEach(function (option) {
subREGL[option] = regl[option]
})
subREGL.frame = function subFrame (cb) {
subcontext.callbacks.push(cb)
return {
cancel: function () {
subcontext.callbacks.splice(subcontext.callbacks.indexOf(cb), 1)
}
}
}
subREGL.destroy = function () {
subcontexts.splice(subcontexts.indexOf(subcontext), 1)
}
subREGL.container = element
return subREGL
}
createSubContext.destroy = function () {
regl.destroy()
}
createSubContext.regl = regl
var setViewport = regl({
context: {
tick: regl.prop('subcontext.tick')
},
viewport: regl.prop('viewbox'),
scissor: {
enable: true,
box: regl.prop('scissorbox')
}
})
function executeCallbacks (context, props) {
var callbacks = props.subcontext.callbacks
for (var i = 0; i < callbacks.length; ++i) {
(callbacks[i])(context)
}
}
regl.frame(function (context) {
regl.clear({
color: [0, 0, 0, 0]
})
var width = window.innerWidth
var height = window.innerHeight
var pixelRatio = context.pixelRatio
for (var i = 0; i < subcontexts.length; ++i) {
var sc = subcontexts[i]
var rect = sc.element.getBoundingClientRect()
if (rect.right < 0 || rect.bottom < 0 ||
width < rect.left || height < rect.top) {
continue
}
viewBox.x = pixelRatio * (rect.left)
viewBox.y = pixelRatio * (height - rect.bottom)
viewBox.width = pixelRatio * (rect.right - rect.left)
viewBox.height = pixelRatio * (rect.bottom - rect.top)
setViewport({
subcontext: sc,
viewbox: viewBox,
scissorbox: viewBox
}, executeCallbacks)
sc.tick += 1
}
})
createSubContext.canvas = canvas
return createSubContext
}