watermark pos bug2

This commit is contained in:
Julian Freeman
2026-01-19 00:18:08 -04:00
parent a0822153c1
commit f34021eae1

View File

@@ -1,40 +1,67 @@
<script setup lang="ts"> <script setup lang="ts">
import { useGalleryStore } from "../stores/gallery"; import { useGalleryStore } from "../stores/gallery";
import { convertFileSrc } from "@tauri-apps/api/core"; import { convertFileSrc } from "@tauri-apps/api/core";
import { ref, computed, onMounted, onUnmounted, watch } from "vue"; import { ref, computed, onMounted, onUnmounted, watch, nextTick } from "vue";
const store = useGalleryStore(); const store = useGalleryStore();
const isDragging = ref(false); const isDragging = ref(false);
const dragStart = ref({ x: 0, y: 0 }); const dragStart = ref({ x: 0, y: 0 });
const imgRef = ref<HTMLImageElement | null>(null); const imgRef = ref<HTMLImageElement | null>(null);
const containerRef = ref<HTMLElement | null>(null); const parentRef = ref<HTMLElement | null>(null); // The black background container
const renderRefSize = ref(0); // Min of width/height
// These dimensions will exactly match the rendered image size
const imageRect = ref({ width: 0, height: 0 });
let resizeObserver: ResizeObserver | null = null; let resizeObserver: ResizeObserver | null = null;
const updateSize = () => { const calculateLayout = () => {
if (imgRef.value) { if (!imgRef.value || !parentRef.value || !store.selectedImage) return;
// Use the smaller dimension to calculate scale, matching backend logic
renderRefSize.value = Math.min(imgRef.value.clientWidth, imgRef.value.clientHeight); // Wait for image natural dimensions to be available
} const natW = imgRef.value.naturalWidth;
const natH = imgRef.value.naturalHeight;
if (!natW || !natH) return; // Not loaded yet
const parentW = parentRef.value.clientWidth;
const parentH = parentRef.value.clientHeight;
// Calculate 'contain' fit manually
const scale = Math.min(
(parentW - 64) / natW, // 64px = 2rem padding * 2 sides (p-8)
(parentH - 64) / natH
);
// Prevent scaling up if image is smaller than screen?
// Usually hero view scales up to fit. Let's stick to contain logic (can scale up).
const finalW = Math.floor(natW * scale);
const finalH = Math.floor(natH * scale);
imageRect.value = { width: finalW, height: finalH };
}; };
onMounted(() => { onMounted(() => {
resizeObserver = new ResizeObserver(() => { // Observe the parent container (window size changes)
updateSize(); if (parentRef.value) {
}); resizeObserver = new ResizeObserver(() => {
calculateLayout();
});
resizeObserver.observe(parentRef.value);
}
window.addEventListener('resize', calculateLayout);
}); });
onUnmounted(() => { onUnmounted(() => {
if (resizeObserver) resizeObserver.disconnect(); if (resizeObserver) resizeObserver.disconnect();
window.removeEventListener('resize', calculateLayout);
}); });
// Watch for image ref availability // Re-calculate when image changes
watch(imgRef, (el) => { watch(() => store.selectedImage, () => {
if (el && resizeObserver) { // Reset size until loaded to avoid jump
resizeObserver.disconnect(); // clear old // imageRect.value = { width: 0, height: 0 };
resizeObserver.observe(el); nextTick(() => calculateLayout());
}
}); });
// Use either manual position (if override is true) or ZCA suggestion // Use either manual position (if override is true) or ZCA suggestion
@@ -42,8 +69,7 @@ const position = computed(() => {
if (store.watermarkSettings.manual_override) { if (store.watermarkSettings.manual_override) {
return store.watermarkSettings.manual_position; return store.watermarkSettings.manual_position;
} }
// Default to bottom center if no ZCA or manual return store.selectedImage?.zcaSuggestion ? { x: store.selectedImage.zcaSuggestion.x, y: store.selectedImage.zcaSuggestion.y } : { x: 0.5, y: 0.99 };
return store.selectedImage?.zcaSuggestion ? { x: store.selectedImage.zcaSuggestion.x, y: store.selectedImage.zcaSuggestion.y } : { x: 0.5, y: 0.98 };
}); });
const onMouseDown = (e: MouseEvent) => { const onMouseDown = (e: MouseEvent) => {
@@ -53,11 +79,10 @@ const onMouseDown = (e: MouseEvent) => {
}; };
const onMouseMove = (e: MouseEvent) => { const onMouseMove = (e: MouseEvent) => {
if (!isDragging.value || !store.selectedImage || !containerRef.value) return; // Need exact dimensions for drag calculation
if (!isDragging.value || !store.selectedImage || imageRect.value.width === 0) return;
// Calculate delta in percentage relative to the image container const rect = imageRect.value; // Use our calculated rect, not bounding client rect (though they should match)
const rect = containerRef.value.getBoundingClientRect();
if (rect.width === 0 || rect.height === 0) return;
const deltaX = (e.clientX - dragStart.value.x) / rect.width; const deltaX = (e.clientX - dragStart.value.x) / rect.width;
const deltaY = (e.clientY - dragStart.value.y) / rect.height; const deltaY = (e.clientY - dragStart.value.y) / rect.height;
@@ -66,8 +91,7 @@ const onMouseMove = (e: MouseEvent) => {
let newX = position.value.x + deltaX; let newX = position.value.x + deltaX;
let newY = position.value.y + deltaY; let newY = position.value.y + deltaY;
// Clamp logic (Allow getting very close to edge, padding handled by visual/backend) // Clamp logic (0-1) - This is now strictly inside the image content area
// Using small padding to prevent sticking perfectly to edge if not desired
const padding = 0.005; const padding = 0.005;
newX = Math.max(padding, Math.min(1 - padding, newX)); newX = Math.max(padding, Math.min(1 - padding, newX));
newY = Math.max(padding, Math.min(1 - padding, newY)); newY = Math.max(padding, Math.min(1 - padding, newY));
@@ -92,28 +116,34 @@ const onMouseLeave = () => {
<template> <template>
<div <div
class="absolute inset-0 flex items-center justify-center bg-black p-4 overflow-hidden" ref="parentRef"
class="absolute inset-0 flex items-center justify-center bg-black overflow-hidden"
@mousemove="onMouseMove" @mousemove="onMouseMove"
@mouseup="onMouseUp" @mouseup="onMouseUp"
@mouseleave="onMouseLeave" @mouseleave="onMouseLeave"
> >
<!-- Wrapper to constrain image --> <!--
Dynamic Wrapper:
Dimensions strictly equal to the rendered image size.
This serves as the coordinate system for the watermark.
-->
<div <div
v-if="store.selectedImage" v-if="store.selectedImage"
ref="containerRef" class="relative shadow-2xl"
class="relative flex justify-center items-center" :style="{
style="width: 100%; height: 100%;" width: imageRect.width + 'px',
height: imageRect.height + 'px'
}"
> >
<img <img
ref="imgRef" ref="imgRef"
:src="convertFileSrc(store.selectedImage.path)" :src="convertFileSrc(store.selectedImage.path)"
class="block shadow-2xl select-none pointer-events-none" class="block w-full h-full select-none pointer-events-none"
style="max-width: 100%; max-height: 100%; object-fit: contain;"
alt="Hero Image" alt="Hero Image"
loading="eager" loading="eager"
decoding="sync" decoding="sync"
fetchpriority="high" fetchpriority="high"
@load="updateSize" @load="calculateLayout"
@error="(e) => console.error('Hero Image Load Error:', e)" @error="(e) => console.error('Hero Image Load Error:', e)"
/> />
@@ -127,7 +157,8 @@ const onMouseLeave = () => {
transform: 'translate(-50%, -50%)', transform: 'translate(-50%, -50%)',
opacity: store.watermarkSettings.opacity, opacity: store.watermarkSettings.opacity,
color: store.watermarkSettings.color, color: store.watermarkSettings.color,
fontSize: (renderRefSize * store.watermarkSettings.scale) + 'px', /* Scale based on min-dimension of the IMAGE, not window */
fontSize: (Math.min(imageRect.width, imageRect.height) * store.watermarkSettings.scale) + 'px',
height: '0px', height: '0px',
display: 'flex', display: 'flex',
alignItems: 'center', alignItems: 'center',