From a78201bffd2a5b53ff277715fc3fe90dd7fed353 Mon Sep 17 00:00:00 2001 From: ShareVB Date: Sun, 6 Jul 2025 12:25:32 +0200 Subject: [PATCH] feat(Watermarker): add options for repeated tile spacing and single watermark positioning Fix https://github.com/sharevb/it-tools/issues/103#issuecomment-2994950670 --- src/tools/watermarker/watermarker.vue | 120 +++++++++++++++++++------- 1 file changed, 89 insertions(+), 31 deletions(-) diff --git a/src/tools/watermarker/watermarker.vue b/src/tools/watermarker/watermarker.vue index d323fcb5..57796593 100644 --- a/src/tools/watermarker/watermarker.vue +++ b/src/tools/watermarker/watermarker.vue @@ -28,6 +28,8 @@ const opacity = useQueryParamOrStorage({ name: 'opa', storageName: 'watermarker: const rotation = useQueryParamOrStorage({ name: 'rot', storageName: 'watermarker:r', defaultValue: 45 }); const repeatWatermark = useQueryParamOrStorage({ name: 'repeat', storageName: 'watermarker:rep', defaultValue: false }); const logoWidth = useQueryParamOrStorage({ name: 'logow', storageName: 'watermarker:lw', defaultValue: 60 }); +const tileSpacing = useQueryParamOrStorage({ name: 'tilespc', storageName: 'watermarker:ts', defaultValue: 30 }); +const watermarkPosition = useQueryParamOrStorage({ name: 'pos', storageName: 'watermarker:pos', defaultValue: 'center' }); const logo = ref(null); const downloadFormat = ref('png'); @@ -105,28 +107,25 @@ function drawImage(index: number) { ctx.drawImage(img, 0, 0); ctx.globalAlpha = opacity.value / 100; + // Measure text + const measureCanvas = document.createElement('canvas'); + const measureCtx = measureCanvas.getContext('2d')!; + measureCtx.font = `${fontSize.value}px sans-serif`; + + const textMetrics = measureCtx.measureText(watermarkText.value); + const textWidth = textMetrics.width; + const textHeight = fontSize.value; + + const angle = (rotation.value * Math.PI) / 180; + + const rotatedWidth = Math.abs(textWidth * Math.cos(angle)) + Math.abs(textHeight * Math.sin(angle)); + const rotatedHeight = Math.abs(textWidth * Math.sin(angle)) + Math.abs(textHeight * Math.cos(angle)); + if (repeatWatermark.value) { - // Step 1: Create measurement context - const measureCanvas = document.createElement('canvas'); - const measureCtx = measureCanvas.getContext('2d')!; - measureCtx.font = `${fontSize.value}px sans-serif`; + // Create single watermark tile canvas + const tileWidth = textWidth + tileSpacing.value; + const tileHeight = textHeight + tileSpacing.value; - const textMetrics = measureCtx.measureText(watermarkText.value); - const textWidth = textMetrics.width; - const textHeight = fontSize.value; - const angle = (rotation.value * Math.PI) / 180; - - // Step 2: Compute bounding box for rotated text - const rotatedWidth = Math.abs(textWidth * Math.cos(angle)) + Math.abs(textHeight * Math.sin(angle)); - const rotatedHeight = Math.abs(textWidth * Math.sin(angle)) + Math.abs(textHeight * Math.cos(angle)); - - const paddingX = 40; - const paddingY = 30; - - const tileWidth = rotatedWidth + paddingX; - const tileHeight = rotatedHeight + paddingY; - - // Step 3: Create offscreen canvas const offCanvas = document.createElement('canvas'); const offCtx = offCanvas.getContext('2d')!; offCanvas.width = tileWidth; @@ -138,21 +137,61 @@ function drawImage(index: number) { offCtx.textAlign = 'center'; offCtx.textBaseline = 'middle'; offCtx.translate(tileWidth / 2, tileHeight / 2); - offCtx.rotate(angle); offCtx.fillText(watermarkText.value, 0, 0); if (logo.value) { drawLogo(offCtx, textHeight / 2); } - const pattern = ctx.createPattern(offCanvas, 'repeat'); - ctx.fillStyle = pattern!; - ctx.fillRect(0, 0, img.width, img.height); + // create a repeated watermark canvas that covers image in diagonal + const imageHypotenuseLength = Math.sqrt(img.width * img.width + img.height * img.height); + const watermarkCanvas = document.createElement('canvas'); + const watermarkCtx = watermarkCanvas.getContext('2d')!; + watermarkCanvas.width = imageHypotenuseLength; + watermarkCanvas.height = imageHypotenuseLength; + + watermarkCtx.clearRect(0, 0, imageHypotenuseLength, imageHypotenuseLength); + const pattern = watermarkCtx.createPattern(offCanvas, 'repeat'); + watermarkCtx.fillStyle = pattern!; + watermarkCtx.fillRect(0, 0, imageHypotenuseLength, imageHypotenuseLength); + + ctx.save(); + ctx.translate(img.width / 2, img.height / 2); + ctx.rotate(angle); + + ctx.drawImage(watermarkCanvas, -watermarkCanvas.width / 2, -watermarkCanvas.height / 2); + + ctx.restore(); } else { ctx.save(); - ctx.translate(img.width / 2, img.height / 2); - ctx.rotate((rotation.value * Math.PI) / 180); + + let posX = img.width / 2; + let posY = img.height / 2; + const margin = tileSpacing.value; + + switch (watermarkPosition.value) { + case 'top-left': + posX = margin + rotatedWidth / 2; + posY = margin + rotatedHeight / 2; + break; + case 'top-right': + posX = img.width - margin - rotatedWidth / 2; + posY = margin + rotatedHeight / 2; + break; + case 'bottom-left': + posX = margin + rotatedWidth / 2; + posY = img.height - margin - rotatedHeight / 2; + break; + case 'bottom-right': + posX = img.width - margin - rotatedWidth / 2; + posY = img.height - margin - rotatedHeight / 2; + break; + // default is center + } + + ctx.translate(posX, posY); + ctx.rotate(angle); drawLogo(ctx); @@ -249,17 +288,36 @@ watchEffect(() => drawImage(selectedIndex.value)); + + + + - - - - - + + + + + + + + +