js实现三角形粒子运动


Posted in Javascript onSeptember 22, 2020

本文实例为大家分享了js实现三角形粒子运动的具体代码,供大家参考,具体内容如下

效果(这里只是截了一个静态图,实际上里面的粒子是运动状态的):

js实现三角形粒子运动

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

<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>粒子</title>
 <style>
  * {
   margin: 0;
   padding: 0;
  }

  body {
   overflow: hidden;
  }
 </style>
 <script>
  //随机数获取 3 10 *7+3
  function random(min, max) {
   return Math.random() * (max - min) + min;

  }
  //亮色系
  // colors = ['#7cb5ec', '#434348', '#90ed7d', '#f7a35c', '#8085e9', '#f15c80', '#e4d354', '#8085e8', '#8d4653', '#91e8e1'];
  //暗色系
  colors = ['#c23531', '#2f4554', '#61a0a8', '#d48265', '#91c7ae', '#749f83', '#ca8622', '#bda29a', '#6e7074', '#546570', '#c4ccd3']
  //获取窗口宽高
  var width = window.innerWidth;
  var height = window.innerHeight;
  function Bubble() {
   this.r = random(5, 100);
   this.x1 = random(this.r, this.r * 2);
   this.y1 = random(this.r, this.r * 2);
   this.x2 = random(this.r, this.r * 2);
   this.y2 = random(this.r, this.r * 2);
   this.x3 = random(this.r, this.r * 2);
   this.y3 = random(this.r, this.r * 2);
   //随机获取colors数组里的颜色
   this.color = colors[Math.floor(random(0, colors.length))];
   //偏移步长
   this.xr = random(-5, 5);
   this.yr = random(-5, 5);
  }
  Bubble.prototype = {
   //绘制
   draw: function (context) {
    //开始路径
    context.beginPath();
    context.moveTo(this.x1, this.y1);
    context.lineTo(this.x2, this.y2);
    context.lineTo(this.x3, this.y3);
    context.lineTo(this.x1, this.y1);
    context.fillStyle = this.color;
    context.fill();
   },
   //移动
   move: function (context) {
    this.x1 += this.xr;
    this.y1 += this.yr;
    this.x2 += this.xr;
    this.y2 += this.yr;
    this.x3 += this.xr;
    this.y3 += this.yr;
    //边缘检测
    (this.x1 > width || this.x1 < 0) ? this.xr = -this.xr : null;
    (this.y1 > height || this.y1 < 0) ? this.yr = -this.yr : null;
    (this.x2 > width || this.x2 < 0) ? this.xr = -this.xr : null;
    (this.y2 > height || this.y2 < 0) ? this.yr = -this.yr : null;
    (this.x3 > width || this.x3 < 0) ? this.xr = -this.xr : null;
    (this.y3 > height || this.y3 < 0) ? this.yr = -this.yr : null;
    this.draw(context);
   }
  }

  window.onload = function () {
   //获取画布dom
   var canvas = document.querySelector('canvas');
   //设置canvas的宽高
   canvas.width = width;
   canvas.height = height;
   //获取画布上下文对象
   var context = canvas.getContext('2d');
   //数组存储bubble
   var arr = [];
   //生成粒子
   var total = 100;
   //生成例子
   for (var i = 0; i < total; i++) {
    var bubble = new Bubble();
    bubble.draw(context);
    arr.push(bubble);
   }
   var id = setInterval(function () {
    //清除
    context.clearRect(0, 0, width, height);
    //开始移动
    for (var i = 0; i < arr.length; i++) {
     arr[i].move(context);
    }
   }, 1000 / 60)
   //点击次数
   var count = 0;
   canvas.onclick = function () {
    if (count++ % 2 == 0) {
     //停止
     clearInterval(id);
    } else {
     //运行
     id = setInterval(function () {
      //清除
      context.clearRect(0, 0, width, height);
      //开始移动
      for (var i = 0; i < arr.length; i++) {
       arr[i].move(context);
      }
     }, 1000 / 60)
    }
   }
  }
 </script>
</head>

<body>
 <canvas title="点击停止,再次点击活动"></canvas>
</body>

