UNPKG

pixi.js

Version:

<p align="center"> <a href="https://pixijs.com" target="_blank" rel="noopener noreferrer"> <img height="150" src="https://files.pixijs.download/branding/pixijs-logo-transparent-dark.svg?v=1" alt="PixiJS logo"> </a> </p> <br/> <p align="center">

82 lines (79 loc) 2.06 kB
import { DOMAdapter } from '../../../../environment/adapter.mjs'; import { ExtensionType } from '../../../../extensions/Extensions.mjs'; import { canvasUtils } from '../utils/canvasUtils.mjs'; "use strict"; class CanvasTextureSystem { /** * @param renderer - The owning CanvasRenderer. */ constructor(renderer) { void renderer; } /** Initializes the system (no-op for canvas). */ init() { } /** * Initializes a texture source (no-op for canvas). * @param _source - Texture source. */ initSource(_source) { } /** * Creates a canvas containing the texture's frame. * @param texture - Texture to render. */ generateCanvas(texture) { const canvas = DOMAdapter.get().createCanvas(); const context = canvas.getContext("2d"); const source = canvasUtils.getCanvasSource(texture); if (!source) { return canvas; } const frame = texture.frame; const resolution = texture.source._resolution ?? texture.source.resolution ?? 1; const sx = frame.x * resolution; const sy = frame.y * resolution; const sw = frame.width * resolution; const sh = frame.height * resolution; canvas.width = Math.ceil(sw); canvas.height = Math.ceil(sh); context.drawImage( source, sx, sy, sw, sh, 0, 0, sw, sh ); return canvas; } /** * Reads pixel data from a texture. * @param texture - Texture to read. */ getPixels(texture) { const canvas = this.generateCanvas(texture); const context = canvas.getContext("2d", { willReadFrequently: true }); const imageData = context.getImageData(0, 0, canvas.width, canvas.height); return { pixels: imageData.data, width: canvas.width, height: canvas.height }; } /** Destroys the system (no-op for canvas). */ destroy() { } } /** @ignore */ CanvasTextureSystem.extension = { type: [ ExtensionType.CanvasSystem ], name: "texture" }; export { CanvasTextureSystem }; //# sourceMappingURL=CanvasTextureSystem.mjs.map