vislite
Version:
灵活、快速、简单的数据可视化交互式跨端前端库
71 lines (68 loc) • 2.39 kB
JavaScript
/*!
* getWebGLContext of VISLite JavaScript Library v1.3.0
* git+https://github.com/oi-contrib/VISLite.git
*/
function getWebGLContext (canvas, scale, opts, mode) {
if (opts === void 0) { opts = {}; }
if (mode === void 0) { mode = "scaleToFill"; }
var names = ["experimental-webgl", "webkit-3d", "moz-webgl"];
var painter = canvas.getContext("webgl", opts);
for (var i = 0; i < names.length; i++) {
try {
painter = canvas.getContext(names[i], opts);
}
catch (e) { }
if (painter)
break;
}
if (!painter)
throw new Error('Non canvas or browser does not support webgl.');
var width = painter.canvas.width, height = painter.canvas.height;
var scaleX = scale, scaleY = scale;
if (mode == "aspectFit") {
if (width > height) {
scaleX *= height / width;
}
else if (height > width) {
scaleY *= width / height;
}
}
else if (mode == "aspectFill") {
if (width > height) {
scaleY *= width / height;
}
else if (height > width) {
scaleX *= height / width;
}
}
var viewWidth = width * scaleX;
var viewHeight = height * scaleY;
painter.viewport((width - viewWidth) * 0.5, (height - viewHeight) * 0.5, viewWidth, viewHeight);
painter.depthFunc(painter.LEQUAL);
painter.enable(painter.DEPTH_TEST);
return painter;
}
var index = (function (el, scale, mode) {
if (scale === void 0) { scale = 1; }
if (!el) {
throw new Error("VISLite getWebGLContext:The mount point requires an HTMLElement type but encountered null.");
}
var width = el.clientWidth, height = el.clientHeight;
var ViewCanvas;
var _el = el;
if (_el._vislite_canvas_) {
ViewCanvas = _el._vislite_canvas_;
}
else {
ViewCanvas = document.createElement('canvas');
el.appendChild(ViewCanvas);
_el._vislite_canvas_ = ViewCanvas;
el.setAttribute('vislite', 'WebGL');
}
ViewCanvas.style.width = width + "px";
ViewCanvas.setAttribute('width', width + "");
ViewCanvas.style.height = height + "px";
ViewCanvas.setAttribute('height', height + "");
return getWebGLContext(ViewCanvas, scale, {}, mode);
});
export { index as default };