This commit is contained in:
kakkokari-gtyih 2024-12-16 10:48:04 +09:00
parent d5ca458c40
commit d2b2422697

View file

@ -42,6 +42,19 @@ export function applyWatermark(img: string | Blob, el: HTMLCanvasElement, config
watermark.onload = () => {
const width = config.width || watermark.width;
const height = config.height || watermark.height;
ctx.globalAlpha = config.opacity;
if (config.repeat !== false) {
const resizedWatermark = document.createElement('canvas');
resizedWatermark.width = width;
resizedWatermark.height = height;
const resizedCtx = resizedWatermark.getContext('2d')!;
resizedCtx.drawImage(watermark, 0, 0, width, height);
const pattern = ctx.createPattern(resizedWatermark, config.repeat === true ? 'repeat' : `repeat-${config.repeat}`);
if (pattern) {
ctx.fillStyle = pattern;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
} else {
const x = (() => {
switch (config.anchor) {
case 'center':
@ -74,8 +87,8 @@ export function applyWatermark(img: string | Blob, el: HTMLCanvasElement, config
return canvas.height - height;
}
})();
ctx.globalAlpha = config.opacity;
ctx.drawImage(watermark, x, y, width, height);
}
};
watermark.src = config.fileUrl;
}