Skip to content

feat: added useFetchForTextures option #532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/core/CoreTextureManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export interface TextureManagerDebugInfo {
export interface TextureManagerSettings {
numImageWorkers: number;
createImageBitmapSupport: 'auto' | 'basic' | 'options' | 'full';
useFetchForTextures: boolean;
}

export type ResizeModeOptions =
Expand Down Expand Up @@ -192,6 +193,7 @@ export class CoreTextureManager extends EventEmitter {
options: false,
full: false,
};
useFetchForTextures = false;

hasWorker = !!self.Worker;
/**
Expand All @@ -217,9 +219,15 @@ export class CoreTextureManager extends EventEmitter {
constructor(stage: Stage, settings: TextureManagerSettings) {
super();

const { numImageWorkers, createImageBitmapSupport } = settings;
const {
numImageWorkers,
createImageBitmapSupport,
useFetchForTextures: useFetchForTextures,
} = settings;

this.stage = stage;
this.numImageWorkers = numImageWorkers;
this.useFetchForTextures = useFetchForTextures;

if (createImageBitmapSupport === 'auto') {
validateCreateImageBitmap()
Expand Down
3 changes: 3 additions & 0 deletions src/core/Stage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export interface StageOptions {
strictBounds: boolean;
textureProcessingTimeLimit: number;
createImageBitmapSupport: 'auto' | 'basic' | 'options' | 'full';
useFetchForTextures: boolean;
}

export type StageFpsUpdateHandler = (
Expand Down Expand Up @@ -153,12 +154,14 @@ export class Stage {
renderEngine,
fontEngines,
createImageBitmapSupport,
useFetchForTextures: useFetchForTextures,
} = options;

this.eventBus = options.eventBus;
this.txManager = new CoreTextureManager(this, {
numImageWorkers,
createImageBitmapSupport,
useFetchForTextures: useFetchForTextures,
});

// Wait for the Texture Manager to initialize
Expand Down
58 changes: 42 additions & 16 deletions src/core/lib/ImageWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,16 @@ function createImageWorker() {
options: {
supportsOptionsCreateImageBitmap: boolean;
supportsFullCreateImageBitmap: boolean;
useFetchForTextures: boolean;
},
): Promise<getImageReturn> {
return new Promise(function (resolve, reject) {
var supportsOptionsCreateImageBitmap =
options.supportsOptionsCreateImageBitmap;
var supportsFullCreateImageBitmap = options.supportsFullCreateImageBitmap;
var xhr = new XMLHttpRequest();
xhr.open('GET', src, true);
xhr.responseType = 'blob';
var useFetchForTextures = options.useFetchForTextures;

xhr.onload = function () {
// On most devices like WebOS and Tizen, the file protocol returns 0 while http(s) protocol returns 200
if (xhr.status !== 200 && xhr.status !== 0) {
return reject(new Error('Failed to load image: ' + xhr.statusText));
}

var blob = xhr.response;
const onFetchSuccess = (blob: Blob) => {
var withAlphaChannel =
premultiplyAlpha !== undefined
? premultiplyAlpha
Expand Down Expand Up @@ -130,13 +123,44 @@ function createImageWorker() {
}
};

xhr.onerror = function () {
reject(
new Error('Network error occurred while trying to fetch the image.'),
);
};
if (useFetchForTextures === true) {
fetch(src)
.then((response) => response.blob())
.then((blob) => {
onFetchSuccess(blob);
})
.catch((error) => {
reject(
new Error(
'Network error occurred while trying to fetch the image.',
),
);
});
} else {
var xhr = new XMLHttpRequest();
xhr.open('GET', src, true);
xhr.responseType = 'blob';

xhr.send();
xhr.onload = function () {
// On most devices like WebOS and Tizen, the file protocol returns 0 while http(s) protocol returns 200
if (xhr.status !== 200 && xhr.status !== 0) {
return reject(new Error('Failed to load image: ' + xhr.statusText));
}

var blob = xhr.response;
onFetchSuccess(blob);
};

xhr.onerror = function () {
reject(
new Error(
'Network error occurred while trying to fetch the image.',
),
);
};

xhr.send();
}
});
}

Expand All @@ -152,10 +176,12 @@ function createImageWorker() {
// these will be set to true if the browser supports the createImageBitmap options or full
var supportsOptionsCreateImageBitmap = false;
var supportsFullCreateImageBitmap = false;
var useFetchForTextures = false;

getImage(src, premultiplyAlpha, x, y, width, height, {
supportsOptionsCreateImageBitmap,
supportsFullCreateImageBitmap,
useFetchForTextures,
})
.then(function (data) {
self.postMessage({ id: id, src: src, data: data });
Expand Down
13 changes: 10 additions & 3 deletions src/core/textures/ImageTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,16 @@ export class ImageTexture extends Texture {
);
}

const blob = await fetchJson(src, 'blob').then(
(response) => response as Blob,
);
let blob;

if (this.txManager.useFetchForTextures === true) {
blob = await fetch(src).then((response) => response.blob());
} else {
blob = await fetchJson(src, 'blob').then(
(response) => response as Blob,
);
}

return this.createImageBitmap(blob, premultiplyAlpha, sx, sy, sw, sh);
}

Expand Down
12 changes: 12 additions & 0 deletions src/main-api/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,16 @@ export interface RendererMainSettings {
* @defaultValue `full`
*/
createImageBitmapSupport?: 'auto' | 'basic' | 'options' | 'full';

/**
* Enable using fetch instead of XMLHttpRequest for image loading
*
* @remarks
* This is used to force the use of fetch instead of XMLHttpRequest for the image loader.
*
* @defaultValue `false`
*/
useFetchForTextures?: boolean;
}

/**
Expand Down Expand Up @@ -415,6 +425,7 @@ export class RendererMain extends EventEmitter {
textureProcessingTimeLimit: settings.textureProcessingTimeLimit || 10,
canvas: settings.canvas || document.createElement('canvas'),
createImageBitmapSupport: settings.createImageBitmapSupport || 'full',
useFetchForTextures: settings.useFetchForTextures || false,
};
this.settings = resolvedSettings;

Expand Down Expand Up @@ -459,6 +470,7 @@ export class RendererMain extends EventEmitter {
strictBounds: this.settings.strictBounds,
textureProcessingTimeLimit: this.settings.textureProcessingTimeLimit,
createImageBitmapSupport: this.settings.createImageBitmapSupport,
useFetchForTextures: this.settings.useFetchForTextures,
});

// Extract the root node
Expand Down