svg轻松实现文字水印

1. 水印图片生成采用svg,这样可以运行时生成名字或其他信息的图片

svg模板

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="300px" height="160px" viewBox="0 0 300 160">
    <text x="-100" y="-30" fill='#000'  transform = "rotate(-35 240 -200)" fill-opacity='0.03' font-size='40'>I love SVG</text>
</svg>

一定要记得设置透明度(fill-opacity),否则水印会遮挡正文

svg需要转码: https://codepen.io/huashiyiqike/pen/rERBeJ

2. 通过css覆盖在整个页面上

水印元素放到页面大小的容器的最后面

父元素:
position: relative;
水印元素
position: absolute; top: 0; bottom: 0; // 整体覆盖 left: 0; width: 100%; height: 100%; z-index: 100; pointer-events: none; // 对鼠标穿透

3. 通过js拼接svg,并写入模板或者html

js: 
this.watermarkstyle = `background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' width='200px' height='160px' viewBox='0 0 200 160'%3E %3Ctext x='-100' y='-30' fill='%23000' transform = 'rotate(-35 240 -200)' fill-opacity='0.04' font-size='40'%3E${ this.$store.state.authUser.username }%3C/text%3E %3C/svg%3E ")`

html:
<div class="watermark" :style="watermarkstyle"></div>

 

转载于:https://www.cnblogs.com/huashiyiqike/p/11171790.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值