使用js實(shí)現(xiàn)動(dòng)態(tài)背景
本文實(shí)例為大家分享了js實(shí)現(xiàn)動(dòng)態(tài)背景的具體代碼,供大家參考,具體內(nèi)容如下
1.將下面的代碼復(fù)制并存為js文件
window.onload = function () { //定義body的margin由默認(rèn)值8px->0px document.body.style.margin = "0"; document.body.style.background = "#30333F"; //創(chuàng)建canvas畫(huà)布 document.body.appendChild(document.createElement('canvas')); var canvas = document.querySelector('canvas'), ctx = canvas.getContext('2d') //ctx返回一個(gè)在canvas上畫(huà)圖的api/dom canvas.width = window.innerWidth; canvas.height = window.innerHeight; canvas.style.position = 'fixed'; ctx.lineWidth = .3; ctx.strokeStyle = (new Color(150)).style; //定義鼠標(biāo)覆蓋范圍 var mousePosition = { x: 30 * canvas.width / 100, y: 30 * canvas.height / 100 }; var dots = { nb: 1000,//Dot的總數(shù) distance: 50, d_radius: 100, array: [] }; //創(chuàng)建顏色類,Color類返回字符串型rgba(*,*,*,.8) function mixComponents(comp1, weight1, comp2, weight2) { return (comp1 * weight1 + comp2 * weight2) / (weight1 + weight2); } function averageColorStyles(dot1, dot2) { var color1 = dot1.color, color2 = dot2.color; var r = mixComponents(color1.r, dot1.radius, color2.r, dot2.radius), g = mixComponents(color1.g, dot1.radius, color2.g, dot2.radius), b = mixComponents(color1.b, dot1.radius, color2.b, dot2.radius); return createColorStyle(Math.floor(r), Math.floor(g), Math.floor(b)); } function colorValue(min) { return Math.floor(Math.random() * 255 + min); } function createColorStyle(r, g, b) { return 'rgba(' + r + ',' + g + ',' + b + ', 0.8)'; } function Color(min) { min = min || 0; this.r = colorValue(min); this.g = colorValue(min); this.b = colorValue(min); this.style = createColorStyle(this.r, this.g, this.b); } //創(chuàng)建Dot類以及一系列方法 function Dot() { this.x = Math.random() * canvas.width; this.y = Math.random() * canvas.height; this.vx = -.5 + Math.random(); this.vy = -.5 + Math.random(); this.radius = Math.random() * 2; this.color = new Color(); } Dot.prototype = { draw: function () { ctx.beginPath(); ctx.fillStyle = this.color.style; ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false); ctx.fill(); } }; function moveDots() {//Dot對(duì)象的移動(dòng) for (i = 0; i < dots.nb; i++) { var dot = dots.array[i]; if (dot.y < 0 || dot.y > canvas.height) { dot.vx = dot.vx; dot.vy = - dot.vy; } else if (dot.x < 0 || dot.x > canvas.width) { dot.vx = - dot.vx; dot.vy = dot.vy; } dot.x += dot.vx; dot.y += dot.vy; } } function connectDots() {//DOt對(duì)象的連接 for (i = 0; i < dots.nb; i++) { for (j = i; j < dots.nb; j++) { i_dot = dots.array[i]; j_dot = dots.array[j]; if ((i_dot.x - j_dot.x) < dots.distance && (i_dot.y - j_dot.y) < dots.distance && (i_dot.x - j_dot.x) > - dots.distance && (i_dot.y - j_dot.y) > - dots.distance) { if ((i_dot.x - mousePosition.x) < dots.d_radius && (i_dot.y - mousePosition.y) < dots.d_radius && (i_dot.x - mousePosition.x) > - dots.d_radius && (i_dot.y - mousePosition.y) > - dots.d_radius) { ctx.beginPath(); ctx.strokeStyle = averageColorStyles(i_dot, j_dot); ctx.moveTo(i_dot.x, i_dot.y); ctx.lineTo(j_dot.x, j_dot.y); ctx.stroke();//繪制定義的路線 ctx.closePath();//創(chuàng)建從當(dāng)前點(diǎn)回到起始點(diǎn)的路徑 } } } } } function createDots() {//創(chuàng)建nb個(gè)Dot對(duì)象 for (i = 0; i < dots.nb; i++) { dots.array.push(new Dot()); } } function drawDots() {//引用Dot原型鏈,使用draw方法,在canvas上畫(huà)出Dot對(duì)象 for (i = 0; i < dots.nb; i++) { var dot = dots.array[i]; dot.draw(); } } function animateDots() { ctx.clearRect(0, 0, canvas.width, canvas.height);//清除畫(huà)布,否則線條會(huì)連在一起 moveDots(); connectDots(); drawDots(); requestAnimationFrame(animateDots); } createDots();//使用創(chuàng)建Dot類函數(shù) requestAnimationFrame(animateDots);//使用canvas獨(dú)有的60Hz刷新屏幕畫(huà)布的方法 document.querySelector('canvas').addEventListener('mousemove', function (e) { mousePosition.x = e.pageX; mousePosition.y = e.pageY; }) document.querySelector('canvas').addEventListener('mouseleave', function (e) {//鼠標(biāo)離開(kāi)時(shí),連接自動(dòng)返回到畫(huà)布中心 mousePosition.x = canvas.width / 2; mousePosition.y = canvas.height / 2; }) }
2.然后在需要使用動(dòng)態(tài)背景的html頁(yè)面引入js文件就可以了
效果如下:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS/HTML5游戲常用算法之碰撞檢測(cè) 像素檢測(cè)算法實(shí)例詳解
這篇文章主要介紹了JS/HTML5游戲常用算法之碰撞檢測(cè) 像素檢測(cè)算法,結(jié)合實(shí)例形式詳細(xì)分析了javascript像素檢測(cè)碰撞算法的原理、實(shí)現(xiàn)步驟及相關(guān)操作技巧,需要的朋友可以參考下2018-12-12JavaScript資源預(yù)加載組件和滑屏組件的使用推薦
這篇文章主要介紹了JavaScript資源預(yù)加載組件和滑屏組件的使用推薦,分別為preload和slide的用法講解,使用起來(lái)非常簡(jiǎn)單,需要的朋友可以參考下2016-03-03微信小程序?qū)崿F(xiàn)頁(yè)面監(jiān)聽(tīng)自定義組件的觸發(fā)事件
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)頁(yè)面監(jiān)聽(tīng)自定義組件的觸發(fā)事件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11實(shí)現(xiàn)兩個(gè)文本框同時(shí)輸入的實(shí)例
下面小編就為大家?guī)?lái)一篇實(shí)現(xiàn)兩個(gè)文本框同時(shí)輸入的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09兩種方法實(shí)現(xiàn)文本框輸入內(nèi)容提示消失
第一種方法:基于HTML5 input標(biāo)簽的新特性 - placeholder 。另外,x-webkit-speech 屬性可以實(shí)現(xiàn)語(yǔ)音輸入功能;第二種方法: 用span模擬,定位span,借助JS鍵盤(pán)事件判斷輸入,確定span里的內(nèi)容顯示隱藏2013-03-03ES6函數(shù)實(shí)現(xiàn)排它兩種寫(xiě)法解析
這篇文章主要介紹了ES6函數(shù)實(shí)現(xiàn)排它兩種寫(xiě)法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05TypeScript?Pinia實(shí)戰(zhàn)分享(Vuex和Pinia對(duì)比梳理總結(jié))
這篇文章主要介紹了TypeScript?Pinia實(shí)戰(zhàn)分享(Vuex和Pinia對(duì)比梳理總結(jié)),今天我們?cè)賮?lái)實(shí)戰(zhàn)下官方推薦的新的vue狀態(tài)管理工具Pini,感興趣的小伙伴可以參考一下2022-06-06javascript實(shí)現(xiàn)倒計(jì)時(shí)提示框
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)倒計(jì)時(shí)提示框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03javascript 利用Image對(duì)象實(shí)現(xiàn)的埋點(diǎn)(某處的點(diǎn)擊數(shù))統(tǒng)計(jì)
統(tǒng)計(jì)用戶頁(yè)面某處的點(diǎn)擊數(shù)或者執(zhí)行到程序中某個(gè)點(diǎn)的次數(shù);根據(jù)實(shí)際情況,創(chuàng)建多個(gè)Image對(duì)象,原則誰(shuí)空閑誰(shuí)做事,解下來(lái)詳細(xì)介紹,需要了解的朋友可以參考下2012-12-12