js canvas實(shí)現(xiàn)隨機(jī)粒子特效
本文實(shí)例為大家分享了js canvas隨機(jī)粒子特效的具體代碼,供大家參考,具體內(nèi)容如下
前言
canvas實(shí)現(xiàn)前端的特效美術(shù)
結(jié)果展示
代碼
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script src="./main.js"></script> </body> </html>
main.js
/* *粒子化類構(gòu)造 *類功能: *1.初始化。創(chuàng)建畫(huà)布,規(guī)定粒子屬性等; *2.創(chuàng)建圖像并且進(jìn)行繪制 *3.區(qū)域顏色定義 *4.粒子移動(dòng)和偏射角度 */ // 生成粒子 let Particle = function(context, options){ let random = Math.random(); this.context = context; // 在畫(huà)布里的x坐標(biāo) this.x = options.x; // 在畫(huà)布里的y坐標(biāo) this.y = options.y; // 取隨機(jī)數(shù)的1/2,對(duì)角度進(jìn)行隨機(jī)放大 this.s = 0.5 + Math.random(); // this.s = 1 + Math.random(); // 粒子運(yùn)動(dòng)的變化角度 this.a = 0; // 寬度 this.w = window.innerWidth; // 高度 this.h = window.innerHeight; // 半徑 this.radius = options.radius || 0.5 + Math.random() * 10; // 顏色 this.color = options.color || "#E5483F"; // console.log(this.color); // 指定3秒后調(diào)用; // 如果粒子的半徑大于0.5,加入新的粒子。 setTimeout(function(){ if(this.radius > 0.5){ particles.push( new Particle(context, { x: this.x, y: this.y, color: this.radius / 2 > 1 ? "#d6433b" : "#FFFFFF", radius: this.radius / 2.2 }) ); } }.call(Particle), 3000); }; // 渲染圖像 Particle.prototype.render = function() { //從(0,0)開(kāi)始新的路徑; this.context.beginPath(); // 創(chuàng)建曲線弧 this.context.arc(this.x, this.y, this.radius, 0, 2 * Math.PI); // 繪圖的線條寬度 this.context.lineWidth = 2; //顏色填充 this.context.fillStyle = this.color; // 填充當(dāng)前圖像的路徑 this.context.fill(); // 返回初始點(diǎn),并且繪制線條到初始位置 this.context.closePath(); }; Particle.prototype.swapColor = function() { // 排除白色 if (this.color != "#FFFFFF") { // 判斷左右界面,并且賦顏色的值 if (this.x < this.w / 2) { // 左半邊 this.color = "#36fcfa"; } else { // 右半邊 this.color = "#E5483F"; } } }; Particle.prototype.move = function() { // 顏色定義 this.swapColor(); // 橫坐標(biāo)按照cos角度進(jìn)行變換,并且對(duì)其進(jìn)行隨機(jī)數(shù)放大; // 偏射角度以便兩個(gè)半界分開(kāi) this.x += Math.cos(this.a) * this.s; this.y += Math.sin(this.a) * this.s; // this.y += Math.cos(this.a) * this.s; // this.x += Math.sin(this.a) * this.s; // 在變化后,對(duì)隨機(jī)角度進(jìn)行再重?。? this.a += Math.random() * 0.8 - 0.4; // 判斷全為0,粒子橫坐標(biāo)無(wú)移動(dòng); if (this.x < 0 || this.x > this.w - this.radius) { return false; } // 粒子縱坐標(biāo)無(wú)移動(dòng); if (this.y < 0 || this.y > this.h - this.radius) { return false; } // 重新繪制圖像 this.render(); return true; }; let canvas = document.createElement('canvas'); canvas.width = window.innerWidth - 20; canvas.height = window.innerHeight - 30; document.body.insertBefore(canvas, null); let context = canvas.getContext('2d'); const conf = { frequency: 50, x: canvas.width, y: canvas.height }; let particles = [], frequency = conf.frequency; setInterval(function(){ popolate(); }.bind(null), frequency); function popolate(){ particles.push( new Particle(context, { x: conf.x / 2, y: conf.y / 2 }) ); return particles.length; } function clear() { context.globalAlpha = 0.04; context.fillStyle = '#000042'; context.fillRect(0,0,canvas.width, canvas.height); context.globalAlpha = 1; } function update(){ particles = particles.filter(p => p.move()); clear(); requestAnimationFrame(arguments.callee); } update();
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 純JavaScript實(shí)現(xiàn)HTML5 Canvas六種特效濾鏡示例
- JavaScript+html5 canvas實(shí)現(xiàn)圖片破碎重組動(dòng)畫(huà)特效
- 教你用幾十行js實(shí)現(xiàn)很炫的canvas交互特效
- js canvas實(shí)現(xiàn)星空連線背景特效
- javascript結(jié)合html5 canvas實(shí)現(xiàn)(可調(diào)畫(huà)筆顏色/粗細(xì)/橡皮)的涂鴉板
- Javascript保存網(wǎng)頁(yè)為圖片借助于html2canvas庫(kù)實(shí)現(xiàn)
- js+canvas實(shí)現(xiàn)滑動(dòng)拼圖驗(yàn)證碼功能
- JavaScript+html5 canvas實(shí)現(xiàn)本地截圖教程
- JS+Canvas 實(shí)現(xiàn)下雨下雪效果
- html5 canvas js(數(shù)字時(shí)鐘)實(shí)例代碼
- js和canvas繪制圓形金屬質(zhì)感特效
相關(guān)文章
JavaScript高級(jí)程序設(shè)計(jì) 閱讀筆記(二十) js錯(cuò)誤處理
語(yǔ)法錯(cuò)誤,也稱解析錯(cuò)誤,發(fā)生在傳統(tǒng)語(yǔ)言的編譯時(shí),在JavaScript中發(fā)生在解釋時(shí),運(yùn)行時(shí)錯(cuò)誤也稱為異常(exception,在編譯期/解釋器后)2012-08-08JavaScript實(shí)現(xiàn)省市聯(lián)動(dòng)過(guò)程中bug的解決方法
這篇文章主要為大家詳細(xì)介紹了解決JavaScript實(shí)現(xiàn)省市聯(lián)動(dòng)過(guò)程中的bug,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12javascript 表單驗(yàn)證常見(jiàn)正則
表單驗(yàn)證正則的代碼,大家可以參考下,可以直接拿來(lái)使用,也可以用來(lái)學(xué)習(xí)正則。2009-09-09前端實(shí)現(xiàn)下載文件(包含壓縮包下載)方式詳細(xì)總結(jié)
這篇文章主要給大家介紹了關(guān)于前端實(shí)現(xiàn)下載文件(包含壓縮包下載)方式的相關(guān)資料,這段時(shí)間項(xiàng)目需要下載文件,所以這里給大家總結(jié)下,需要的朋友可以參考下2023-09-09基于JavaScript打造一款桌面級(jí)便簽系統(tǒng)
本文將用html,css和JavaScript實(shí)現(xiàn)一個(gè)簡(jiǎn)單的便簽系統(tǒng)。除非手動(dòng)清空便簽,否則便簽會(huì)一直保留,非常方便。感興趣的小伙伴可以跟隨小編一起動(dòng)手試一試2022-02-02JavaScript+CSS實(shí)現(xiàn)的可折疊二級(jí)菜單實(shí)例
這篇文章主要介紹了JavaScript+CSS實(shí)現(xiàn)的可折疊二級(jí)菜單,以完整實(shí)例形式分析了JavaScript基于頁(yè)面元素節(jié)點(diǎn)及樣式的動(dòng)態(tài)操作實(shí)現(xiàn)折疊菜單的相關(guān)技巧,需要的朋友可以參考下2016-02-02