laya-coreui-es
Version:
laya核心库、ui库es版本,剔除了媒体、TTF等功能。基于 LayaAir-release_2.12.0 修改
121 lines (120 loc) • 4.13 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HTMLCanvas = void 0;
const Bitmap_1 = require("./Bitmap");
const Texture_1 = require("./Texture");
const Texture2D_1 = require("./Texture2D");
const ILaya_1 = require("../../ILaya");
const Browser_1 = require("../utils/Browser");
class HTMLCanvas extends Bitmap_1.Bitmap {
get source() {
return this._source;
}
_getSource() {
return this._source;
}
constructor(createCanvas = false) {
super();
if (createCanvas)
this._source = Browser_1.Browser.createElement("canvas");
else {
this._source = this;
}
this.lock = true;
}
clear() {
if (this._ctx) {
if (this._ctx.clear) {
this._ctx.clear();
}
else {
this._ctx.clearRect(0, 0, this._width, this._height);
}
}
if (this._texture) {
this._texture.destroy();
this._texture = null;
}
}
destroy() {
super.destroy();
this._setCPUMemory(0);
this._ctx && this._ctx.destroy && this._ctx.destroy();
this._ctx = null;
}
release() {
}
get context() {
if (this._ctx)
return this._ctx;
if (this._source == this) {
this._ctx = new ILaya_1.ILaya.Context();
}
else {
this._ctx = this._source.getContext(ILaya_1.ILaya.Render.isConchApp ? 'layagl' : '2d');
}
this._ctx._canvas = this;
return this._ctx;
}
_setContext(context) {
this._ctx = context;
}
getContext(contextID, other = null) {
return this.context;
}
getMemSize() {
return 0;
}
size(w, h) {
if (this._width != w || this._height != h || (this._source && (this._source.width != w || this._source.height != h))) {
this._width = w;
this._height = h;
this._setCPUMemory(w * h * 4);
this._ctx && this._ctx.size && this._ctx.size(w, h);
if (this._source) {
this._source.height = h;
this._source.width = w;
}
if (this._texture) {
this._texture.destroy();
this._texture = null;
}
}
}
getTexture() {
if (!this._texture) {
var bitmap = new Texture2D_1.Texture2D();
bitmap.loadImageSource(this.source);
this._texture = new Texture_1.Texture(bitmap);
}
return this._texture;
}
toBase64(type, encoderOptions) {
if (this._source) {
if (ILaya_1.ILaya.Render.isConchApp) {
var win = window;
if (win.conchConfig.threadMode == 2) {
throw "native 2 thread mode use toBase64Async";
}
var width = this._ctx._targets.sourceWidth;
var height = this._ctx._targets.sourceHeight;
var data = this._ctx._targets.getData(0, 0, width, height);
return win.conchToBase64FlipY ? win.conchToBase64FlipY(type, encoderOptions, data.buffer, width, height) : win.conchToBase64(type, encoderOptions, data.buffer, width, height);
}
else {
return this._source.toDataURL(type, encoderOptions);
}
}
return null;
}
toBase64Async(type, encoderOptions, callBack) {
var width = this._ctx._targets.sourceWidth;
var height = this._ctx._targets.sourceHeight;
this._ctx._targets.getDataAsync(0, 0, width, height, function (data) {
let win = window;
var base64 = win.conchToBase64FlipY ? win.conchToBase64FlipY(type, encoderOptions, data.buffer, width, height) : win.conchToBase64(type, encoderOptions, data.buffer, width, height);
callBack(base64);
});
}
}
exports.HTMLCanvas = HTMLCanvas;