@lightningjs/renderer
Version:
Lightning 3 Renderer
105 lines • 4.48 kB
JavaScript
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2026 Comcast Cable Communications Management, LLC.
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { WebPlatform } from './WebPlatform.js';
import { createImageWorkerLegacy } from './lib/ImageWorkerLegacy.js';
import { isBase64Image, dataURIToBlob, convertUrlToAbsolute, } from './lib/utils.js';
/**
* Legacy Web Platform implementation that uses Image() instead of createImageBitmap
*
* @remarks
* This platform is designed for environments that don't support createImageBitmap API,
* or for compatibility with older browsers. It uses the traditional HTMLImageElement
* approach for image loading.
*
*/
export class WebPlatformLegacy extends WebPlatform {
constructor(settings = {}) {
super({ ...settings, numImageWorkers: settings.numImageWorkers ?? 0 });
}
getImageWorkerFactory() {
return createImageWorkerLegacy;
}
async loadImage(src, premultiplyAlpha, sx, sy, sw, sh) {
const isBase64 = isBase64Image(src);
if (isBase64 === false && this.settings.numImageWorkers > 0) {
return super.loadImage(src, premultiplyAlpha, sx, sy, sw, sh);
}
const absoluteSrc = convertUrlToAbsolute(src);
// For base64 images, use blob conversion
if (isBase64 === true) {
const blob = dataURIToBlob(src);
return this.createImage(blob, premultiplyAlpha, sx ?? null, sy ?? null, sw ?? null, sh ?? null);
}
// For regular URLs, load directly without blob conversion
const hasAlpha = premultiplyAlpha ?? absoluteSrc.toLowerCase().endsWith('.png');
const img = new Image();
img.crossOrigin = 'anonymous';
return new Promise((resolve, reject) => {
img.onload = () => {
resolve({ data: img, premultiplyAlpha: hasAlpha });
};
img.onerror = (err) => {
const errorMessage = err instanceof Error
? err.message
: err instanceof Event
? `Image loading failed for ${img.src}`
: 'Unknown image loading error';
reject(new Error(`Image loading failed: ${errorMessage}`));
};
img.src = absoluteSrc;
});
}
async createImage(blob, premultiplyAlpha,
// Cropping parameters are not supported in legacy mode
// eslint-disable-next-line @typescript-eslint/no-unused-vars
sx,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
sy,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
sw,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
sh) {
const hasAlpha = premultiplyAlpha ?? blob.type.includes('image/png');
const src = URL.createObjectURL(blob);
const img = new Image();
if (typeof src === 'string' && isBase64Image(src) === false) {
img.crossOrigin = 'anonymous';
}
return new Promise((resolve, reject) => {
img.onload = () => {
URL.revokeObjectURL(src);
resolve({ data: img, premultiplyAlpha: hasAlpha });
};
img.onerror = (err) => {
URL.revokeObjectURL(src);
const errorMessage = err instanceof Error
? err.message
: err instanceof Event
? `Image loading failed for ${img.src}`
: 'Unknown image loading error';
reject(new Error(`Image loading failed: ${errorMessage}`));
};
img.src = src;
});
}
async loadCompressedTexture(src) {
throw new Error(`Compressed textures are not supported in legacy mode. Attempted to load: ${src}`);
}
}
//# sourceMappingURL=WebPlatformLegacy.js.map