本页导航
article
useWatermark
AI摘要
本文介绍了useWatermark工具,其主要功能是为网页元素添加防篡改水印。文章核心说明了使用方法:水印默认添加至document.body,若需添加至其他元素(如#app),则必须确保该元素的定位属性为relative或absolute,以保证水印正确显示。同时,用户可通过调整配置选项(如字体大小、颜色、旋转角度等)来自定义水印样式。
说明
设置防篡改水印
参数
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>
最后更新于 2026-02-17 19:41