js粒子效果(一)

?效果:

代碼:

<!doctype html>
<html>
<head><meta charset="utf-8"><title>HTML5鼠標經過粒子散開動畫特效</title><style>html, body {position: absolute;overflow: hidden;margin: 0;padding: 0;width: 100%;height: 100%;background: #fff;touch-action: none;content-zooming: none;}canvas {position: absolute;width: 100%;height: 100%;background: #fff;}</style>
</head>
<body>
<canvas></canvas><script>"use strict";{// particles classclass Particle {constructor(k, i, j) {this.i = i;this.j = j;this.init();this.x = this.x0;this.y = this.y0;this.pos = posArray.subarray(k * 3, k * 3 + 3);this.pointer = pointer;}init() {this.x0 = canvas.width * 0.5 + this.i * canvas.width / 240;this.y0 = canvas.height * 0.5 + this.j * canvas.height / 160;}move() {const dx = this.pointer.x - this.x;const dy = this.pointer.y - this.y;const d = Math.sqrt(dx * dx + dy * dy);const s = 1000 / d;this.x += -s * (dx / d) + (this.x0 - this.x) * 0.02;this.y += -s * (dy / d) + (this.y0 - this.y) * 0.02;// update buffer positionthis.pos[0] = this.x;this.pos[1] = this.y;this.pos[2] = 0.15 * s * s;}}// webGL canvasconst canvas = {init(options) {// set webGL contextthis.elem = document.querySelector("canvas");const gl = (this.gl =this.elem.getContext("webgl", options) ||this.elem.getContext("experimental-webgl", options));if (!gl) return false;// compile shadersconst vertexShader = gl.createShader(gl.VERTEX_SHADER);gl.shaderSource(vertexShader,`precision highp float;attribute vec3 aPosition;uniform vec2 uResolution;void main() {gl_PointSize = max(2.0, min(30.0, aPosition.z));gl_Position = vec4(( aPosition.x / uResolution.x * 2.0) - 1.0, (-aPosition.y / uResolution.y * 2.0) + 1.0, 0.0,1.0);}`);gl.compileShader(vertexShader);const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);gl.shaderSource(fragmentShader,`precision highp float;void main() {vec2 pc = 2.0 * gl_PointCoord - 1.0;gl_FragColor = vec4(0.2, 0.2, 0.8, 1.0 - dot(pc, pc));}`);gl.compileShader(fragmentShader);const program = (this.program = gl.createProgram());gl.attachShader(this.program, vertexShader);gl.attachShader(this.program, fragmentShader);gl.linkProgram(this.program);gl.useProgram(this.program);// resolutionthis.uResolution = gl.getUniformLocation(this.program, "uResolution");gl.enableVertexAttribArray(this.uResolution);// canvas resizethis.resize();window.addEventListener("resize", () => this.resize(), false);return gl;},resize() {this.width = this.elem.width = this.elem.offsetWidth;this.height = this.elem.height = this.elem.offsetHeight;for (const p of particles) p.init();this.gl.uniform2f(this.uResolution, this.width, this.height);this.gl.viewport(0,0,this.gl.drawingBufferWidth,this.gl.drawingBufferHeight);}};const pointer = {init(canvas) {this.x = 0.1 + canvas.width * 0.5;this.y = canvas.height * 0.5;this.s = 0;["mousemove", "touchstart", "touchmove"].forEach((event, touch) => {document.addEventListener(event,e => {if (touch) {e.preventDefault();this.x = e.targetTouches[0].clientX;this.y = e.targetTouches[0].clientY;} else {this.x = e.clientX;this.y = e.clientY;}},false);});}};// init webGL canvasconst particles = [];const gl = canvas.init({alpha: true,stencil: true,antialias: true,depth: false});// additive blending "lighter"gl.blendFunc(gl.SRC_ALPHA, gl.ONE);gl.enable(gl.BLEND);// init pointerpointer.init(canvas);// init particlesconst nParticles = 240 * 80;const posArray = new Float32Array(nParticles * 3);let k = 0;for (let i = -120; i < 120; i++) {for (let j = -40; j < 40; j++) {particles.push(new Particle(k++, i, j));}}// create position bufferconst aPosition = gl.getAttribLocation(canvas.program, "aPosition");gl.enableVertexAttribArray(aPosition);const positionBuffer = gl.createBuffer();// draw all particlesconst draw = () => {gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);gl.vertexAttribPointer(aPosition, 3, gl.FLOAT, false, 0, 0);gl.bufferData(gl.ARRAY_BUFFER,posArray,gl.DYNAMIC_DRAW);gl.drawArrays(gl.GL_POINTS, 0, nParticles);}// main animation loopconst run = () => {requestAnimationFrame(run);for (const p of particles) p.move();draw();};requestAnimationFrame(run);}
</script>
<div style="text-align:center;margin:50px 0; font:normal 14px/24px 'MicroSoft YaHei';"></div>
</body>
</html>

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/167875.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/167875.shtml
英文地址,請注明出處:http://en.pswp.cn/news/167875.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

DELL MD3600F存儲重置管理軟件密碼

注意&#xff1a;密碼清除可能會導致業務秒斷&#xff0c;建議非業務時間操作 針對一臺控制器操作即可&#xff0c;另一控制器會同步操作 重置后密碼為空&#xff01; 需求&#xff1a;重置存儲管理軟件密碼 管理軟件中分配物理磁盤時提示輸入密碼(類似是否了解風險確認操作的提…

華為OD機試 - 二叉樹計算(Java JS Python C)

目錄 題目描述 輸入描述 輸出描述 用例 題目解析 JS算法源碼 Java算法源碼

io.lettuce.core.RedisCommandExecutionException

io.lettuce.core.RedisCommandExecutionException: ERR invalid password ERR invalid password-CSDN博客 io.lettuce.core.RedisCommandExecutionException /** Copyright 2011-2022 the original author or authors.** Licensed under the Apache License, Version 2.0 (the…

Rust UI開發(一):使用iced構建UI時,如何在界面顯示中文字符

注&#xff1a;此文適合于對rust有一些了解的朋友 iced是一個跨平臺的GUI庫&#xff0c;用于為rust語言程序構建UI界面。 iced的基本邏輯是&#xff1a; UI交互產生消息message&#xff0c;message傳遞給后臺的update&#xff0c;在這個函數中編寫邏輯&#xff0c;然后通過…

2023-11-24--oracle--實驗--[Merge 語句]

oracle--實驗---Merge語句 1.認知Merge 語句 ? merge 語句是 sql 語句的一種。在 SQL server 、 Oracle 數據庫中可用&#xff0c; MySQL 中不可用。 ? merge 用來合并 update 和 insert 語句。目的&#xff1a;通過 merge 語句&#xff0c;根據一張表&#xff08; 原數據表…

IOS免簽封裝打包蘋果APP的方法

IOS免簽app封裝打包蘋果APP的方法如下&#xff1a; 準備一個未簽名的IPA文件。獲取一個企業證書或個人證書&#xff0c;用于簽名IPA文件。將證書添加到Keychain Access中。安裝iOS App Signer&#xff08;可以在網上找到相關下載鏈接&#xff09;。打開iOS App Signer&#xf…

AT360-6T GNSS 單頻高精度授時模塊特性參數

AT360-6T 模塊具有高靈敏度、低功耗、低cost等優勢&#xff0c;可以滿足電力授時&#xff0c;通信授時等領域的應用。AT360-6T特點&#xff1a; 1.支持北斗二代/北斗三代信號 2.高精度授時 3.可靠性授時 實時高精度授時 AT360-6T 系列模塊的授時秒脈沖抖動可以達到 10ns&am…

Vue學習筆記-搭建Vuex

1.概念 在Vue實現集中式狀態&#xff08;數據&#xff09;管理的一個插件&#xff0c;對Vue中多個組件的共享狀態進行集中式的管理&#xff08;讀/寫&#xff09;&#xff0c;也是一種組件間的通信方式&#xff0c;適用于任意組件間的通信 2.使用場景 多個組件需要共享數據時…

Mysql存儲引擎分類

Mysql存儲引擎分類&#xff1a; 在選擇存儲引擎時&#xff0c;應該根據應用系統的特點選擇合適的存儲引擎。對于復雜的應用系統&#xff0c;還可以根據實際情況選擇多種存儲引擎進行組合。 InnoDB: 是Mysql的默認存儲引擎&#xff0c;支持事務、外鍵。如果應用對事務的完整性有…

杰發科技AC7801——ADC軟件觸發的簡單使用

前言 7801資料讀起來不是很好理解&#xff0c;大概率是之前MTK的大佬寫的。在此以簡單的方式進行描述。我們做一個簡單的規則組軟件觸發Demo。因為規則組通道只有一個數據寄存器&#xff0c;因此還需要用上DMA方式搬運數據到內存。 AC7801的ADC簡介 7801的ADC是一種 12 位 逐…

一文學會qml自定義組件

文章目錄 最簡單的自定義控件:自定義按鈕組件添加自定義信號在QML中,自定義組件通常是通過創建一個新的QML文件來實現的,這個文件定義了組件的屬性、信號、槽以及界面。你可以將這個組件看作是一個可重用的模塊,它可以在不同的QML場景中使用,而不需要重復編寫代碼。 以下…

洛谷P1157組合的輸出 遞歸:我他又來辣

沒沒沒沒沒沒沒錯&#xff0c;這是一道簡單的遞歸&#xff08;其實是深搜加回溯) 我不管&#xff0c;我說是遞歸就是遞歸。 上題干&#xff1a; 題目描述 排列與組合是常用的數學方法&#xff0c;其中組合就是從 n 個元素中抽出 r個元素&#xff08;不分順序且 r≤n&#x…

查swap內存使用

查詢linux的swap被什么使用了 查詢centos的swap被什么進程使用了 swap內存被什么程序占用&#xff0c;什么程序使用了swap分區&#xff0c;占用swap內存的進程 查系統使用swap內存前10個進程&#xff1a; for i in $( cd /proc;ls |grep "^[0-9]"|awk $0 >10…

大數據技術之數據安全與網絡安全——CMS靶場實訓

大數據技術之數據安全與網絡安全——CMS靶場實訓 在當今數字化時代&#xff0c;大數據技術的迅猛發展帶來了前所未有的數據增長&#xff0c;同時也催生了對數據安全和網絡安全的更為迫切的需求。本篇博客將聚焦于大數據技術背景下的數據安全與網絡安全&#xff0c;并通過CMS&a…

C語言-指針講解(3)

文章目錄 1.字符指針變量1.1 字符指針變量類型是什么1.2字符指針變量的兩種使用方法&#xff1a;1.3字符指針筆試題講解1.3.1 代碼解剖 2.數組指針變量2.1 什么是數組指針2.2 數組指針變量是什么&#xff1f;2.2.3 數組指針變量的舉例 2.3數組指針和指針數組的區別是什么&#…

npm ERR! node-sass@4.13.0 postinstall: `node scripts/build.js`

npm ERR! node-sass4.13.0 postinstall: node scripts/build.js npm config set sass_binary_sitehttps://npm.taobao.org/mirrors/node-sass npm install npm run dev Microsoft Windows [版本 10.0.19045.2965] (c) Microsoft Corporation。保留所有權利。C:\Users\Administr…

4.操作系統常見面試題(2)

3.4 虛擬內存 直接使?物理內存會產??些問題 1. 內存空間利?率的問題&#xff1a;各個進程對內存的使?會導致內存碎?化&#xff0c;當要? malloc 分配?塊很?的內存空間時&#xff0c;可能會出現雖然有?夠多的空閑物理內存&#xff0c;卻沒有?夠?的連續空閑內存這種…

手動實現 git 的 git diff 功能

這是 git diff 后的效果&#xff0c;感覺挺簡單的&#xff0c;不就是 比較新舊版本&#xff0c;新增了就用 "" 顯示新加一行&#xff0c;刪除了就用 "-" 顯示刪除一行&#xff0c;修改了一行就用 "-"、"" 顯示將舊版本中的該行干掉了并…

騰訊云AMD服務器標準型SA5實例AMD EPYC Bergamo處理器

騰訊云服務器標準型SA5實例是最新一代的標準型實例&#xff0c;CPU采用AMD EPYC? Bergamo全新處理器&#xff0c;采用最新DDR5內存&#xff0c;默認網絡優化&#xff0c;最高內網收發能力達4500萬pps。騰訊云百科txybk.com分享騰訊云標準型SA5云服務器CPU、內存、網絡、性能、…

Python 忽略warning警告錯誤 + 跳過報錯繼續執行程序

如何主動產生warning錯誤: import warnings def fxn(): warnings.warn("deprecated", DeprecationWarning) with warnings.catch_warnings(): warnings.simplefilter("ignore") fxn() 那么如何來控制警告錯誤的輸出呢? import warnings warnings.fi…