Skip to content
Open
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
32 changes: 21 additions & 11 deletions application/ui/src/features/annotator/tools/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,22 +348,32 @@ export const getRelativePoint = (element: ElementType, point: Point, zoom: numbe
};
};

export const loadImage = (link: string): Promise<HTMLImageElement> =>
new Promise<HTMLImageElement>((resolve, reject) => {
const image = new Image();
image.crossOrigin = 'anonymous';
export const loadImage = async (link: string): Promise<HTMLImageElement> => {
// Fetch as blob first to avoid CORS issues with cached images
const response = await fetch(link, { credentials: 'include' });
if (!response.ok) {
throw new Error(`Failed to load image from ${link}: ${response.status} ${response.statusText}`);
}

image.onload = () => resolve(image);
image.onerror = (error) => reject(error);
const blob = await response.blob();
const objectUrl = URL.createObjectURL(blob);

image.fetchPriority = 'high';
image.src = link;
return new Promise<HTMLImageElement>((resolve, reject) => {
const image = new Image();

if (process.env.NODE_ENV === 'test') {
// Immediately load the media item's image
image.onload = () => {
URL.revokeObjectURL(objectUrl);
resolve(image);
}
};
image.onerror = (error) => {
URL.revokeObjectURL(objectUrl);
reject(error);
};

image.fetchPriority = 'high';
image.src = objectUrl;
});
};

const drawImageOnCanvas = (img: HTMLImageElement, filter = ''): HTMLCanvasElement => {
const canvas: HTMLCanvasElement = document.createElement('canvas');
Expand Down
Loading