forked from stackgl/headless-gl
-
Notifications
You must be signed in to change notification settings - Fork 5
/
browser_index.js
73 lines (63 loc) · 1.48 KB
/
browser_index.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
'use strict'
function createContext (width, height, options) {
width = width | 0
height = height | 0
if (!(width > 0 && height > 0)) {
return null
}
var canvas = document.createElement('canvas')
if (!canvas) {
return null
}
var gl
canvas.width = width
canvas.height = height
try {
gl = canvas.getContext('webgl', options)
} catch (e) {
try {
gl = canvas.getContext('experimental-webgl', options)
} catch (e) {
return null
}
}
var _getExtension = gl.getExtension
var extDestroy = {
destroy: function () {
var loseContext = _getExtension.call(gl, 'WEBGL_lose_context')
if (loseContext) {
loseContext.loseContext()
}
}
}
var extResize = {
resize: function (w, h) {
canvas.width = w
canvas.height = h
}
}
var _supportedExtensions = gl.getSupportedExtensions().slice()
_supportedExtensions.push(
'STACKGL_destroy_context',
'STACKGL_resize_drawingbuffer')
gl.getSupportedExtensions = function () {
return _supportedExtensions.slice()
}
gl.getExtension = function (extName) {
var name = extName.toLowerCase()
if (name === 'stackgl_resize_drawingbuffer') {
return extResize
}
if (name === 'stackgl_destroy_context') {
return extDestroy
}
return _getExtension.call(gl, extName)
}
Object.defineProperty(gl, 'canvas', {
get: function () {
return null
}
})
return gl || null
}
module.exports = createContext