Appearance
useWatermark
说明
设置防篡改水印
参数
typescript
type waterMarkOptions = {
// 自定义水印的文字大小
fontSize?: number;
// 自定义水印的文字颜色
fontColor?: string;
// 自定义水印的文字字体
fontFamily?: string;
// 自定义水印的文字对齐方式
textAlign?: CanvasTextAlign;
// 自定义水印的文字基线
textBaseline?: CanvasTextBaseline;
// 自定义水印的文字倾斜角度
rotate?: number;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
使用
默认添加到
document.body
,如果添加到其他元素要保证添加水印的元素(此例中为#app
)的position
属性设为relative
或者absolute
,这样水印才能正确定位。可按需调整
waterMarkOptions
对象里的配置选项,例如fontSize
、fontColor
、rotate
等。
vue
<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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36