-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommittedRoisButton.tsx
More file actions
61 lines (54 loc) · 1.46 KB
/
CommittedRoisButton.tsx
File metadata and controls
61 lines (54 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { writeCanvas } from 'image-js';
import { useEffect, useReducer, useRef } from 'react';
import { useCommittedRois } from '../../src/index.ts';
import { useLoadImage } from './useLoadImage.ts';
export function CommittedRoisButton(props: {
showImage?: boolean;
isDefaultShown?: boolean;
}) {
const { showImage = true } = props;
const ref = useRef<HTMLCanvasElement>(null);
const rois = useCommittedRois();
const [isShown, show] = useReducer((s) => !s, props.isDefaultShown || false);
const image = useLoadImage('story-image', ref);
useEffect(() => {
if (!image) {
return;
}
const newImage = image.clone();
for (const roi of rois) {
const points = roi.getRectanglePoints();
newImage.drawPolygon(
points.map((point) => ({
row: point.y,
column: point.x,
})),
{
strokeColor: [255, 255, 255],
out: newImage,
},
);
}
if (ref.current) {
writeCanvas(newImage, ref.current);
}
}, [rois, isShown, image]);
return (
<>
<button type="button" onClick={show}>
{isShown ? 'Hide committed ROIs' : 'Show committed ROIs'}
</button>
<>
<canvas
ref={ref}
id="transformed-image"
style={{
maxWidth: 400,
display: showImage && isShown ? 'block' : 'none',
}}
/>
{isShown && <pre>{JSON.stringify(rois, null, 2)}</pre>}
</>
</>
);
}