pixi.js
Version:
PixiJS — The HTML5 Creation Engine =============
59 lines (55 loc) • 1.94 kB
JavaScript
'use strict';
var adapter = require('../../../../../environment/adapter.js');
var Extensions = require('../../../../../extensions/Extensions.js');
var TextureSource = require('./TextureSource.js');
"use strict";
class CanvasSource extends TextureSource.TextureSource {
constructor(options) {
if (!options.resource) {
options.resource = adapter.DOMAdapter.get().createCanvas();
}
if (!options.width) {
options.width = options.resource.width;
if (!options.autoDensity) {
options.width /= options.resolution;
}
}
if (!options.height) {
options.height = options.resource.height;
if (!options.autoDensity) {
options.height /= options.resolution;
}
}
super(options);
this.uploadMethodId = "image";
this.autoDensity = options.autoDensity;
const canvas = options.resource;
if (this.pixelWidth !== canvas.width || this.pixelWidth !== canvas.height) {
this.resizeCanvas();
}
this.transparent = !!options.transparent;
}
resizeCanvas() {
if (this.autoDensity) {
this.resource.style.width = `${this.width}px`;
this.resource.style.height = `${this.height}px`;
}
if (this.resource.width !== this.pixelWidth || this.resource.height !== this.pixelHeight) {
this.resource.width = this.pixelWidth;
this.resource.height = this.pixelHeight;
}
}
resize(width = this.width, height = this.height, resolution = this._resolution) {
const didResize = super.resize(width, height, resolution);
if (didResize) {
this.resizeCanvas();
}
return didResize;
}
static test(resource) {
return globalThis.HTMLCanvasElement && resource instanceof HTMLCanvasElement || globalThis.OffscreenCanvas && resource instanceof OffscreenCanvas;
}
}
CanvasSource.extension = Extensions.ExtensionType.TextureSource;
exports.CanvasSource = CanvasSource;
//# sourceMappingURL=CanvasSource.js.map