Skip to content

Commit 165ce58

Browse files
committed
feat:水印针对黑白主题的适配
1 parent 66a062c commit 165ce58

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/hooks/web/useWatermark.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
import { useAppStore } from '@/store/modules/app'
2+
import { watch } from 'vue'
3+
14
const domSymbol = Symbol('watermark-dom')
25

36
export function useWatermark(appendEl: HTMLElement | null = document.body) {
47
let func: Fn = () => {}
58
const id = domSymbol.toString()
9+
const appStore = useAppStore()
10+
let watermarkStr = ''
11+
612
const clear = () => {
713
const domId = document.getElementById(id)
814
if (domId) {
@@ -22,7 +28,7 @@ export function useWatermark(appendEl: HTMLElement | null = document.body) {
2228
if (cans) {
2329
cans.rotate((-20 * Math.PI) / 120)
2430
cans.font = '15px Vedana'
25-
cans.fillStyle = 'rgba(0, 0, 0, 0.15)'
31+
cans.fillStyle = appStore.getIsDark ? 'rgba(255, 255, 255, 0.15)' : 'rgba(0, 0, 0, 0.15)'
2632
cans.textAlign = 'left'
2733
cans.textBaseline = 'middle'
2834
cans.fillText(str, can.width / 20, can.height)
@@ -44,12 +50,23 @@ export function useWatermark(appendEl: HTMLElement | null = document.body) {
4450
}
4551

4652
function setWatermark(str: string) {
53+
watermarkStr = str
4754
createWatermark(str)
4855
func = () => {
4956
createWatermark(str)
5057
}
5158
window.addEventListener('resize', func)
5259
}
5360

61+
// 监听主题变化
62+
watch(
63+
() => appStore.getIsDark,
64+
() => {
65+
if (watermarkStr) {
66+
createWatermark(watermarkStr)
67+
}
68+
}
69+
)
70+
5471
return { setWatermark, clear }
5572
}

0 commit comments

Comments
 (0)