Improve resizing of bounding box
This commit is contained in:
@@ -13,24 +13,42 @@
|
|||||||
{ id: 2, x: 0, y: 0 },
|
{ id: 2, x: 0, y: 0 },
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
let width = $state(0);
|
||||||
|
let height = $state(0);
|
||||||
|
|
||||||
|
const resize = () => {
|
||||||
|
width = image?.getBoundingClientRect().width ?? 0;
|
||||||
|
height = image?.getBoundingClientRect().height ?? 0;
|
||||||
|
};
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
ws = new WebSocket("/ws");
|
ws = new WebSocket("/ws");
|
||||||
ws.onmessage = (event) => {
|
ws.onmessage = (event) => {
|
||||||
const data = JSON.parse(event.data);
|
const data = JSON.parse(event.data);
|
||||||
trackers = data;
|
trackers = data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
width = image?.getBoundingClientRect().width ?? 0;
|
||||||
|
height = image?.getBoundingClientRect().height ?? 0;
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
<svelte:window onresize={resize} />
|
||||||
<div class="relative">
|
<div class="relative">
|
||||||
<img src="/scene_drawing.png" alt="" bind:this={image} class="scale-50" />
|
<img
|
||||||
|
src="/scene_drawing.png"
|
||||||
|
alt=""
|
||||||
|
bind:this={image}
|
||||||
|
class="scale-50"
|
||||||
|
onload={resize}
|
||||||
|
/>
|
||||||
<div
|
<div
|
||||||
bind:this={parent}
|
bind:this={parent}
|
||||||
class="absolute inset-0 border border-red-500"
|
class="absolute inset-0 border border-red-500"
|
||||||
style={"width: " +
|
style={"width: " +
|
||||||
image?.getBoundingClientRect().width +
|
width +
|
||||||
"px; height: " +
|
"px; height: " +
|
||||||
image?.getBoundingClientRect().height +
|
height +
|
||||||
"px;" +
|
"px;" +
|
||||||
"transform: translate(50%, 50%);"}
|
"transform: translate(50%, 50%);"}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -15,15 +15,18 @@
|
|||||||
parent,
|
parent,
|
||||||
}: Props = $props();
|
}: Props = $props();
|
||||||
|
|
||||||
let element: HTMLElement | null = $state(null);
|
let element: HTMLElement;
|
||||||
|
|
||||||
let vis_x = $derived(
|
let vis_x = $state(0);
|
||||||
x + (parent?.clientWidth ?? 0) / 2 - (element?.clientWidth ?? 0) / 2,
|
|
||||||
);
|
|
||||||
|
|
||||||
let vis_y = $derived(
|
let vis_y = $state(0);
|
||||||
y + (parent?.clientHeight ?? 0) / 2 - (element?.clientHeight ?? 0) / 2,
|
|
||||||
);
|
$effect(() => {
|
||||||
|
vis_x = x + (parent.clientWidth ?? 0) / 2 - (element.clientWidth ?? 0) / 2;
|
||||||
|
|
||||||
|
vis_y =
|
||||||
|
y + (parent.clientHeight ?? 0) / 2 - (element.clientHeight ?? 0) / 2;
|
||||||
|
});
|
||||||
|
|
||||||
let capturedPointerId: number | null = $state(null);
|
let capturedPointerId: number | null = $state(null);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user