mirror of
https://github.com/gpujs/gpu.js.git
synced 2026-01-18 16:04:10 +00:00
227 lines
5.9 KiB
JavaScript
227 lines
5.9 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
*
|
|
* @desc Reduced subset of Utils, used exclusively in gpu-core.js
|
|
* Various utility functions / snippets of code that GPU.JS uses internally.\
|
|
* This covers various snippets of code that is not entirely gpu.js specific (ie. may find uses elsewhere)
|
|
*
|
|
* Note that all methods in this class is 'static' by nature `UtilsCore.functionName()`
|
|
*
|
|
* @class UtilsCore
|
|
*
|
|
*/
|
|
|
|
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
|
|
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
|
|
var UtilsCore = function () {
|
|
function UtilsCore() {
|
|
_classCallCheck(this, UtilsCore);
|
|
}
|
|
|
|
_createClass(UtilsCore, null, [{
|
|
key: 'isCanvas',
|
|
|
|
|
|
/**
|
|
* @typedef {Object} webGlContext
|
|
*/
|
|
|
|
/**
|
|
* @typedef {Object} CanvasDOMObject
|
|
*/
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//
|
|
// Canvas validation and support
|
|
//
|
|
//-----------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @name isCanvas
|
|
* @static
|
|
* @function
|
|
* @memberOf UtilsCore
|
|
*
|
|
*
|
|
* @desc Return TRUE, on a valid DOM canvas object
|
|
*
|
|
* Note: This does just a VERY simply sanity check. And may give false positives.
|
|
*
|
|
* @param {CanvasDOMObject} canvasObj - Object to validate
|
|
*
|
|
* @returns {Boolean} TRUE if the object is a DOM canvas
|
|
*
|
|
*/
|
|
value: function isCanvas(canvasObj) {
|
|
return canvasObj !== null && canvasObj.nodeName && canvasObj.getContext && canvasObj.nodeName.toUpperCase() === 'CANVAS';
|
|
}
|
|
|
|
/**
|
|
* @name isCanvasSupported
|
|
* @function
|
|
* @static
|
|
* @memberOf UtilsCore
|
|
*
|
|
* @desc Return TRUE, if browser supports canvas
|
|
*
|
|
* @returns {Boolean} TRUE if browser supports canvas
|
|
*
|
|
*/
|
|
|
|
}, {
|
|
key: 'isCanvasSupported',
|
|
value: function isCanvasSupported() {
|
|
return _isCanvasSupported;
|
|
}
|
|
|
|
/**
|
|
* @name initCanvas
|
|
* @function
|
|
* @static
|
|
* @memberOf UtilsCore
|
|
*
|
|
* @desc Initiate and returns a canvas, for usage in init_webgl.
|
|
* Returns only if canvas is supported by browser.
|
|
*
|
|
* @returns {CanvasDOMObject} CanvasDOMObject if supported by browser, else null
|
|
*
|
|
*/
|
|
|
|
}, {
|
|
key: 'initCanvas',
|
|
value: function initCanvas() {
|
|
// Fail fast if browser previously detected no support
|
|
if (!_isCanvasSupported) {
|
|
return null;
|
|
}
|
|
|
|
// Create a new canvas DOM
|
|
var canvas = document.createElement('canvas');
|
|
|
|
// Default width and height, to fix webgl issue in safari
|
|
canvas.width = 2;
|
|
canvas.height = 2;
|
|
|
|
// Returns the canvas
|
|
return canvas;
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//
|
|
// Webgl validation and support
|
|
//
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
/**
|
|
*
|
|
* @name isWebGl
|
|
* @function
|
|
* @static
|
|
* @memberOf UtilsCore
|
|
*
|
|
* @desc Return TRUE, on a valid webGlContext object
|
|
*
|
|
* Note: This does just a VERY simply sanity check. And may give false positives.
|
|
*
|
|
* @param {webGlContext} webGlObj - Object to validate
|
|
*
|
|
* @returns {Boolean} TRUE if the object is a webGlContext object
|
|
*
|
|
*/
|
|
|
|
}, {
|
|
key: 'isWebGl',
|
|
value: function isWebGl(webGlObj) {
|
|
return webGlObj && typeof webGlObj.getExtension === 'function';
|
|
}
|
|
|
|
/**
|
|
* @name isWebGlSupported
|
|
* @function
|
|
* @static
|
|
* @memberOf UtilsCore
|
|
*
|
|
* @desc Return TRUE, if browser supports webgl
|
|
*
|
|
* @returns {Boolean} TRUE if browser supports webgl
|
|
*
|
|
*/
|
|
|
|
}, {
|
|
key: 'isWebGlSupported',
|
|
value: function isWebGlSupported() {
|
|
return _isWebGlSupported;
|
|
}
|
|
}, {
|
|
key: 'isWebGlDrawBuffersSupported',
|
|
value: function isWebGlDrawBuffersSupported() {
|
|
return _isWebGlDrawBuffersSupported;
|
|
}
|
|
|
|
// Default webgl options to use
|
|
|
|
}, {
|
|
key: 'initWebGlDefaultOptions',
|
|
value: function initWebGlDefaultOptions() {
|
|
return {
|
|
alpha: false,
|
|
depth: false,
|
|
antialias: false
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @name initWebGl
|
|
* @function
|
|
* @static
|
|
* @memberOf UtilsCore
|
|
*
|
|
* @desc Initiate and returns a webGl, from a canvas object
|
|
* Returns only if webGl is supported by browser.
|
|
*
|
|
* @param {CanvasDOMObject} canvasObj - Object to validate
|
|
*
|
|
* @returns {CanvasDOMObject} CanvasDOMObject if supported by browser, else null
|
|
*
|
|
*/
|
|
|
|
}, {
|
|
key: 'initWebGl',
|
|
value: function initWebGl(canvasObj) {
|
|
|
|
// First time setup, does the browser support check memorizer
|
|
if (typeof _isCanvasSupported !== 'undefined' || canvasObj === null) {
|
|
if (!_isCanvasSupported) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// Fail fast for invalid canvas object
|
|
if (!UtilsCore.isCanvas(canvasObj)) {
|
|
throw new Error('Invalid canvas object - ' + canvasObj);
|
|
}
|
|
|
|
// Create a new canvas DOM
|
|
return canvasObj.getContext('webgl2', UtilsCore.initWebGlDefaultOptions());
|
|
}
|
|
}]);
|
|
|
|
return UtilsCore;
|
|
}();
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//
|
|
// Canvas & Webgl validation and support constants
|
|
//
|
|
//-----------------------------------------------------------------------------
|
|
|
|
var _isCanvasSupported = typeof document !== 'undefined' ? UtilsCore.isCanvas(document.createElement('canvas')) : false;
|
|
var _testingWebGl = UtilsCore.initWebGl(UtilsCore.initCanvas());
|
|
var _isWebGlSupported = UtilsCore.isWebGl(_testingWebGl);
|
|
var _isWebGlDrawBuffersSupported = _isWebGlSupported;
|
|
|
|
module.exports = UtilsCore; |