mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-22 05:51:44 +03:00
feat(Watermarker): add options for repeated tile spacing and single watermark positioning
Fix https://github.com/sharevb/it-tools/issues/103#issuecomment-2994950670
This commit is contained in:
1 parent
a12c43bfae
commit
a78201bffd
1 file changed
+89
-31
@@ -28,6 +28,8 @@ const opacity = useQueryParamOrStorage({ name: 'opa', storageName: 'watermarker:
|
|||||||
const rotation = useQueryParamOrStorage({ name: 'rot', storageName: 'watermarker:r', defaultValue: 45 });
|
const rotation = useQueryParamOrStorage({ name: 'rot', storageName: 'watermarker:r', defaultValue: 45 });
|
||||||
const repeatWatermark = useQueryParamOrStorage({ name: 'repeat', storageName: 'watermarker:rep', defaultValue: false });
|
const repeatWatermark = useQueryParamOrStorage({ name: 'repeat', storageName: 'watermarker:rep', defaultValue: false });
|
||||||
const logoWidth = useQueryParamOrStorage({ name: 'logow', storageName: 'watermarker:lw', defaultValue: 60 });
|
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<HTMLImageElement | null>(null);
|
const logo = ref<HTMLImageElement | null>(null);
|
||||||
const downloadFormat = ref('png');
|
const downloadFormat = ref('png');
|
||||||
@@ -105,28 +107,25 @@ function drawImage(index: number) {
|
|||||||
ctx.drawImage(img, 0, 0);
|
ctx.drawImage(img, 0, 0);
|
||||||
ctx.globalAlpha = opacity.value / 100;
|
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) {
|
if (repeatWatermark.value) {
|
||||||
// Step 1: Create measurement context
|
// Create single watermark tile canvas
|
||||||
const measureCanvas = document.createElement('canvas');
|
const tileWidth = textWidth + tileSpacing.value;
|
||||||
const measureCtx = measureCanvas.getContext('2d')!;
|
const tileHeight = textHeight + tileSpacing.value;
|
||||||
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;
|
|
||||||
|
|
||||||
// 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 offCanvas = document.createElement('canvas');
|
||||||
const offCtx = offCanvas.getContext('2d')!;
|
const offCtx = offCanvas.getContext('2d')!;
|
||||||
offCanvas.width = tileWidth;
|
offCanvas.width = tileWidth;
|
||||||
@@ -138,21 +137,61 @@ function drawImage(index: number) {
|
|||||||
offCtx.textAlign = 'center';
|
offCtx.textAlign = 'center';
|
||||||
offCtx.textBaseline = 'middle';
|
offCtx.textBaseline = 'middle';
|
||||||
offCtx.translate(tileWidth / 2, tileHeight / 2);
|
offCtx.translate(tileWidth / 2, tileHeight / 2);
|
||||||
offCtx.rotate(angle);
|
|
||||||
offCtx.fillText(watermarkText.value, 0, 0);
|
offCtx.fillText(watermarkText.value, 0, 0);
|
||||||
|
|
||||||
if (logo.value) {
|
if (logo.value) {
|
||||||
drawLogo(offCtx, textHeight / 2);
|
drawLogo(offCtx, textHeight / 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
const pattern = ctx.createPattern(offCanvas, 'repeat');
|
// create a repeated watermark canvas that covers image in diagonal
|
||||||
ctx.fillStyle = pattern!;
|
const imageHypotenuseLength = Math.sqrt(img.width * img.width + img.height * img.height);
|
||||||
ctx.fillRect(0, 0, img.width, 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 {
|
else {
|
||||||
ctx.save();
|
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);
|
drawLogo(ctx);
|
||||||
|
|
||||||
@@ -249,17 +288,36 @@ watchEffect(() => drawImage(selectedIndex.value));
|
|||||||
<n-form-item label="Color:" label-placement="left" mt-2>
|
<n-form-item label="Color:" label-placement="left" mt-2>
|
||||||
<NColorPicker v-model:value="fontColor" style="width:100px" />
|
<NColorPicker v-model:value="fontColor" style="width:100px" />
|
||||||
</n-form-item>
|
</n-form-item>
|
||||||
|
<n-form-item label="Rotation (°):" label-placement="left" mt-2>
|
||||||
|
<NSlider v-model:value="rotation" :step="0.5" :min="0" :max="360" mr-2 />
|
||||||
|
<n-input-number v-model:value="rotation" size="small" :min="0" :max="360" />
|
||||||
|
</n-form-item>
|
||||||
</NSpace>
|
</NSpace>
|
||||||
|
|
||||||
<NSpace justify="center">
|
<NSpace justify="center">
|
||||||
<n-form-item label="Rotation (°):" label-placement="left" mt-2>
|
|
||||||
<NSlider v-model:value="rotation" :step="0.5" :min="0" :max="360" mr-2 />
|
|
||||||
<n-input-number v-model:value="rotation" size="small" :min="1" :max="360" />
|
|
||||||
</n-form-item>
|
|
||||||
|
|
||||||
<n-form-item label="Repeat Watermark:" label-placement="left" mt-2>
|
<n-form-item label="Repeat Watermark:" label-placement="left" mt-2>
|
||||||
<NSwitch v-model:value="repeatWatermark" />
|
<NSwitch v-model:value="repeatWatermark" />
|
||||||
</n-form-item>
|
</n-form-item>
|
||||||
|
|
||||||
|
<n-form-item :label="repeatWatermark ? 'Tile spacing:' : 'Margin:'" label-placement="left" mt-2>
|
||||||
|
<NSlider v-model:value="tileSpacing" :step="1" :min="1" :max="1000" mr-2 />
|
||||||
|
<n-input-number v-model:value="tileSpacing" size="small" :min="1" :max="1000" />
|
||||||
|
</n-form-item>
|
||||||
|
|
||||||
|
<n-form-item v-if="!repeatWatermark" label="Position:" label-placement="left" mt-2>
|
||||||
|
<NSelect
|
||||||
|
v-model:value="watermarkPosition"
|
||||||
|
style="min-width: 130px"
|
||||||
|
:options="[
|
||||||
|
{ label: 'Center', value: 'center' },
|
||||||
|
{ label: 'Top Left', value: 'top-left' },
|
||||||
|
{ label: 'Top Right', value: 'top-right' },
|
||||||
|
{ label: 'Bottom Left', value: 'bottom-left' },
|
||||||
|
{ label: 'Bottom Right', value: 'bottom-right' },
|
||||||
|
]"
|
||||||
|
placeholder="Watermark Position"
|
||||||
|
/>
|
||||||
|
</n-form-item>
|
||||||
</NSpace>
|
</NSpace>
|
||||||
|
|
||||||
<c-card title="Optional Logo">
|
<c-card title="Optional Logo">
|
||||||
|
|||||||
Reference in new issue
Block a user