js實(shí)現(xiàn)三角形粒子運(yùn)動(dòng)
本文實(shí)例為大家分享了js實(shí)現(xiàn)三角形粒子運(yùn)動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下
效果(這里只是截了一個(gè)靜態(tài)圖,實(shí)際上里面的粒子是運(yùn)動(dòng)狀態(tài)的):
<!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> //隨機(jī)數(shù)獲取 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); //隨機(jī)獲取colors數(shù)組里的顏色 this.color = colors[Math.floor(random(0, colors.length))]; //偏移步長(zhǎng) this.xr = random(-5, 5); this.yr = random(-5, 5); } Bubble.prototype = { //繪制 draw: function (context) { //開(kāi)始路徑 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(); }, //移動(dòng) 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; //邊緣檢測(cè) (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 () { //獲取畫(huà)布dom var canvas = document.querySelector('canvas'); //設(shè)置canvas的寬高 canvas.width = width; canvas.height = height; //獲取畫(huà)布上下文對(duì)象 var context = canvas.getContext('2d'); //數(shù)組存儲(chǔ)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); //開(kāi)始移動(dòng) for (var i = 0; i < arr.length; i++) { arr[i].move(context); } }, 1000 / 60) //點(diǎn)擊次數(shù) var count = 0; canvas.onclick = function () { if (count++ % 2 == 0) { //停止 clearInterval(id); } else { //運(yùn)行 id = setInterval(function () { //清除 context.clearRect(0, 0, width, height); //開(kāi)始移動(dòng) for (var i = 0; i < arr.length; i++) { arr[i].move(context); } }, 1000 / 60) } } } </script> </head> <body> <canvas title="點(diǎn)擊停止,再次點(diǎn)擊活動(dòng)"></canvas> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- js canvas實(shí)現(xiàn)隨機(jī)粒子特效
- js實(shí)現(xiàn)3D粒子酷炫動(dòng)態(tài)旋轉(zhuǎn)特效
- JavaScript動(dòng)畫(huà)實(shí)例之粒子文本的實(shí)現(xiàn)方法詳解
- JS實(shí)現(xiàn)躲避粒子小游戲
- javascript Canvas動(dòng)態(tài)粒子連線
- JavaScript實(shí)現(xiàn)鼠標(biāo)移動(dòng)粒子跟隨效果
- 基于three.js實(shí)現(xiàn)的3D粒子動(dòng)效實(shí)例代碼
- 原生JS+HTML5實(shí)現(xiàn)跟隨鼠標(biāo)一起流動(dòng)的粒子動(dòng)畫(huà)效果
- 使用3D引擎threeJS實(shí)現(xiàn)星空粒子移動(dòng)效果
- ThingJS粒子特效一鍵實(shí)現(xiàn)雨雪效果
相關(guān)文章
淺談關(guān)于JS下大批量異步任務(wù)按順序執(zhí)行解決方案一點(diǎn)思考
這篇文章主要介紹了淺談關(guān)于JS下大批量異步任務(wù)按順序執(zhí)行解決方案一點(diǎn)思考,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01JS獲取html元素的標(biāo)記名實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇JS獲取html元素的標(biāo)記名實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10[Web]防止用戶復(fù)制頁(yè)面內(nèi)容和另存頁(yè)面的方法
原理就是利用js控制一些復(fù)制等事件,但破解也簡(jiǎn)單,這里就不說(shuō)了。2009-02-02Javascript中的var_dump函數(shù)實(shí)現(xiàn)代碼
發(fā)現(xiàn)了一個(gè)非常好的JavaScript調(diào)試方法,目前看到的是可以打印Object/Array/Function/String四種類型,使用方法和PHP中的var_dump()一樣,只要直接dump(變量名)即可。2009-09-09JS實(shí)現(xiàn)課堂隨機(jī)點(diǎn)名和順序點(diǎn)名
這篇文章主要介紹了基于JS實(shí)現(xiàn)課堂隨機(jī)點(diǎn)名和順序點(diǎn)名的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-03-03JavaScript中發(fā)出HTTP請(qǐng)求最常用的方法
JavaScript具有很好的模塊和方法來(lái)發(fā)送可用于從服務(wù)器端資源發(fā)送或接收數(shù)據(jù)的HTTP請(qǐng)求。這篇文章主要介紹了JavaScript中發(fā)出HTTP請(qǐng)求最常用的方法,需要的朋友可以參考下2018-07-07