Files
il/resources/public/js/cljs-runtime/module$node_modules$pixi_DOT_js$lib$rendering$renderers$canvas$utils$canvasUtils.js
2026-07-01 22:38:29 -06:00

187 lines
11 KiB
JavaScript

shadow$provide.module$node_modules$pixi_DOT_js$lib$rendering$renderers$canvas$utils$canvasUtils = function(global, require, module, exports) {
var Color = require("module$node_modules$pixi_DOT_js$lib$color$Color"), adapter = require("module$node_modules$pixi_DOT_js$lib$environment$adapter"), groupD8 = require("module$node_modules$pixi_DOT_js$lib$maths$matrix$groupD8");
global = require("module$node_modules$pixi_DOT_js$lib$rendering$renderers$canvas$utils$canUseNewCanvasBlendModes");
"use strict";
const canvasUtils = {canvas:null, convertTintToImage:!1, cacheStepsPerColorChannel:8, canUseMultiply:global.canUseNewCanvasBlendModes(), tintMethod:null, _canvasSourceCache:new WeakMap(), _unpremultipliedCache:new WeakMap(), getCanvasSource:texture => {
texture = texture.source;
var resource = texture?.resource;
if (!resource) {
return null;
}
var isPMA = "premultiplied-alpha" === texture.alphaMode, resourceHeight = texture.resourceHeight ?? texture.pixelHeight;
resourceHeight = (texture.resourceWidth ?? texture.pixelWidth) !== texture.pixelWidth || resourceHeight !== texture.pixelHeight;
if (isPMA) {
if ((resource instanceof HTMLCanvasElement || "undefined" !== typeof OffscreenCanvas && resource instanceof OffscreenCanvas) && !resourceHeight) {
return resource;
}
var cached = canvasUtils._unpremultipliedCache.get(texture);
if (cached?.resourceId === texture._resourceId) {
return cached.canvas;
}
}
if (resource instanceof Uint8Array || resource instanceof Uint8ClampedArray || resource instanceof Int8Array || resource instanceof Uint16Array || resource instanceof Int16Array || resource instanceof Uint32Array || resource instanceof Int32Array || resource instanceof Float32Array || resource instanceof ArrayBuffer) {
isPMA = canvasUtils._canvasSourceCache.get(texture);
if (isPMA?.resourceId === texture._resourceId) {
return isPMA.canvas;
}
isPMA = adapter.DOMAdapter.get().createCanvas(texture.pixelWidth, texture.pixelHeight);
resourceHeight = isPMA.getContext("2d");
cached = resourceHeight.createImageData(texture.pixelWidth, texture.pixelHeight);
var data = cached.data;
resource = resource instanceof ArrayBuffer ? new Uint8Array(resource) : new Uint8Array(resource.buffer, resource.byteOffset, resource.byteLength);
if ("bgra8unorm" === texture.format) {
for (var i = 0; i < data.length && i + 3 < resource.length; i += 4) {
data[i] = resource[i + 2], data[i + 1] = resource[i + 1], data[i + 2] = resource[i], data[i + 3] = resource[i + 3];
}
} else {
data.set(resource.subarray(0, data.length));
}
resourceHeight.putImageData(cached, 0, 0);
canvasUtils._canvasSourceCache.set(texture, {canvas:isPMA, resourceId:texture._resourceId});
return isPMA;
}
if (isPMA) {
isPMA = adapter.DOMAdapter.get().createCanvas(texture.pixelWidth, texture.pixelHeight);
resourceHeight = isPMA.getContext("2d", {willReadFrequently:!0});
isPMA.width = texture.pixelWidth;
isPMA.height = texture.pixelHeight;
resourceHeight.drawImage(resource, 0, 0);
resource = resourceHeight.getImageData(0, 0, isPMA.width, isPMA.height);
cached = resource.data;
for (data = 0; data < cached.length; data += 4) {
i = cached[data + 3], 0 < i && (i = 255 / i, cached[data] = Math.min(255, cached[data] * i + 0.5), cached[data + 1] = Math.min(255, cached[data + 1] * i + 0.5), cached[data + 2] = Math.min(255, cached[data + 2] * i + 0.5));
}
resourceHeight.putImageData(resource, 0, 0);
canvasUtils._unpremultipliedCache.set(texture, {canvas:isPMA, resourceId:texture._resourceId});
return isPMA;
}
if (resourceHeight) {
isPMA = canvasUtils._canvasSourceCache.get(texture);
if (isPMA?.resourceId === texture._resourceId) {
return isPMA.canvas;
}
isPMA = adapter.DOMAdapter.get().createCanvas(texture.pixelWidth, texture.pixelHeight);
resourceHeight = isPMA.getContext("2d");
isPMA.width = texture.pixelWidth;
isPMA.height = texture.pixelHeight;
resourceHeight.drawImage(resource, 0, 0);
canvasUtils._canvasSourceCache.set(texture, {canvas:isPMA, resourceId:texture._resourceId});
return isPMA;
}
return resource;
}, getTintedCanvas:(sprite, color) => {
const texture = sprite.texture;
sprite = Color.Color.shared.setValue(color).toHex();
const cache = texture.tintCache || (texture.tintCache = {});
var cachedCanvas = cache[sprite];
const resourceId = texture.source._resourceId;
if (cachedCanvas?.tintId === resourceId) {
return cachedCanvas;
}
cachedCanvas = cachedCanvas && "getContext" in cachedCanvas ? cachedCanvas : adapter.DOMAdapter.get().createCanvas();
canvasUtils.tintMethod(texture, color, cachedCanvas);
cachedCanvas.tintId = resourceId;
canvasUtils.convertTintToImage && void 0 !== cachedCanvas.toDataURL ? (color = adapter.DOMAdapter.get().createImage(), color.src = cachedCanvas.toDataURL(), color.tintId = resourceId, cache[sprite] = color) : cache[sprite] = cachedCanvas;
return cache[sprite];
}, getTintedPattern:(texture, color) => {
const stringColor = Color.Color.shared.setValue(color).toHex(), cache = texture.patternCache || (texture.patternCache = {}), resourceId = texture.source._resourceId;
let pattern = cache[stringColor];
if (pattern?.tintId === resourceId) {
return pattern;
}
canvasUtils.canvas || (canvasUtils.canvas = adapter.DOMAdapter.get().createCanvas());
canvasUtils.tintMethod(texture, color, canvasUtils.canvas);
pattern = canvasUtils.canvas.getContext("2d").createPattern(canvasUtils.canvas, "repeat");
pattern.tintId = resourceId;
return cache[stringColor] = pattern;
}, applyPatternTransform:(pattern, matrix, invert = !0) => {
if (matrix && pattern.setTransform) {
var DOMMatrixCtor = globalThis.DOMMatrix;
DOMMatrixCtor && (matrix = new DOMMatrixCtor([matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty]), pattern.setTransform(invert ? matrix.inverse() : matrix));
}
}, tintWithMultiply:(texture, color, canvas) => {
const context = canvas.getContext("2d"), crop = texture.frame.clone();
var resolution = texture.source._resolution ?? texture.source.resolution ?? 1;
const rotate = texture.rotate;
crop.x *= resolution;
crop.y *= resolution;
crop.width *= resolution;
crop.height *= resolution;
var isVertical = groupD8.groupD8.isVertical(rotate);
resolution = isVertical ? crop.height : crop.width;
isVertical = isVertical ? crop.width : crop.height;
canvas.width = Math.ceil(resolution);
canvas.height = Math.ceil(isVertical);
context.save();
context.fillStyle = Color.Color.shared.setValue(color).toHex();
context.fillRect(0, 0, resolution, isVertical);
context.globalCompositeOperation = "multiply";
if (texture = canvasUtils.getCanvasSource(texture)) {
rotate && canvasUtils._applyInverseRotation(context, rotate, crop.width, crop.height), context.drawImage(texture, crop.x, crop.y, crop.width, crop.height, 0, 0, crop.width, crop.height), context.globalCompositeOperation = "destination-atop", context.drawImage(texture, crop.x, crop.y, crop.width, crop.height, 0, 0, crop.width, crop.height);
}
context.restore();
}, tintWithOverlay:(texture, color, canvas) => {
const context = canvas.getContext("2d"), crop = texture.frame.clone();
var resolution = texture.source._resolution ?? texture.source.resolution ?? 1;
const rotate = texture.rotate;
crop.x *= resolution;
crop.y *= resolution;
crop.width *= resolution;
crop.height *= resolution;
var isVertical = groupD8.groupD8.isVertical(rotate);
resolution = isVertical ? crop.height : crop.width;
isVertical = isVertical ? crop.width : crop.height;
canvas.width = Math.ceil(resolution);
canvas.height = Math.ceil(isVertical);
context.save();
context.globalCompositeOperation = "copy";
context.fillStyle = Color.Color.shared.setValue(color).toHex();
context.fillRect(0, 0, resolution, isVertical);
context.globalCompositeOperation = "destination-atop";
if (texture = canvasUtils.getCanvasSource(texture)) {
rotate && canvasUtils._applyInverseRotation(context, rotate, crop.width, crop.height), context.drawImage(texture, crop.x, crop.y, crop.width, crop.height, 0, 0, crop.width, crop.height);
}
context.restore();
}, tintWithPerPixel:(texture, color, canvas) => {
const context = canvas.getContext("2d");
var crop = texture.frame.clone(), resolution = texture.source._resolution ?? texture.source.resolution ?? 1, rotate = texture.rotate;
crop.x *= resolution;
crop.y *= resolution;
crop.width *= resolution;
crop.height *= resolution;
var isVertical = groupD8.groupD8.isVertical(rotate);
resolution = isVertical ? crop.height : crop.width;
isVertical = isVertical ? crop.width : crop.height;
canvas.width = Math.ceil(resolution);
canvas.height = Math.ceil(isVertical);
context.save();
context.globalCompositeOperation = "copy";
if (texture = canvasUtils.getCanvasSource(texture)) {
rotate && canvasUtils._applyInverseRotation(context, rotate, crop.width, crop.height);
context.drawImage(texture, crop.x, crop.y, crop.width, crop.height, 0, 0, crop.width, crop.height);
context.restore();
crop = color >> 16 & 255;
rotate = color >> 8 & 255;
color &= 255;
resolution = context.getImageData(0, 0, resolution, isVertical);
isVertical = resolution.data;
for (texture = 0; texture < isVertical.length; texture += 4) {
isVertical[texture] = isVertical[texture] * crop / 255, isVertical[texture + 1] = isVertical[texture + 1] * rotate / 255, isVertical[texture + 2] = isVertical[texture + 2] * color / 255;
}
context.putImageData(resolution, 0, 0);
} else {
context.restore();
}
}, _applyInverseRotation:(context, rotate, srcWidth, srcHeight) => {
var inv = groupD8.groupD8.inv(rotate);
rotate = groupD8.groupD8.uX(inv);
const b = groupD8.groupD8.uY(inv), c = groupD8.groupD8.vX(inv);
inv = groupD8.groupD8.vY(inv);
context.transform(rotate, b, c, inv, -Math.min(0, rotate * srcWidth, c * srcHeight, rotate * srcWidth + c * srcHeight), -Math.min(0, b * srcWidth, inv * srcHeight, b * srcWidth + inv * srcHeight));
}};
canvasUtils.tintMethod = canvasUtils.canUseMultiply ? canvasUtils.tintWithMultiply : canvasUtils.tintWithPerPixel;
exports.canvasUtils = canvasUtils;
};
//# sourceMappingURL=module$node_modules$pixi_DOT_js$lib$rendering$renderers$canvas$utils$canvasUtils.js.map