送你 爱心代码,做懂浪漫的程序猿---【赋码源】

10 篇文章 3 订阅
4 篇文章 2 订阅

 看完效果,咱话不多少,咱们直入主题!

CSS部分:

    * {
      padding: 0;
      margin: 0;
    }
    html,
    body {
      height: 100%;
      padding: 0;
      margin: 0;
      background: #000;
    }

    .container {
      width: 100%;
      height: 100%;
      position: relative;
    }
    canvas {
      z-index: 99;
      position: absolute;
      width: 100%;
      height: 100%;
    }
    .txtName {
      color: rgb(247, 85, 233);
      font-size: 30px;
      position: absolute;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
    }

HTML部分:

中间名字可自行替换


    <div id="flower" class="container">
      <!-- 中间区域显示,可自行定义 -->
      <text class="txtName">XX I Love You</text>
      <!-- 爱心 -->
      <canvas id="pinkHeart" class="container"> </canvas>
    </div>

JavaScript部分:

  var setUp = {
    particles: {
      length: 500,
      duration: 2,
      velocity: 100,
      effect: -0.75,
      size: 30,
    },
  };

  var Point = (function () {
    function Point(x, y) {
      this.x = typeof x !== "undefined" ? x : 0;
      this.y = typeof y !== "undefined" ? y : 0;
    }
    Point.prototype.clone = function () {
      return new Point(this.x, this.y);
    };
    Point.prototype.length = function (length) {
      if (typeof length == "undefined")
        return Math.sqrt(this.x * this.x + this.y * this.y);
      this.normalize();
      this.x *= length;
      this.y *= length;
      return this;
    };
    Point.prototype.normalize = function () {
      var length = this.length();
      this.x /= length;
      this.y /= length;
      return this;
    };
    return Point;
  })();

  var Particle = (function () {
    function Particle() {
      this.position = new Point();
      this.velocity = new Point();
      this.acceleration = new Point();
      this.age = 0;
    }
    Particle.prototype.initialize = function (x, y, dx, dy) {
      this.position.x = x;
      this.position.y = y;
      this.velocity.x = dx;
      this.velocity.y = dy;
      this.acceleration.x = dx * setUp.particles.effect;
      this.acceleration.y = dy * setUp.particles.effect;
      this.age = 0;
    };
    Particle.prototype.update = function (deltaTime) {
      this.position.x += this.velocity.x * deltaTime;
      this.position.y += this.velocity.y * deltaTime;
      this.velocity.x += this.acceleration.x * deltaTime;
      this.velocity.y += this.acceleration.y * deltaTime;
      this.age += deltaTime;
    };
    Particle.prototype.draw = function (context, image) {
      function ease(t) {
        return --t * t * t + 1;
      }
      var size = image.width * ease(this.age / setUp.particles.duration);
      context.globalAlpha = 1 - this.age / setUp.particles.duration;
      context.drawImage(
        image,
        this.position.x - size / 2,
        this.position.y - size / 2,
        size,
        size
      );
    };
    return Particle;
  })();

  var Petal = (function () {
    var particles,
      firstActive = 0,
      firstFree = 0,
      duration = setUp.particles.duration;

    function Petal(length) {
      particles = new Array(length);
      for (var i = 0; i < particles.length; i++) particles[i] = new Particle();
    }
    Petal.prototype.add = function (x, y, dx, dy) {
      particles[firstFree].initialize(x, y, dx, dy);

      firstFree++;
      if (firstFree == particles.length) firstFree = 0;
      if (firstActive == firstFree) firstActive++;
      if (firstActive == particles.length) firstActive = 0;
    };
    Petal.prototype.update = function (deltaTime) {
      var i;

      if (firstActive < firstFree) {
        for (i = firstActive; i < firstFree; i++)
          particles[i].update(deltaTime);
      }
      if (firstFree < firstActive) {
        for (i = firstActive; i < particles.length; i++)
          particles[i].update(deltaTime);
        for (i = 0; i < firstFree; i++) particles[i].update(deltaTime);
      }

      while (
        particles[firstActive].age >= duration &&
        firstActive != firstFree
      ) {
        firstActive++;
        if (firstActive == particles.length) firstActive = 0;
      }
    };
    Petal.prototype.draw = function (context, image) {
      if (firstActive < firstFree) {
        for (i = firstActive; i < firstFree; i++)
          particles[i].draw(context, image);
      }
      if (firstFree < firstActive) {
        for (i = firstActive; i < particles.length; i++)
          particles[i].draw(context, image);
        for (i = 0; i < firstFree; i++) particles[i].draw(context, image);
      }
    };
    return Petal;
  })();

  (function (canvas) {
    var context = canvas.getContext("2d"),
      particles = new Petal(setUp.particles.length),
      particleRate = setUp.particles.length / setUp.particles.duration,
      time;

    function pointOnHeart(a) {
      return new Point(
        160 * Math.pow(Math.sin(a), 3),
        130 * Math.cos(a) -
          50 * Math.cos(2 * a) -
          20 * Math.cos(3 * a) -
          10 * Math.cos(4 * a) +
          25
      );
    }

    var image = (function () {
      var canvas = document.createElement("canvas"),
        context = canvas.getContext("2d");
      canvas.width = setUp.particles.size;
      canvas.height = setUp.particles.size;
      function to(t) {
        var point = pointOnHeart(t);
        point.x =
          setUp.particles.size / 2 + (point.x * setUp.particles.size) / 350;
        point.y =
          setUp.particles.size / 2 - (point.y * setUp.particles.size) / 350;
        return point;
      }
      context.beginPath();
      var t = -Math.PI;
      var point = to(t);
      context.moveTo(point.x, point.y);
      while (t < Math.PI) {
        t += 0.01;
        point = to(t);
        context.lineTo(point.x, point.y);
      }
      context.closePath();
      context.fillStyle = "#ea80b0";
      context.fill();
      var image = new Image();
      image.src = canvas.toDataURL();
      return image;
    })();

    function render() {
      requestAnimationFrame(render);

      var newTime = new Date().getTime() / 1000,
        deltaTime = newTime - (time || newTime);
      time = newTime;

      context.clearRect(0, 0, canvas.width, canvas.height);

      var amount = particleRate * deltaTime;
      for (var i = 0; i < amount; i++) {
        var pos = pointOnHeart(Math.PI - 2 * Math.PI * Math.random());
        var dir = pos.clone().length(setUp.particles.velocity);
        particles.add(
          canvas.width / 2 + pos.x,
          canvas.height / 2 - pos.y,
          dir.x,
          -dir.y
        );
      }

      particles.update(deltaTime);
      particles.draw(context, image);
    }

    function onResize() {
      canvas.width = canvas.clientWidth;
      canvas.height = canvas.clientHeight;
    }
    window.onresize = onResize;

    setTimeout(function () {
      onResize();
      render();
    }, 10);
  })(document.getElementById("pinkHeart"));

 

 就这样,爱心代码就大功告成!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旺旺大力包

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值