canvas水印

<template>
  <div class="water">
    <canvas id="canvas"></canvas>
  </div>
</template>

<script>

export default {
  name: "Water",
  mounted() {
    var canvas = document.getElementById("canvas");
    var width = window.innerWidth;
    var height = window.innerHeight;
    canvas.setAttribute("width", width + "px");
    canvas.setAttribute("height", height + "px");
    var ctx = canvas.getContext("2d");
    var word1 = "王小明";
    var word2 = "15168836829";
    var word = word1 + word2;
    var fontSize = 20;
    var fontLength =
      word1.length * fontSize + (word2.length * fontSize) / 2 - 5;
    var widthStep = width / fontLength;
    var heigtStep = height / fontSize;
    ctx.fillStyle = "blue";

    function rotateContext(ctx, x, y, degree) {
      ctx.translate(x, y);
      ctx.rotate((degree * Math.PI) / 180);
      ctx.translate(-x, -y);
    }
    for (let i = 0; i < heigtStep; i++) {
      for (let j = 0; j < widthStep; j++) {
        ctx.save();
        ctx.beginPath();
        ctx.font = fontSize + "px Arial";
        let x = j * fontLength;
        let y = i * (fontSize + 60);
        rotateContext(ctx, x, y, -35);
        ctx.fillText(word, x, y);
        ctx.restore();
      }
    }
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->

<style scoped>
.water {
  position: fixed;
  width: 100%;
  height: 100%;
  opacity: 0.13;
  z-index: 9999999;
  top: 0;
  left: 0;
  pointer-events: none;
}
</style>

猜你喜欢

转载自blog.csdn.net/liuhao9999/article/details/115167488