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 相关文章推荐
解决 firefox 不支持 document.all的方法
Mar 12 Javascript
ExtJs事件机制基本代码模型和流程解析
Oct 24 Javascript
给artDialog 5.02 增加ajax get功能详细介绍
Nov 13 Javascript
IE中JS跳转丢失referrer问题的2个解决方法
Jul 18 Javascript
jQuery实现的图文高亮滚动切换特效实例
Aug 10 Javascript
JavaScript的==运算详解
Jul 20 Javascript
vue.js 左侧二级菜单显示与隐藏切换的实例代码
May 23 Javascript
three.js实现3D模型展示的示例代码
Dec 31 Javascript
vue微信分享出来的链接点开是首页问题的解决方法
Nov 28 Javascript
Vue动态组件与异步组件实例详解
Feb 23 Javascript
小程序分享模块超级详解(推荐)
Apr 10 Javascript
vue3中provide && inject的使用
Jul 01 Vue.js
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
全国FM电台频率大全 - 30 宁夏回族自治区
2020/03/11 无线电
一个分页的论坛
2006/10/09 PHP
求PHP数组最大值,最小值的代码
2011/10/31 PHP
ThinkPHP自动转义存储富文本编辑器内容导致读取出错的解决方法
2014/08/08 PHP
php实现背景图上添加圆形logo图标的方法
2016/11/17 PHP
jQuery html()等方法介绍
2009/11/18 Javascript
重写javascript中window.confirm的行为
2012/10/21 Javascript
了解了这些才能开始发挥jQuery的威力
2013/10/10 Javascript
JS实现局部选择打印和局部不选择打印
2014/04/03 Javascript
jQuery实现折叠、展开的菜单组效果代码
2015/09/16 Javascript
JavaScript小技巧整理篇(非常全)
2016/01/26 Javascript
js弹出窗口简单实现代码
2017/03/22 Javascript
深入理解angular2启动项目步骤
2017/07/15 Javascript
vscode下vue项目中eslint的使用方法
2019/01/13 Javascript
深入探讨JavaScript的最基本部分之执行上下文
2019/02/12 Javascript
使vue实现jQuery调用的两种方法
2019/05/12 jQuery
uni-app微信小程序登录并使用vuex存储登录状态的思路详解
2019/11/04 Javascript
python elasticsearch从创建索引到写入数据的全过程
2019/08/04 Python
Python3 文章标题关键字提取的例子
2019/08/26 Python
关于sys.stdout和print的区别详解
2019/12/05 Python
django API 中接口的互相调用实例
2020/04/01 Python
如何在windows下安装配置python工具Ulipad
2020/10/27 Python
Python更改pip镜像源的方法示例
2020/12/01 Python
python文件路径操作方法总结
2020/12/21 Python
英国著名的茶叶品牌:Whittard of Chelsea
2016/09/22 全球购物
美国卡车、吉普车和SUV零件网站:4 Wheel Parts
2016/11/24 全球购物
捷克玩具商店:Bambule
2019/02/23 全球购物
在校硕士自我鉴定
2014/01/23 职场文书
投标邀请书范文
2014/01/31 职场文书
团日活动总结书格式
2014/05/08 职场文书
2014年国庆节广播稿
2014/09/19 职场文书
走群众路线学习心得体会
2014/10/31 职场文书
2014年煤矿工作总结
2014/11/24 职场文书
2015年电厂工作总结范文
2015/05/13 职场文书
Python基础数据类型tuple元组的概念与用法
2021/08/02 Python
vmware虚拟机打不开vmx文件怎么办 ?vmware虚拟机vmx文件打开方法
2022/04/08 数码科技