当前位置: 首页 > news >正文

前端爱心代码跟个风

光棍节new一个对象发给Ta

<!DOCTYPE html>
<html>

<head>
  <title></title>
</head>
<style>
  * {
    padding: 0;
    margin: 0;
  }

  html,
  body {
    height: 100%;
    padding: 0;
    margin: 0;
    background: rgb(2, 2, 2);
  }

  canvas {
    position: absolute;
    width: 100%;

    height: 100%;
  }

  .aa {
    position: fixed;
    left: 50%;
    bottom: 10px;
    color: rgb(9, 194, 113)
  }
</style>

<body>
  <canvas id="pinkboard"></canvas>

  <script>

    /*
     * Settings
     */
    var settings = {
      particles: {
        length: 1000, // 最大颗粒量
        duration: 3, // 粒子持续时间(秒)
        velocity: 100, // 粒子速度(像素/秒)
        effect: -0.75, // 玩这个效果很好
        size: 20, // 颗粒大小(像素)
      },
    };

    (function () { var b = 0; var c = ["ms", "moz", "webkit", "o"]; for (var a = 0; a < c.length && !window.requestAnimationFrame; ++a) { window.requestAnimationFrame = window[c[a] + "RequestAnimationFrame"]; window.cancelAnimationFrame = window[c[a] + "CancelAnimationFrame"] || window[c[a] + "CancelRequestAnimationFrame"] } if (!window.requestAnimationFrame) { window.requestAnimationFrame = function (h, e) { var d = new Date().getTime(); var f = Math.max(0, 16 - (d - b)); var g = window.setTimeout(function () { h(d + f) }, f); b = d + f; return g } } if (!window.cancelAnimationFrame) { window.cancelAnimationFrame = function (d) { clearTimeout(d) } } }());

    /*
     * Point class
     */
    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;
    })();

    /*
     * Particle class
     */
    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 * settings.particles.effect;
        this.acceleration.y = dy * settings.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 / settings.particles.duration);
        context.globalAlpha = 1 - this.age / settings.particles.duration;
        context.drawImage(image, this.position.x - size / 2, this.position.y - size / 2, size, size);
      };
      return Particle;
    })();

    /*
     * ParticlePool class
     */
    var ParticlePool = (function () {
      var particles,
        firstActive = 0,
        firstFree = 0,
        duration = settings.particles.duration;

      function ParticlePool(length) {
        // 创建和填充粒子池
        particles = new Array(length);
        for (var i = 0; i < particles.length; i++)
          particles[i] = new Particle();
      }
      ParticlePool.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;
      };
      ParticlePool.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;
        }


      };
      ParticlePool.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 ParticlePool;
    })();

    /*
     * 把所有这些放在一起
     */
    (function (canvas) {
      var context = canvas.getContext('2d'),
        particles = new ParticlePool(settings.particles.length),
        particleRate = settings.particles.length / settings.particles.duration, //粒子/秒
        time;

      // get point on heart with -PI <= t <= PI
      function pointOnHeart(t) {
        return new Point(
          160 * Math.pow(Math.sin(t), 3),
          130 * Math.cos(t) - 50 * Math.cos(2 * t) - 20 * Math.cos(3 * t) - 10 * Math.cos(4 * t) + 25
        );
      }

      //使用虚拟画布创建粒子图像
      var image = (function () {
        var canvas = document.createElement('canvas'),
          context = canvas.getContext('2d');
        canvas.width = settings.particles.size;
        canvas.height = settings.particles.size;
        //helper函数创建路径
        function to(t) {
          var point = pointOnHeart(t);
          point.x = settings.particles.size / 2 + point.x * settings.particles.size / 350;
          point.y = settings.particles.size / 2 - point.y * settings.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; // baby steps!
          point = to(t);
          context.lineTo(point.x, point.y);
        }
        context.closePath();
        //创建填充
        context.fillStyle = '#ff9999';
        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(settings.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('pinkboard'));
  </script>
</body>

</html>

在这里插入图片描述

相关文章:

  • 【数据结构】C语言实现顺序栈 OJ题 —— 有效的括号
  • Hive笔记
  • 趣味益智小游戏 三子棋+五子棋 优化版(可任意选择棋盘大小)
  • MySQL : 彻底搞懂一条SQL的执行过程
  • 【成为红帽工程师】第三天 web服务器
  • 【Node.js实战】一文带你开发博客项目(API 对接 MySQL)
  • 鸿蒙开发套件全面升级,助力鸿蒙生态蓬勃发展
  • HTML期末大作业——游戏介绍(HTML+CSS+JavaScript) web前端开发技术 web课程设计网页规划与设计 Web大学生网页成品
  • 读书笔记:《高频交易员》
  • HTML小游戏6 —— 《高达战争》横版射击游戏(附完整源码)
  • 【深度学习】第三章:卷积神经网络
  • 几款很好看的爱心表白代码(动态)
  • C语言百日刷题第六天
  • 表白爱心代码
  • linux无界面手敲命令笔记
  • 9月CHINA-PUB-OPENDAY技术沙龙——IPHONE
  • 【从零开始安装kubernetes-1.7.3】2.flannel、docker以及Harbor的配置以及作用
  • 【腾讯Bugly干货分享】从0到1打造直播 App
  • 2017年终总结、随想
  • E-HPC支持多队列管理和自动伸缩
  • If…else
  • iOS高仿微信项目、阴影圆角渐变色效果、卡片动画、波浪动画、路由框架等源码...
  • Javascripit类型转换比较那点事儿,双等号(==)
  • JavaScript/HTML5图表开发工具JavaScript Charts v3.19.6发布【附下载】
  • JavaSE小实践1:Java爬取斗图网站的所有表情包
  • JSDuck 与 AngularJS 融合技巧
  • JS学习笔记——闭包
  • mysql 数据库四种事务隔离级别
  • PAT A1050
  • SpringBoot 实战 (三) | 配置文件详解
  • 阿里中间件开源组件:Sentinel 0.2.0正式发布
  • 回顾 Swift 多平台移植进度 #2
  • 数据库写操作弃用“SELECT ... FOR UPDATE”解决方案
  • #每天一道面试题# 什么是MySQL的回表查询
  • #我与Java虚拟机的故事#连载10: 如何在阿里、腾讯、百度、及字节跳动等公司面试中脱颖而出...
  • (libusb) usb口自动刷新
  • (Ruby)Ubuntu12.04安装Rails环境
  • (差分)胡桃爱原石
  • (区间dp) (经典例题) 石子合并
  • (深度全面解析)ChatGPT的重大更新给创业者带来了哪些红利机会
  • (五)Python 垃圾回收机制
  • (已解决)报错:Could not load the Qt platform plugin “xcb“
  • ... fatal error LINK1120:1个无法解析的外部命令 的解决办法
  • .360、.halo勒索病毒的最新威胁:如何恢复您的数据?
  • .java 指数平滑_转载:二次指数平滑法求预测值的Java代码
  • .net core 3.0 linux,.NET Core 3.0 的新增功能
  • .Net Core 中间件验签
  • .NET 将混合了多个不同平台(Windows Mac Linux)的文件 目录的路径格式化成同一个平台下的路径
  • .net 托管代码与非托管代码
  • .NET/C# 使用 SpanT 为字符串处理提升性能
  • .net反编译工具
  • .net生成的类,跨工程调用显示注释
  • .pings勒索病毒的威胁:如何应对.pings勒索病毒的突袭?
  • @ComponentScan比较
  • [20171113]修改表结构删除列相关问题4.txt