@antv/g-mobile-webgl
Version:
A renderer implemented by WebGL1/2 in mobile environment
239 lines (227 loc) • 8.86 kB
JavaScript
/*!
* @antv/g-mobile-webgl
* @description A renderer implemented by WebGL1/2 in mobile environment
* @version 1.0.44
* @date 2/27/2025, 8:28:41 AM
* @author AntVis
* @docs https://g.antv.antgroup.com/
*/
;
var _createClass = require('@babel/runtime/helpers/createClass');
var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
var _callSuper = require('@babel/runtime/helpers/callSuper');
var _inherits = require('@babel/runtime/helpers/inherits');
var gLite = require('@antv/g-lite');
var DeviceRenderer = require('@antv/g-plugin-device-renderer');
var DragDropEvent = require('@antv/g-plugin-dragndrop');
var HTMLRenderer = require('@antv/g-plugin-html-renderer');
var ImageLoader = require('@antv/g-plugin-image-loader');
var DomInteraction = require('@antv/g-plugin-mobile-interaction');
var GesturePlugin = require('@antv/g-plugin-gesture');
var util = require('@antv/util');
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
var gDeviceApi = require('@antv/g-device-api');
var _regeneratorRuntime = require('@babel/runtime/helpers/regeneratorRuntime');
var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator');
function _interopNamespaceDefault(e) {
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n.default = e;
return Object.freeze(n);
}
var DeviceRenderer__namespace = /*#__PURE__*/_interopNamespaceDefault(DeviceRenderer);
var DragDropEvent__namespace = /*#__PURE__*/_interopNamespaceDefault(DragDropEvent);
var HTMLRenderer__namespace = /*#__PURE__*/_interopNamespaceDefault(HTMLRenderer);
var ImageLoader__namespace = /*#__PURE__*/_interopNamespaceDefault(ImageLoader);
var DomInteraction__namespace = /*#__PURE__*/_interopNamespaceDefault(DomInteraction);
var GesturePlugin__namespace = /*#__PURE__*/_interopNamespaceDefault(GesturePlugin);
function isCanvasElement(el) {
if (!el || typeof el !== 'object') return false;
if (el.nodeType === 1 && el.nodeName) {
// HTMLCanvasElement
return true;
}
// CanvasElement
return !!el.isCanvasElement;
}
var WebGLContextService = /*#__PURE__*/function () {
function WebGLContextService(context) {
_classCallCheck(this, WebGLContextService);
this.canvasConfig = context.config;
// @ts-ignore
this.deviceRendererPlugin = context.deviceRendererPlugin;
}
return _createClass(WebGLContextService, [{
key: "init",
value: function () {
var _init = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
var _this$canvasConfig, canvas, devicePixelRatio, dpr;
return _regeneratorRuntime().wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
_this$canvasConfig = this.canvasConfig, canvas = _this$canvasConfig.canvas, devicePixelRatio = _this$canvasConfig.devicePixelRatio;
this.$canvas = canvas;
// 实际获取到小程序环境的上下文
this.context = this.$canvas.getContext('webgl');
// use user-defined dpr first
dpr = devicePixelRatio || 1;
dpr = dpr >= 1 ? Math.ceil(dpr) : 1;
this.dpr = dpr;
this.resize(this.canvasConfig.width, this.canvasConfig.height);
case 7:
case "end":
return _context.stop();
}
}, _callee, this);
}));
function init() {
return _init.apply(this, arguments);
}
return init;
}()
}, {
key: "getContext",
value: function getContext() {
return this.context;
}
}, {
key: "getDomElement",
value: function getDomElement() {
return this.$canvas;
}
}, {
key: "getDPR",
value: function getDPR() {
return this.dpr;
}
}, {
key: "getBoundingClientRect",
value: function getBoundingClientRect() {
if (this.$canvas.getBoundingClientRect) {
return this.$canvas.getBoundingClientRect();
}
}
}, {
key: "destroy",
value: function destroy() {
// TODO: 小程序环境销毁 context
this.context = null;
this.$canvas = null;
}
}, {
key: "resize",
value: function resize(width, height) {
var pixelRatio = devicePixelRatio;
var canvasDOM = this.$canvas; // HTMLCanvasElement or canvasElement
// 浏览器环境设置style样式
// @ts-ignore 使用style功能判断不使用类型判断更准确
if (canvasDOM.style) {
// @ts-ignore
canvasDOM.style.width = "".concat(width, "px");
// @ts-ignore
canvasDOM.style.height = "".concat(height, "px");
}
if (isCanvasElement(canvasDOM)) {
canvasDOM.width = width * pixelRatio;
canvasDOM.height = height * pixelRatio;
// if (pixelRatio !== 1) {
// this.context.scale(pixelRatio, pixelRatio);
// }
}
}
}, {
key: "applyCursorStyle",
value: function applyCursorStyle(cursor) {
// 小程序环境无需设置鼠标样式
}
}, {
key: "toDataURL",
value: function () {
var _toDataURL = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(options) {
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
return _context2.abrupt("return", this.deviceRendererPlugin.toDataURL(options));
case 1:
case "end":
return _context2.stop();
}
}, _callee2, this);
}));
function toDataURL(_x) {
return _toDataURL.apply(this, arguments);
}
return toDataURL;
}()
}]);
}();
var ContextRegisterPlugin = /*#__PURE__*/function (_AbstractRendererPlug) {
function ContextRegisterPlugin(rendererPlugin, config) {
var _this;
_classCallCheck(this, ContextRegisterPlugin);
_this = _callSuper(this, ContextRegisterPlugin);
_this.name = 'mobile-webgl-context-register';
_this.rendererPlugin = rendererPlugin;
_this.config = config;
return _this;
}
_inherits(ContextRegisterPlugin, _AbstractRendererPlug);
return _createClass(ContextRegisterPlugin, [{
key: "init",
value: function init() {
this.context.ContextService = WebGLContextService;
this.context.deviceRendererPlugin = this.rendererPlugin;
var config = this.config;
this.context.deviceContribution = new gDeviceApi.WebGLDeviceContribution(_objectSpread({}, config !== null && config !== void 0 && config.targets ? {
targets: config.targets
} : {
targets: ['webgl2', 'webgl1']
}));
}
}, {
key: "destroy",
value: function destroy() {
delete this.context.ContextService;
}
}]);
}(gLite.AbstractRendererPlugin);
var Renderer = /*#__PURE__*/function (_AbstractRenderer) {
function Renderer(config) {
var _this;
_classCallCheck(this, Renderer);
_this = _callSuper(this, Renderer, [config]);
var deviceRendererPlugin = new DeviceRenderer__namespace.Plugin();
_this.registerPlugin(new ContextRegisterPlugin(deviceRendererPlugin, config));
_this.registerPlugin(new ImageLoader__namespace.Plugin());
_this.registerPlugin(deviceRendererPlugin);
_this.registerPlugin(new DomInteraction__namespace.Plugin());
_this.registerPlugin(new HTMLRenderer__namespace.Plugin());
_this.registerPlugin(new DragDropEvent__namespace.Plugin({
isDocumentDraggable: util.isNil(config === null || config === void 0 ? void 0 : config.isDocumentDraggable) ? true : config.isDocumentDraggable,
isDocumentDroppable: util.isNil(config === null || config === void 0 ? void 0 : config.isDocumentDroppable) ? true : config.isDocumentDroppable,
dragstartDistanceThreshold: util.isNil(config === null || config === void 0 ? void 0 : config.dragstartDistanceThreshold) ? 10 : config.dragstartDistanceThreshold,
dragstartTimeThreshold: util.isNil(config === null || config === void 0 ? void 0 : config.dragstartTimeThreshold) ? 50 : config.dragstartTimeThreshold
}));
_this.registerPlugin(new GesturePlugin__namespace.Plugin({
isDocumentGestureEnabled: true
}));
return _this;
}
_inherits(Renderer, _AbstractRenderer);
return _createClass(Renderer);
}(gLite.AbstractRenderer);
exports.DeviceRenderer = DeviceRenderer__namespace;
exports.HTMLRenderer = HTMLRenderer__namespace;
exports.DomInteraction = DomInteraction__namespace;
exports.Renderer = Renderer;
//# sourceMappingURL=index.js.map