adf0d17497
publish / version_or_publish (push) Has been cancelled
storybook-build / changes (push) Has been cancelled
storybook-build / :storybook-build (push) Has been cancelled
Sync Gradio Skills to Hugging Face / sync-skills (push) Has been cancelled
functional / changes (push) Has been cancelled
functional / build-frontend (push) Has been cancelled
functional / functional-test-SSR=false (push) Has been cancelled
functional / functional-reload (push) Has been cancelled
js / changes (push) Has been cancelled
js / js-test (push) Has been cancelled
docs-build / changes (push) Has been cancelled
docs-build / docs-build (push) Has been cancelled
docs-build / website-build (push) Has been cancelled
functional / functional-test-SSR=true (push) Has been cancelled
hygiene / hygiene-test (push) Has been cancelled
python / changes (push) Has been cancelled
python / build (push) Has been cancelled
python / test-ubuntu-latest-flaky (push) Has been cancelled
python / test-ubuntu-latest-not-flaky (push) Has been cancelled
python / test-windows-latest-flaky (push) Has been cancelled
python / test-windows-latest-not-flaky (push) Has been cancelled
28 lines
978 B
TypeScript
28 lines
978 B
TypeScript
export const get_coordinates_of_clicked_image = (
|
|
evt: MouseEvent
|
|
): [number, number] | null => {
|
|
let image;
|
|
if (evt.currentTarget instanceof Element) {
|
|
image = evt.currentTarget.querySelector("img") as HTMLImageElement;
|
|
} else {
|
|
return [NaN, NaN];
|
|
}
|
|
|
|
const imageRect = image.getBoundingClientRect();
|
|
// The image is rendered with `object-fit: scale-down`: centered, never
|
|
// scaled above its natural size, so empty space can appear on both axes.
|
|
const scale = Math.min(
|
|
imageRect.width / image.naturalWidth,
|
|
imageRect.height / image.naturalHeight,
|
|
1
|
|
);
|
|
const x_offset = (imageRect.width - image.naturalWidth * scale) / 2;
|
|
const y_offset = (imageRect.height - image.naturalHeight * scale) / 2;
|
|
const x = Math.round((evt.clientX - imageRect.left - x_offset) / scale);
|
|
const y = Math.round((evt.clientY - imageRect.top - y_offset) / scale);
|
|
if (x < 0 || x >= image.naturalWidth || y < 0 || y >= image.naturalHeight) {
|
|
return null;
|
|
}
|
|
return [x, y];
|
|
};
|