js实现canvas闪电式雕刻文字表白动画效果代码

代码语言:html

所属分类:表白

代码描述:js实现canvas闪电式雕刻文字表白动画效果代码,可在下方输入框输入文字进行闪电告白。

代码标签: canvas 闪电 文字 表白

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="UTF-8">




    <style>
        .page-thunder-to-text {
          position: relative;
          overflow: hidden;
        }
        .page-thunder-to-text canvas {
          display: block;
        }
        .page-thunder-to-text input {
          position: absolute;
          bottom: 50px;
          left: 0;
          right: 0;
          display: block;
          outline: none;
          background-color: rgba(38, 50, 56, 0.2);
          color: #ffffff;
          border: none;
          width: 50%;
          min-width: 500px;
          max-width: 100%;
          margin: auto;
          height: 60px;
          line-height: 60px;
          font-size: 40px;
          padding: 0 20px;
        }
        .page-thunder-to-text input:hover, .page-thunder-to-text input:focus {
          border: 1px solid rgba(38, 50, 56, 0.6);
        }
        .page-thunder-to-text input::-webkit-input-placeholder {
          color: rgba(255, 255, 255, 0.1);
        }
    </style>




</head>

<body>
    <div class="page page-thunder-to-text">
        <input id="input" type="text" maxlength="24" placeholder="I love you!">
        <canvas id="canvas"></canvas>
    </div>


    <script>
        let canvas, ctx, w, h, thunder, text, particles, input;
        
        function Thunder(options) {
          options = options || {};
          this.lifespan = options.lifespan || Math.round(Math.random() * 10 + 10);
          this.maxlife = this.lifespan;
          this.color = options.color || '#fefefe';
          this.glow = options.glow || '#2323fe';
          this.x = options.x || Math.random() * w;
          this.y = options.y || Math.random() * h;
          this.width = options.width || 2;
          this.direct = options.direct || Math.random() * Math.PI * 2;
          this.max = options.max || Math.round(Math.random() * 10 + 20);
          this.segments = [...new Array(this.max)].map(() => {
            return {
              direct: this.direct + (Math.PI * Math.random() * 0.2 - 0.1),
              length: Math.random() * 20 + 80,
              change: Math.random() * 0.04 - 0.02 };
        
          });
        
          this.update = function (index, array) {
            this.segments.forEach(s => {(s.direct += s.change) && Math.random() > 0.96 && (s.change *= -1);});
            this.lifespan > 0 && this.lifespan-- || this.remove(index, array);
          };
        
          this.render = function (ctx) {
            if (this.lifespan <= 0) return;
            ctx.beginPath();
            ctx.globalAlpha = this.lifespan / this.maxlife;
            ctx.strokeStyle = this.color;
            ctx.lineWidth = this.width;
            ctx.shadowBlur = 32;
            ctx.shadowColor = this.glow;
            ctx.moveTo(this.x, this.y);
            let prev = { x: this.x, y: this.y };
            this.segments.forEach(s => {
              const x = prev.x + Math.cos(s.direct) * s.length;
              const y = prev.y + Math.sin(s.direct) * s.length;
              prev = { x: x, y: y };
              ctx.lineTo(x, y);
            });
            ctx.stroke();
            ctx.closePath();
            ctx.shadowBlur = 0;
            const strength = Math.random() * 80 + 40;
            const light = ctx.createRadialGradient(this.x, this.y, 0, this.x, this.y, strength);
            light.addColorStop(0, 'rgba(250, 200, 50, 0.6)');
            light.addColorStop(0.1, 'rgba(250, 200, 50, 0.2)');
            light.addColorStop(0.4, 'rgba(250, 200, 50, 0.06)');
            light.addColorStop(0.65, 'rgba(250, 200, 50, 0.01)');
            light.addColorStop(0.8, 'rgba(250, 200, 50, 0)');
            ctx.beginPath();
            ctx.fillStyle = light;
            ctx.arc(this.x, this.y, strength, 0, Math.PI * 2);
            ctx.fill();
            ctx.closePath();
          };
        
          this.remove = function (index, array) {
            array.splice(index, 1);
          };
        }
        
        function Spark(options) {
          options = options || {};
          this.x = options.x || w * 0.5;
          this.y = options.y || h * 0.5;
          this.v = options.v || { direct: Math.random() * Math.PI * 2, weight: Math.random() * 14 + 2, friction: 0.88 };
          this.a = options.a || { change: Math.random() * 0.4 - 0.2, min: this.v.direct - Math.PI * 0.4, max: this.v.direct + Math.PI * 0.4 };
          this.g = options.g || { direct: Math.PI * 0.5 + (Math.random() * 0.4 - 0.2), weight: Math.random() * 0.25 + 0.25 };
          this.width = options.width || Math.random() * 3;
          this.lifespan = options.lifespan || Math.round(Math.random() * 20 + 40);
          this.maxlife = this.lifespan;
          this.color = options.color || '#feca32';
          this.prev = { x: this.x, y: this.y };
        
          this.update = function (index, array) {
            this.prev = { x: this.x, y: this.y };
            this.x += Math.cos(this.v.direct) * this.v.weight;
            this.x += Math.cos(this.g.direct) * this.g.weight;
            this.y += Math.sin(this.v.direct) * this.v.weight;
            this.y += Math.sin(this.g.direct) * this.g.weight;
            this.v.weight > 0.2 && (this.v.weight *= this.v.frict.........完整代码请登录后点击上方下载按钮下载查看

网友评论1

  1. # 98
    怎么修改
    166790300461574 2022-11-08回复