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 相关文章推荐
IE中jscript/javascript的条件编译
Sep 07 Javascript
Ext面向对象开发实践(续)
Nov 18 Javascript
JavaScript 异步调用框架 (Part 1 - 问题 &amp; 场景)
Aug 03 Javascript
extjs DataReader、JsonReader、XmlReader的构造方法
Nov 07 Javascript
Javascript中的Array数组对象详谈
Mar 03 Javascript
jQuery提示效果代码分享
Nov 20 Javascript
jquery hover 不停闪动问题的解决方法(亦为stop()的使用)
Feb 10 Javascript
浅谈React 服务器端渲染的使用
May 08 Javascript
浅谈node.js 命令行工具(cli)
May 10 Javascript
vue移动端路由切换实例分析
May 14 Javascript
JavaScript的变量声明与声明提前用法实例分析
Nov 26 Javascript
微信小程序地图绘制线段并且测量(实例代码)
Jan 02 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
PHP URL地址获取函数代码(端口等) 推荐
2010/05/15 PHP
PHP 获取MySQL数据库里所有表的实现代码
2011/07/13 PHP
PHP服务器页面间跳转实现方法
2012/08/02 PHP
浅析PHP页面局部刷新功能的实现小结
2013/06/21 PHP
thinkPHP框架实现的无限回复评论功能示例
2018/06/09 PHP
基于php+MySql实现学生信息管理系统实例
2020/08/04 PHP
从sohu弄下来的flash中展示图片的代码
2007/04/27 Javascript
JavaScript 克隆数组最简单的方法
2009/02/12 Javascript
extjs两个tbar问题探讨
2013/08/08 Javascript
jquery事件与函数的使用介绍
2013/09/29 Javascript
jquery清空表单数据示例分享
2014/02/13 Javascript
IE下使用jQuery重置iframe地址时内存泄露问题解决办法
2015/02/05 Javascript
JavaScript中的事件委托及好处
2016/07/12 Javascript
jQuery弹出下拉列表插件(实现kindeditor的@功能)
2016/08/16 Javascript
详解为Angular.js内置$http服务添加拦截器的方法
2016/12/20 Javascript
vue component组件使用方法详解
2017/07/14 Javascript
JavaScript正则表达式和级联效果
2017/09/14 Javascript
Vue工程模板文件 webpack打包配置方法
2017/12/26 Javascript
security.js实现的RSA加密功能示例
2018/06/06 Javascript
angular6的table组件开发的实现示例
2018/12/26 Javascript
ES6 Generator函数的应用实例分析
2019/06/26 Javascript
vue插件--仿微信小程序showModel实现模态提示窗功能
2020/08/19 Javascript
Mac OS X10.9安装的Python2.7升级Python3.3步骤详解
2013/12/04 Python
浅谈终端直接执行py文件,不需要python命令
2017/01/23 Python
详解pyppeteer(python版puppeteer)基本使用
2019/06/12 Python
Python3内置模块之base64编解码方法详解
2019/07/13 Python
html5触摸事件判断滑动方向的实现
2018/06/05 HTML / CSS
H5 meta小结(前端必看篇)
2016/08/24 HTML / CSS
three.js模拟实现太阳系行星体系功能
2019/09/03 HTML / CSS
澳大利亚电商Catch新西兰站:Catch.co.nz
2020/05/30 全球购物
优秀中职教师事迹材料
2014/08/26 职场文书
文明单位创建材料
2014/12/24 职场文书
婚礼答谢词范文
2015/09/29 职场文书
详解Html5项目适配系统深色模式方案总结
2021/04/14 HTML / CSS
Python Parser的用法
2021/05/12 Python
如何通过cmd 连接阿里云服务器
2022/04/18 Servers