自己用 canvas 制作简易的水印效果

效果图

在这里插入图片描述

js

function waterLine(data = {
    container:'', // 容器元素
    text:'', // 文字内容
    fontSize: 14, // 文字字体大小
    color: '#000000', // 文字眼色
    rotate: 0, // 画布旋转角度
    cWidth: 0, // 画布宽度
    cHeight: 0 // 画布高度
  }) {
    let {container, text, fontSize, color, rotate, cWidth, cHeight} = data;
    let canvas = container;
    canvas.style = `width: ${cWidth}px;height:${cHeight}px;`;
    canvas.width = cWidth;
    canvas.height = cHeight;
    let cxt = canvas.getContext("2d");
    cxt.rotate(rotate * Math.PI/180);
    for(let j = -30; j < 50; j++) {
      for(let i = -30; i < 50; i++) {
        cxt.font = `${fontSize}px PingFangSC-Regular,PingFang SC,sans-serif`;
        cxt.fillStyle = color;
        cxt.fillText(text, i*fontSize*(text.length + Math.floor(text.length/2)) + j*fontSize*Math.ceil(text.length/2), (j + 1) * 40);
      }
    }
  }

  waterLine({container: document.getElementById('myCanvas'), text: '呔,你这厮~', fontSize: 16, color: '#E9EAEC', rotate: 45, cWidth: 1024, cHeight: 2000});

html

<div class="content">
  <div class="list"></div>
  <canvas id="myCanvas" ></canvas>
</div>

css

.content {
  position: relative;
  overflow: hidden;
  background: #F5F6F8;
}
.list {
  width: 100%;
  height: 100vh;
}

#myCanvas {
  width: 100%;
  height: 100%;
  border:1px solid #d3d3d3;
  position: absolute;
  top: 0;
  left: 0;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值