這篇文章跟大家介紹一下使用HTML5 canvas繪製酷炫能量線條特效的方法。有一定的參考價值,有需要的朋友可以參考一下,希望對你們有幫助。
上面是效果圖,下面直接附上js程式碼,希望對大家有幫助! !
// UTILconst PI = Math.PI, TWO_PI = Math.PI * 2;const Util = {};Util.timeStamp = function() { return window.performance.now();};Util.random = function(min, max) { return min + Math.random() * (max - min);};Util.map = function(a, b, c, d, e) { return (a - b) / (c - b) * (e - d) + d;};Util.lerp = function(value1, value2, amount) { return value1 + (value2 - value1) * amount;};Util.clamp = function(value, min, max) { return Math.max(min, Math.min(max, value));};// Vectorclass Vector { constructor(x, y) { this.x = x || 0; this.y = y || 0; } set(x, y) { this.x = x; this.y = y; } reset() { this.x = 0; this.y = 0; } fromAngle(angle) { let x = Math.cos(angle), y = Math.sin(angle); return new Vector(x, y); } add(vector) { this.x += vector.x; this.y += vector.y; } sub(vector) { this.x -= vector.x; this.y -= vector.y; } mult(scalar) { this.x *= scalar; this.y *= scalar; } p(scalar) { this.x /= scalar; this.y /= scalar; } dot(vector) { return vector.x * this.x + vector.y * this.y; } limit(limit_value) { if (this.mag() > limit_value) this.setMag(limit_value); } mag() { return Math.hypot(this.x, this.y); } setMag(new_mag) { if (this.mag() > 0) { this.normalize(); } else { this.x = 1; this.y = 0; } this.mult(new_mag); } normalize() { let mag = this.mag(); if (mag > 0) { this.x /= mag; this.y /= mag; } } heading() { return Math.atan2(this.y, this.x); } setHeading(angle) { let mag = this.mag(); this.x = Math.cos(angle) * mag; this.y = Math.sin(angle) * mag; } dist(vector) { return new Vector(this.x - vector.x, this.y - vector.y).mag(); } angle(vector) { return Math.atan2(vector.y - this.y, vector.x - this.x); } copy() { return new Vector(this.x, this.y); }}// Init canvaslet canvas = document.createElement("canvas"), ctx = canvas.getContext("2d"), H = (canvas.height = window.innerHeight), W = (canvas.width = window.innerWidth);document.body.appendChild(canvas);// Mouselet mouse = { x: W/2, y: H/2};canvas.onmousemove = function(event) { mouse.x = event.clientX - canvas.offsetLeft; mouse.y = event.clientY - canvas.offsetTop;};document.body.onresize = function(event){ H = (canvas.height = window.innerHeight); W = (canvas.width = window.innerWidth);}// Let's goclass Arrow { constructor(x, y, target) { this.position = new Vector(x, y); this.velocity = new Vector().fromAngle(Util.random(0,TWO_PI)); this.acceleration = new Vector(0, 0); this.target = target; this.travelled_distance = 0; this.min_size = 1; this.max_size = 6; this.size = Util.random(this.min_size, this.max_size); this.zone = this.size * 4; this.topSpeed = Util.map(this.size,this.min_size,this.max_size,40,10); let tailLength = Math.floor(Util.map(this.size, this.min_size, this.max_size, 4, 16)); this.tail = []; for (let i = 0; i < tailLength; i++) { this.tail.push({ x: this.position.x, y: this.position.y }); } this.wiggle_speed = Util.map(this.size, this.min_size, this.max_size, 2 , 1.2); this.blink_offset = Util.random(0, 100); this.alpha = Util.random(0.1,1) } render() { this.update(); this.draw(); } update() { let old_position = this.position.copy(); // Focus on target let t = new Vector(this.target.x, this.target.y), angle = this.position.angle(t); let d_f_target = t.dist(this.position); let f = new Vector().fromAngle(angle); f.setMag(Util.map(Util.clamp(d_f_target,0,400), 0, 400, 0, this.topSpeed * 0.1)); this.addForce(f); // Update position and velocity this.velocity.add(this.acceleration); if(d_f_target < 800){ this.velocity.limit(Util.map(Util.clamp(d_f_target,0,800), 0, 800, this.topSpeed*0.4, this.topSpeed)); }else{ this.velocity.limit(this.topSpeed); } this.position.add(this.velocity); // Reset acceleration for the next loop this.acceleration.mult(0); this.travelled_distance += old_position.dist(this.position); let wiggle = Math.sin(frame * this.wiggle_speed) * Util.map(this.velocity.mag(), 0, this.topSpeed, 0, this.size); let w_a = this.velocity.heading() + Math.PI / 2; let w_x = this.position.x + Math.cos(w_a) * wiggle, w_y = this.position.y + Math.sin(w_a) * wiggle; this.travelled_distance = 0; let from = this.tail.length - 1, to = 0; let n = new Vector().fromAngle(Util.random(0,TWO_PI)); n.setMag(Math.random()*this.size); var tail = { x: w_x+ n.x, y: w_y + n.y}; this.tail.splice(from, 1); this.tail.splice(to, 0, tail); } draw() { let energy = Util.map(this.velocity.mag(),0,this.topSpeed,0.1,1); let color = "hsl("+Math.sin((frame + this.blink_offset) * 0.1) * 360+",50%,"+ Util.map(this.velocity.mag(),0,this.topSpeed,40,100) * this.alpha +"%)"; ctx.globalAlpha = this.alpha; ctx.strokeStyle = color; for (let i = 0; i < this.tail.length - 1; i++) { let t = this.tail[i], next_t = this.tail[i + 1]; ctx.lineWidth = Util.map(i, 0, this.tail.length - 1, this.size, 1); ctx.beginPath(); ctx.moveTo(t.x, t.y); ctx.lineTo(next_t.x, next_t.y); ctx.closePath(); ctx.stroke(); } let gradient_size = 140 * energy;var grd = ctx.createRadialGradient( this.position.x,this.position.y , 5, this.position.x,this.position.y, gradient_size);grd.addColorStop(0, "rgba(255,255,255,0.01)");grd.addColorStop(0.1, "rgba(255,120,200,0.02)");grd.addColorStop(0.9, "rgba(255,255,120,0)");grd.addColorStop(1, "rgba(0,0,0,0)");// Fill with gradientctx.fillStyle = grd;ctx.fillRect(this.position.x - gradient_size / 2 ,this.position.y - gradient_size / 2 , gradient_size, gradient_size); ctx.globalAlpha = energy+0.2; ctx.fillStyle = "white"; for(let i = 0; i < 4; i++){ let n = new Vector().fromAngle(Util.random(0,TWO_PI)); n.setMag(Math.random()*energy*100); n.add(this.position); ctx.beginPath(); ctx.arc(n.x,n.y,Math.random(),0,TWO_PI) ctx.fill(); } } addForce(vector) { this.acceleration.add(vector); } avoid(others) { others.forEach(other => { if (other !== this) { let dist = this.position.dist(other.position), max_dist = this.zone + other.size; if (max_dist - dist >= 0) { let angle = other.position.angle(this.position); let force = new Vector().fromAngle(angle); force.setMag(Util.map(dist, 0, max_dist, 2, 0)); this.addForce(force); } } }); }}let arrows = [];for (let i = 0; i < 100; i++) { arrows.push(new Arrow(W / 2, H / 2, mouse));}let frame = 0;ctx.strokeStyle = "white";function loop() { ctx.fillStyle="black"; ctx.globalCompositeOperation = "source-over"; ctx.globalAlpha = 0.2; ctx.fillRect(0, 0, W, H); ctx.globalAlpha = 1; ctx.globalCompositeOperation = "lighter"; arrows.forEach(a => { a.avoid(arrows); }); arrows.forEach(a => { a.render(); }); frame += 1; requestAnimationFrame(loop);}ctx.lineCap = "round";ctx.lineJoin = "round";loop();
建議學習:Html5影片教學
以上是HTML5 canvas如何繪製酷炫能量線條效果(附程式碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

H5開發需要掌握的工具和框架包括Vue.js、React和Webpack。 1.Vue.js適用於構建用戶界面,支持組件化開發。 2.React通過虛擬DOM優化頁面渲染,適合複雜應用。 3.Webpack用於模塊打包,優化資源加載。

HTML5hassignificantlytransformedwebdevelopmentbyintroducingsemanticelements,enhancingmultimediasupport,andimprovingperformance.1)ItmadewebsitesmoreaccessibleandSEO-friendlywithsemanticelementslike,,and.2)HTML5introducednativeandtags,eliminatingthenee

