FxWatermark 水印
在页面或容器上覆盖水印(文本 / 多行 / 图片 / 富文本),支持防删除、暗水印。基于 watermark-js-plus,不依赖任何 UI 框架。
FxWatermark 属于 @fx/components。
基础用法
<FxWatermark> 作为容器包裹内容,水印画在容器内。
多行文本
content-type="multi-line-text",content 用 (换行符)分隔多行。
图片水印
content-type="image",image 传图片地址。
富文本
content-type="rich-text",content 传 HTML 片段。
描边水印
text-type="stroke" 用描边方式绘制文字(默认 fill 填充),适合需要强对比的场景。
网格布局
layout="grid" + grid-layout-options。
防删除 + 动效
mutation-observe 监听 DOM 篡改自动恢复;monitor-protection 开启保护;movable 平移动效。
暗水印
blind 开启暗水印(肉眼几乎不可见)。配合 FxWatermarkDecoder 解析还原。
全局水印
不绑定容器,用 createWatermark(options, mode?) 盖全屏,返回库实例,自行 .create() / .destroy()。
局部引入
ts
import {
FxWatermark,
createWatermark,
decodeBlindWatermark,
Watermark,
BlindWatermark,
} from "@fx/components"API
Attributes
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| blind | 暗水印(内部用 BlindWatermark) | boolean | false |
| disabled | 禁用水印(销毁已绘制水印;false 恢复重绘)——常驻包裹 + 开关显隐场景用 | boolean | false |
| content | 内容(文本/多行/富文本) | string | — |
| content-type | 内容类型 | 'text' | 'image' | 'multi-line-text' | 'rich-text' | 'text' |
| image | 图片水印地址(content-type=image) | string | — |
| width / height | 水印画布宽高 | number | 库默认 |
| rotate | 旋转角度 | number | 库默认 |
| global-alpha | 透明度 | number | 库默认 |
| z-index | 层级 | number | 库默认 |
| text-type | 文本绘制方式(填充/描边) | 'fill' | 'stroke' | 'fill' |
| font-size / font-color / font-weight / font-family | 字体 | string | 库默认 |
| font-style / font-variant | 斜体/小型大写字母等 | string | '' |
| line-height | 行高(多行文本间距) | number | 30 |
| letter-spacing / word-spacing | 字间距/词间距 | string | '0px' |
| text-align | 水平排布 | 'start' | 'center' | 'end' | 'left' | 'right' | 随定位 |
| text-baseline | 垂直基线 | 'top' | 'middle' | 'bottom' | 'alphabetic' | … | 随定位 |
| text-row-max-width | 单行最大宽度(多行自动换行) | number | 同 width |
| rich-text-width / rich-text-height | 富文本宽高(content-type=rich-text) | number | — |
| layout | 布局 | 'default' | 'grid' | 'default' |
| grid-layout-options | 网格布局 | GridLayoutOptions | — |
| auxiliary-line | 辅助线(调试画布边界) | boolean | false |
| background-position | 背景定位 | string | '0 0' |
| background-repeat | 背景重复 | string | 'repeat' |
| movable | 平移动效 | boolean | false |
| mutation-observe | DOM 篡改监听(防删除) | boolean | true |
| monitor-protection | 保护 | boolean | false |
| translate-placement / translate-x / translate-y | 定位 | — | 库默认 |
| image-width / image-height | 图片水印尺寸 | number | — |
| advanced-style / shadow-style / extra-draw-func / filter | 高级绘制 | — | — |
完整字段见 watermark-js-plus WatermarkOptions。
parent/mode不对外(parent 固定根元素,mode 由 blind 映射)。
Events
| 事件 | 说明 | 载荷 |
|---|---|---|
| success | 创建成功 | — |
| before-destroy | 销毁前 | — |
| destroyed | 销毁后 | — |
| observe-error | MutationObserver 失败 | — |
Slots
| 名称 | 说明 |
|---|---|
| default | 水印覆盖的子内容 |
Exposes
| 名称 | 说明 | 类型 |
|---|---|---|
| create | 创建水印 | () => Promise<void> |
| destroy | 销毁水印 | () => void |
| check | 检查水印是否存在 | () => Promise<boolean> |
| changeOptions | 更新配置 | (args, mode?, redraw?) => Promise<void> |
全局 API
createWatermark(options, mode?)
创建水印实例(全局层面),返回库实例。
ts
const wm = createWatermark({ content: "悦安", globalAlpha: 0.5 })
wm.create()
wm.changeOptions({ content: "新文字" })
wm.destroy()
// 暗水印
const blind = createWatermark({ content: "secret" }, "blind")
blind.create()| 参数 | 类型 | 说明 |
|---|---|---|
| options | WatermarkOptions | watermark-js-plus 配置 |
| mode | 'default' | 'blind' | 缺省 'default' |
同时 re-export 库原始类:Watermark / BlindWatermark / ImageWatermark,可直接 new Watermark(opts).create()。
