本页导航
article
useWatermark
AI摘要
本文介绍了 useWatermark 函数的使用方法及配置参数,重点说明了水印添加的默认位置和元素定位要求,同时通过 Vue 示例演示了如何添加和移除自定义水印文本,方便开发者实现防篡改水印功能。
说明
设置防篡改水印
参数
type waterMarkOptions = {
// 自定义水印的文字大小
fontSize?: number;
// 自定义水印的文字颜色
fontColor?: string;
// 自定义水印的文字字体
fontFamily?: string;
// 自定义水印的文字对齐方式
textAlign?: CanvasTextAlign;
// 自定义水印的文字基线
textBaseline?: CanvasTextBaseline;
// 自定义水印的文字倾斜角度
rotate?: number;
};
使用
-
默认添加到
document.body
,如果添加到其他元素要保证添加水印的元素(此例中为#app
)的position
属性设为relative
或者absolute
,这样水印才能正确定位。 -
可按需调整
waterMarkOptions
对象里的配置选项,例如fontSize
、fontColor
、rotate
等。
<template>
<div id="app">
<h1>水印示例</h1>
<button @click="addWatermark">添加水印</button>
<button @click="removeWatermark">移除水印</button>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { useWatermark } from 'liyao-vue-common';
const appRef = ref<HTMLElement | null>(null);
const { setWatermark, clear } = useWatermark(appRef, {
// waterMarkOptions...
fontSize: 16,
fontColor: 'rgba(0, 0, 0, 0.2)',
rotate: 30
});
const addWatermark = () => {
setWatermark('自定义水印文本');
};
const removeWatermark = () => {
clear();
};
</script>
<style scoped>
#app {
position: relative;
padding: 20px;
}
</style>
最后更新于 2025-09-30 20:31