H5通過語義化元素和ARIA屬性提升網頁的可訪問性和SEO效果。 1.使用、、等元素組織內容結構,提高SEO。 2.ARIA屬性如aria-label增強可訪問性,輔助技術用戶可順利使用網頁。

"h5"和"HTML5"在大多數情況下是相同的,但它們在某些特定場景下可能有不同的含義。 1."HTML5"是W3C定義的標準,包含新標籤和API。 2."h5"通常是HTML5的簡稱,但在移動開發中可能指基於HTML5的框架。理解這些區別有助於在項目中準確使用這些術語。

H5,即HTML5,是HTML的第五個版本,它為開發者提供了更強大的工具集,使得創建複雜的網頁應用變得更加簡單。 H5的核心功能包括:1)元素允許在網頁上繪製圖形和動畫;2)語義化標籤如、等,使網頁結構清晰,利於SEO優化;3)新API如GeolocationAPI,支持基於位置的服務;4)跨瀏覽器兼容性需要通過兼容性測試和Polyfill庫來確保。

如何創建 H5 鏈接?確定鏈接目標:獲取 H5 頁面或應用程序的 URL。創建 HTML 錨點:使用 <a> 標記創建錨點並指定鏈接目標URL。設置鏈接屬性(可選):根據需要設置 target、title 和 onclick 屬性。添加到網頁:將 HTML 錨點代碼添加到希望鏈接出現的網頁中。

解決 H5 兼容問題的方法包括:使用響應式設計,允許網頁根據屏幕尺寸調整佈局。採用跨瀏覽器測試工具,在發布前測試兼容性。使用 Polyfill,為舊瀏覽器提供對新 API 的支持。遵循 Web 標準,使用有效的代碼和最佳實踐。使用 CSS 預處理器,簡化 CSS 代碼並提高可讀性。優化圖像,減小網頁大小並加快加載速度。啟用 HTTPS,確保網站的安全性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3漢化版
中文版,非常好用