forked from iewebgl/iewebgl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webglhelper.js
173 lines (148 loc) · 5.95 KB
/
webglhelper.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
/**********************************************************
IEWebGL support routines
You can copy, use, modify, distribute this file.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
**********************************************************/
var WebGLHelper = {
'pluginInstallerURL': 'http://localhost/iewebgl.cab',
'currentVersion': "#Version=1,0,1,0",
'manualLoadScreen': {
'innerHTML':
'<div style="width:100%;height:100%;border-width:1px;border-style:solid">' +
'<div style="height:50%"></div>' +
'<p style="text-align:center">' +
'Please get IEWebGL plugin <a href="http://iewebgl.com/">here</a> to see 3D.' +
'</p>' +
'</div>'
},
'autoLoadScreen': {
'innerHTML':
'<div style="height:50%; text-align:center"></div>' +
'<div style="text-align:center">' +
'Please wait, while IEWebGL is loading for the first time.<br/>' +
'<img src="http://iewebgl.com/progress.gif">' +
'</div>',
'cssText': 'position:absolute;top:0;left:0;width:100%;height:100%;border-style:solid;border-width:1px;background-color:#FFFFFF;',
'className': undefined
},
'IsIE': function () {
return navigator.userAgent.indexOf("MSIE") >= 0;
},
'GetGLContext': function (cnv, attributes) {
var ctxNames = ["webgl", "experimental-webgl"];
var glCtx = null;
for (var i = 0; i < ctxNames.length && glCtx == null; ++i) {
try {
glCtx = cnv.getContext(ctxNames[i], attributes);
}
catch (e) { }
}
return glCtx;
},
'CreateNativeCanvas': function (element, id, replace, okHandler, failHandler) {
try {
var cnv = document.createElement("canvas");
if (replace) {
if (element.attributes.width) cnv.width = element.attributes.width.value;
if (element.attributes.height) cnv.height = element.attributes.height.value;
if (element.style.cssText) cnv.style.cssText = element.style.cssText;
element.parentNode.replaceChild(cnv, element);
}
else {
element.appendChild(cnv);
}
cnv.innerHTML = "Your browser does not support <canvas> tag.";
cnv.id = id;
if (okHandler) {
okHandler(cnv, id);
}
return cnv;
}
catch(e) {
if (failHandler) {
failHandler(null, id);
}
return null;
}
},
'CreatePluginCanvas': function (element, id, replace, okHandler, failHandler) {
var container = document.createElement("div");
container.style.cssText = "position:relative;width:100%;height:100%";
var obj = document.createElement("object");
if (replace) {
if (element.attributes.width) obj.width = element.attributes.width.value;
if (element.attributes.height) obj.height = element.attributes.height.value;
if (element.style.cssText) obj.style.cssText = element.style.cssText;
element.parentNode.replaceChild(container, element);
}
else {
element.appendChild(container);
}
container.appendChild(obj);
try {
obj.innerHTML = WebGLHelper.manualLoadScreen.innerHTML;
}
catch (e) { /* IE8 workaround */
obj.altHtml = WebGLHelper.manualLoadScreen.innerHTML;
}
var loadTimeOverlay = document.createElement("div");
loadTimeOverlay.innerHTML = WebGLHelper.autoLoadScreen.innerHTML;
if (WebGLHelper.autoLoadScreen.className == undefined) {
loadTimeOverlay.style.cssText = WebGLHelper.autoLoadScreen.cssText;
}
else {
loadTimeOverlay["class"] = WebGLHelper.autoLoadScreen.className;
}
container.appendChild(loadTimeOverlay);
var showPlugin = function () {
if (loadTimeOverlay.parentNode) {
loadTimeOverlay.parentNode.removeChild(loadTimeOverlay);
}
obj.style.visibility = "visible";
};
var errorHandler = function() {
showPlugin();
obj.onreadystatechange = null;
if (failHandler)
failHandler(null, id);
}
var successHandler = function() {
showPlugin();
if (okHandler)
okHandler(obj, id);
}
obj.style.visibility = "hidden";
obj.onreadystatechange = successHandler;
obj.onerror = errorHandler;
obj.id = id;
obj.codeBase = WebGLHelper.pluginInstallerURL + WebGLHelper.currentVersion;
obj.type = "application/x-webgl";
return obj;
},
'CreateGLCanvas': function (el, id, replace, okHandler, failHandler) {
if (WebGLHelper.IsIE()) {
var usePlugin;
try {
usePlugin = WebGLRenderingContext.hasOwnProperty('iewebgl');
} catch (e) {
usePlugin = true;
}
if (usePlugin) {
return WebGLHelper.CreatePluginCanvas(el, id, replace, okHandler, failHandler);
}
else {
return WebGLHelper.CreateNativeCanvas(el, id, replace, okHandler, failHandler);
}
}
else {
return WebGLHelper.CreateNativeCanvas(el, id, replace, okHandler, failHandler);
}
},
'CreateGLCanvasInline': function (id, okHandler, failHandler) {
var placeHolder = document.getElementById("WebGLCanvasCreationScript");
WebGLHelper.CreateGLCanvas(placeHolder, id, true, okHandler, failHandler);
}
}