Skip to content

Commit c183777

Browse files
committed
fix cross origin image loading race condition
1 parent dbffd24 commit c183777

File tree

4 files changed

+68
-17
lines changed

4 files changed

+68
-17
lines changed

src/FullImageSegmentationAnnotator/index.story.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,37 @@ storiesOf("FullImageSegmentationAnnotator.Basic", module)
4545
/>
4646
</div>
4747
))
48+
.add("S3 CORS domain", () => (
49+
<div style={{ width: "100vw", height: "100vh" }}>
50+
<FullImageSegmentationAnnotator
51+
images={[
52+
{
53+
name: "Seve's Desk",
54+
src:
55+
"https://s3.amazonaws.com/datasets.workaround.online/faces/010041.jpg",
56+
regions: [
57+
[0, 100, 125],
58+
[0, 100, 150],
59+
[1, 40, 280],
60+
[1, 10, 10],
61+
[1, 240, 300],
62+
].map(([cls, y, x], i) => ({
63+
cls: ["fg", "bg"][cls],
64+
color: "hsl(162,100%,50%)",
65+
editingLabels: false,
66+
highlighted: false,
67+
id: "a" + i,
68+
type: "point",
69+
x: x / 320,
70+
y: y / 249,
71+
})),
72+
},
73+
]}
74+
regionClsList={["bg", "fg"]}
75+
onExit={action("onExit")}
76+
/>
77+
</div>
78+
))
4879
.add("Orange 3 Class", () => (
4980
<div style={{ width: "100vw", height: "100vh" }}>
5081
<FullImageSegmentationAnnotator
@@ -70,7 +101,7 @@ storiesOf("FullImageSegmentationAnnotator.Basic", module)
70101
})),
71102
},
72103
]}
73-
regionClsList={["bg", "orange" , "hand"]}
104+
regionClsList={["bg", "orange", "hand"]}
74105
onExit={action("onExit")}
75106
/>
76107
</div>

src/ImageMask/index.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import React, { useState, useEffect, useMemo, useRef } from "react"
44
import { colorInts } from "../colors"
55
import { useDebounce } from "react-use"
6+
import loadImage from "./load-image"
67

78
import MMGC_INIT from "mmgc1-cpp"
89

@@ -24,25 +25,11 @@ export const ImageMask = ({
2425

2526
useEffect(() => {
2627
if (!imageSrc) return
27-
const canvas = document.createElement("canvas")
28-
const ctx = canvas.getContext("2d")
2928

30-
const image = new Image()
31-
image.crossOrigin = "anonymous"
32-
image.src = imageSrc
33-
image.onload = () => {
34-
canvas.width = image.naturalWidth
35-
canvas.height = image.naturalHeight
36-
ctx.drawImage(image, 0, 0)
37-
const imageData = ctx.getImageData(
38-
0,
39-
0,
40-
image.naturalWidth,
41-
image.naturalHeight
42-
)
29+
loadImage(imageSrc).then((imageData) => {
4330
superPixelsGenerated.current = false
4431
setSampleImageData(imageData)
45-
}
32+
})
4633
}, [imageSrc])
4734

4835
useDebounce(

src/ImageMask/load-image.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export const loadImage = (imageSrc) => {
2+
// Check if image is already loaded in a page element
3+
let image = Array.from(document.getElementsByTagName("img")).find(
4+
(img) => img.src === imageSrc
5+
)
6+
7+
const canvas = document.createElement("canvas")
8+
const ctx = canvas.getContext("2d")
9+
10+
if (!image) {
11+
image = new Image()
12+
image.crossOrigin = "anonymous"
13+
image.src = imageSrc
14+
}
15+
16+
return new Promise((resolve, reject) => {
17+
image.onload = () => {
18+
canvas.width = image.naturalWidth
19+
canvas.height = image.naturalHeight
20+
ctx.drawImage(image, 0, 0)
21+
const imageData = ctx.getImageData(
22+
0,
23+
0,
24+
image.naturalWidth,
25+
image.naturalHeight
26+
)
27+
resolve(imageData)
28+
}
29+
})
30+
}
31+
32+
export default loadImage

src/VideoOrImageCanvasBackground/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export default ({
119119
ref={imageRef}
120120
style={stylePosition}
121121
onLoad={onImageLoaded}
122+
crossOrigin="anonymous"
122123
/>
123124
) : (
124125
<Video

0 commit comments

Comments
 (0)