表白-love

飞扬的心 

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>心形,我心飞扬</title>
  <script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
  <style>
     body {
          margin:0;
          overflow:hidden;
          background:#303035;
    }
  </style>
</head>
<body>
<canvas></canvas>

<script>
  var canvas = document.querySelector("canvas");
  var ctx = canvas.getContext("2d");
  //初始化页面新的数量
  var num = 100;
  var ww = window.innerWidth;
  var wh = window.innerHeight;

  var hearts = [];

  function init() {
    requestAnimationFrame(render);
    canvas.width = ww;
    canvas.height = wh;
    for (var i = 0; i < num; i++) {
      hearts.push(new Heart());
    }
    //      console.log(hearts)
  }
  var timeaur;
  $("body").on("mousedown", function(event) {
    var e = event || window.event;
    var x = e.clientX,
            y = e.clientY;
    timeaur = setInterval(function() {
      hearts.push(new Heart(x, y));
      num++
    }, 100)
    $("body").on("mousemove", function(event) {
      var e = event || window.event;
      var x = e.clientX,
              y = e.clientY;
      hearts.push(new Heart(x, y));
      num++
    })
  })

  $("body").on("mouseup", function() {
    clearInterval(timeaur)
    $("body").off("mousemove")
  })

  function Heart(x, y) {
    if (x) {
      this.x = x;
      this.y = y;
    } else {
      this.x = Math.random() * ww;
      this.y = Math.random() * wh;
    }

    this.opacity = (Math.random() * 0.5) + 0.5;
    this.vel = {
      x: (Math.random() - 0.5) * 4,
      y: (Math.random() - 0.5) * 4
    };
    this.targetScale = (Math.random() * 0.5) + 0.02;
    this.scale = this.targetScale * Math.random();
  }

  Heart.prototype.update = function() {
    this.x += this.vel.x;
    this.y += this.vel.y;

    this.scale += (this.targetScale - this.scale) * 0.01;
    if (this.x - this.width > ww || this.x + this.width < 0) {
      this.scale = 0;
      this.x = Math.random() * ww;
    }
    if (this.y - this.height > wh || this.y + this.height < 0) {
      this.scale = 0;
      this.y = Math.random() * wh;
    }
    this.width = 473.8 * this.scale;
    this.height = 408.6 * this.scale;
  }
  Heart.prototype.draw = function() {
    ctx.globalAlpha = this.opacity;
    ctx.drawImage(heartImage, this.x - this.width * 0.5, this.y - this.height * 0.5, this.width, this.height);
  }

  function render() {
    ctx.clearRect(0, 0, ww, wh);
    for (var i = 0; i < num; i++) {
      hearts[i].update();
      hearts[i].draw();
    }
    requestAnimationFrame(render);
  }

  var heartImage = new Image();
  heartImage.onload = init();
  heartImage.src = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI0NzMuOHB4IiBoZWlnaHQ9IjQwOC42cHgiIHZpZXdCb3g9IjAgMCA0NzMuOCA0MDguNiI+PHBhdGggZmlsbD0iI2QzMjkzMiIgZD0iTTQwNC42LDE2LjZDMzg1LjQsNi4xLDM2My41LDAsMzQwLDBjLTQxLjUsMC03OC41LDE4LjktMTAzLDQ4LjVDMjEyLjMsMTguOSwxNzUuMywwLDEzMy44LDAgYy0yMy4zLDAtNDUuMyw2LjEtNjQuNSwxNi42QzI3LjksMzkuNSwwLDgzLjQsMCwxMzMuOWMwLDE0LjQsMi40LDI4LjMsNi42LDQxLjJDMjkuNiwyNzguNCwyMzcsNDA4LjYsMjM3LDQwOC42IHMyMDcuMi0xMzAuMiwyMzAuMi0yMzMuNWM0LjMtMTIuOSw2LjYtMjYuOCw2LjYtNDEuMkM0NzMuOCw4My40LDQ0NS45LDM5LjYsNDA0LjYsMTYuNnoiLz48L3N2Zz4=";

  window.addEventListener("resize", function() {
    ww = window.innerWidth;
    wh = window.innerHeight;
  })
</script>

</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值