Skip to content

Commit

Permalink
fix(gui): draw source to canvas immediately on change (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed May 5, 2023
1 parent da6aa15 commit 59075a7
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions gui/src/components/input/MaskCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@ export function MaskCanvas(props: MaskCanvasProps) {
// painting state
const painting = useRef(false);
const dirty = useRef(false);
const background = useRef<string>();
const background = useRef<{ name: string; url: Maybe<string> }>({
name: '',
url: undefined,
});

const state = mustExist(useContext(StateContext));
const brush = useStore(state, (s) => s.brush);
Expand All @@ -218,21 +221,21 @@ export function MaskCanvas(props: MaskCanvasProps) {
}
}, [mask]);

useEffect(() => {
if (doesExist(source)) {
if (doesExist(background.current)) {
URL.revokeObjectURL(background.current);
}
// update background ref
if (doesExist(source) && background.current.name !== source.name) {
if (doesExist(background.current.url)) {
URL.revokeObjectURL(background.current.url);
}

background.current = URL.createObjectURL(source);
background.current.url = URL.createObjectURL(source);
background.current.name = source.name;

// initialize the mask if it does not exist
if (doesExist(mask) === false) {
getClearContext(maskRef);
dirty.current = true;
}
// initialize the mask if it does not exist
if (doesExist(mask) === false) {
getClearContext(maskRef);
dirty.current = true;
}
}, [source]);
}

const backgroundStyle: React.CSSProperties = {
backgroundPosition: 'top left',
Expand All @@ -244,7 +247,7 @@ export function MaskCanvas(props: MaskCanvasProps) {
};

if (doesExist(background.current)) {
backgroundStyle.backgroundImage = `url(${background.current})`;
backgroundStyle.backgroundImage = `url(${background.current.url})`;
}

const hiddenStyle: React.CSSProperties = {
Expand Down

0 comments on commit 59075a7

Please sign in to comment.