</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
图片按比例缩放函数
Jun 26 Javascript
学习YUI.Ext 第二天
Mar 10 Javascript
jQuery源码分析-01总体架构分析
Nov 14 Javascript
基于jQuery实现收缩展开功能
Mar 18 Javascript
全面了解javascript中的错误处理机制
Jul 18 Javascript
Angular.js中用ng-repeat-start实现自定义显示
Oct 18 Javascript
解决IE7中使用jQuery动态操作name问题
Aug 28 jQuery
详解vscode中vue代码颜色插件
Oct 11 Javascript
vue 使用vue-i18n做全局中英文切换的方法
Oct 29 Javascript
Vue2.x Todo之自定义指令实现自动聚焦的方法
Jan 08 Javascript
如何自定义微信小程序tabbar上边框的颜色
Jul 09 Javascript
解决Vue router-link绑定事件不生效的问题
Jul 22 Javascript
js操作两个json数组合并、去重,以及删除某一项元素
Sep 22 #Javascript
js实现删除json中指定的元素
Sep 22 #Javascript
vue使用canvas实现移动端手写签名
Sep 22 #Javascript
JSON 入门教程基础篇 json入门学习笔记
Sep 22 #Javascript
Vue+Java 通过websocket实现服务器与客户端双向通信操作
Sep 22 #Javascript
Vue实现开关按钮拖拽效果
Sep 22 #Javascript
JS如何生成动态列表
Sep 22 #Javascript
You might like
DOTA2 探索永无止境 玩家自创强悍插眼攻略
2020/04/20 DOTA
PHP连接MSSQL2008/2005数据库(SQLSRV)配置实例
2014/10/22 PHP
Smarty模板简单配置与使用方法示例
2016/05/23 PHP
thinkPHP框架实现图像裁剪、缩放、加水印的方法
2017/03/14 PHP
jQuery中的常用事件总结
2009/12/27 Javascript
json格式化/压缩工具 Chrome插件扩展版
2010/05/25 Javascript
jquery.boxy插件的iframe扩展代码
2010/07/02 Javascript
鼠标移到导航当前位置的LI变色处于选中状态
2013/08/23 Javascript
jquery中$.post()方法的简单实例
2014/02/04 Javascript
jQuery 弹出层插件(推荐)
2016/05/24 Javascript
json数据处理及数据绑定
2017/01/25 Javascript
纯js模仿windows系统日历
2017/02/04 Javascript
vue插件vue-resource的使用笔记(小结)
2017/08/04 Javascript
JavaScript实现京东购物放大镜和选项卡效果的方法分析
2018/07/05 Javascript
vue中使用codemirror的实例详解
2018/11/01 Javascript
VUE组件中的 Drawer 抽屉实现代码
2019/08/06 Javascript
微信小程序webSocket的使用方法
2020/02/20 Javascript
Python语言生成水仙花数代码示例
2017/12/18 Python
浅析python中numpy包中的argsort函数的使用
2018/08/30 Python
python利用thrift服务读取hbase数据的方法
2018/12/27 Python
浅谈Python小波分析库Pywavelets的一点使用心得
2019/07/09 Python
python科学计算之numpy——ufunc函数用法
2019/11/25 Python
Python开发之pip安装及使用方法详解
2020/02/21 Python
css3的动画特效之动画序列(animation)
2017/12/22 HTML / CSS
家庭睡衣和家庭用品:Little Blue House
2018/03/18 全球购物
英国地毯卖家:The Rug Seller
2019/07/18 全球购物
Jacadi Paris英国官网:法国童装品牌
2019/08/09 全球购物
.net C#面试题
2012/08/28 面试题
项目委托协议书(最新)
2014/09/13 职场文书
信用卡工资证明范本
2014/10/17 职场文书
房地产销售经理岗位职责
2015/02/02 职场文书
论文评审意见
2015/06/05 职场文书
MySQL数据库事务的四大特性
2022/04/20 MySQL
Redis实现主从复制方式(Master&Slave)
2022/06/21 Redis
element tree树形组件回显数据问题解决
2022/08/14 Javascript
CSS 实现磨砂玻璃(毛玻璃)效果样式
2023/05/21 HTML / CSS