Refactor crossOrigin handling for image loading

In my last change, I didn't try to change the APIs too much, and kept the concept of `crossOrigin` running through `getBestImageUrlForLayer`.

Now, I've moved the `safeImageUrl` call _outside_ `getBestImageUrlForLayer`, by putting it at the call site: We now call `safeImageUrl` from `loadImage` (which needs to know the `crossOrigin` flag anyway!), and at the `img` tag call site.

This simplifies all of the call sites a lot, I think!
This commit is contained in:
Emi Matchu 2021-06-20 10:54:03 -07:00
parent c2535f811f
commit 92001d514a
4 changed files with 16 additions and 23 deletions

View file

@ -607,11 +607,7 @@ function useDownloadableImage(visibleLayers) {
// performance (can use cached SVG), and predictability (image will // performance (can use cached SVG), and predictability (image will
// look like what you see here). // look like what you see here).
const imagePromises = visibleLayers.map((layer) => const imagePromises = visibleLayers.map((layer) =>
loadImage({ loadImage(getBestImageUrlForLayer(layer, { hiResMode }), {
src: getBestImageUrlForLayer(layer, {
hiResMode,
crossOrigin: "anonymous",
}),
crossOrigin: "anonymous", crossOrigin: "anonymous",
}) })
); );

View file

@ -289,10 +289,7 @@ export async function loadMovieLibrary(librarySrc) {
const manifestImages = new Map( const manifestImages = new Map(
library.properties.manifest.map(({ id, src }) => [ library.properties.manifest.map(({ id, src }) => [
id, id,
loadImage({ loadImage(librarySrcDir + "/" + src, {
src: safeImageUrl(librarySrcDir + "/" + src, {
crossOrigin: "anonymous",
}),
crossOrigin: "anonymous", crossOrigin: "anonymous",
}), }),
]) ])

View file

@ -262,7 +262,9 @@ export function OutfitLayers({
) : ( ) : (
<Box <Box
as="img" as="img"
src={getBestImageUrlForLayer(layer, { hiResMode })} src={safeImageUrl(
getBestImageUrlForLayer(layer, { hiResMode })
)}
alt="" alt=""
objectFit="contain" objectFit="contain"
maxWidth="100%" maxWidth="100%"
@ -332,14 +334,11 @@ export function FullScreenCenter({ children, ...otherProps }) {
); );
} }
export function getBestImageUrlForLayer( export function getBestImageUrlForLayer(layer, { hiResMode = false } = {}) {
layer,
{ hiResMode = false, crossOrigin = null } = {}
) {
if (hiResMode && layer.svgUrl) { if (hiResMode && layer.svgUrl) {
return safeImageUrl(layer.svgUrl, { crossOrigin }); return layer.svgUrl;
} else { } else {
return safeImageUrl(layer.imageUrl, { crossOrigin }); return layer.imageUrl;
} }
} }
@ -386,12 +385,12 @@ export function usePreloadLayers(layers) {
}) })
); );
} else { } else {
return loadImage({ return loadImage(getBestImageUrlForLayer(layer, { hiResMode })).then(
src: getBestImageUrlForLayer(layer, { hiResMode }), (image) => ({
}).then((image) => ({ type: "image",
type: "image", image,
image, })
})); );
} }
}); });

View file

@ -352,7 +352,8 @@ export function useLocalStorage(key, initialValue) {
return [storedValue, setValue]; return [storedValue, setValue];
} }
export function loadImage({ src, crossOrigin = null }) { export function loadImage(rawSrc, { crossOrigin = null } = {}) {
const src = safeImageUrl(rawSrc, { crossOrigin });
const image = new Image(); const image = new Image();
const promise = new Promise((resolve, reject) => { const promise = new Promise((resolve, reject) => {
image.onload = () => resolve(image); image.onload = () => resolve(image);