使用js實現動態背景
本文實例為大傢分享瞭js實現動態背景的具體代碼,供大傢參考,具體內容如下
1.將下面的代碼復制並存為js文件
window.onload = function () { //定義body的margin由默認值8px->0px document.body.style.margin = "0"; document.body.style.background = "#30333F"; //創建canvas畫佈 document.body.appendChild(document.createElement('canvas')); var canvas = document.querySelector('canvas'), ctx = canvas.getContext('2d') //ctx返回一個在canvas上畫圖的api/dom canvas.width = window.innerWidth; canvas.height = window.innerHeight; canvas.style.position = 'fixed'; ctx.lineWidth = .3; ctx.strokeStyle = (new Color(150)).style; //定義鼠標覆蓋范圍 var mousePosition = { x: 30 * canvas.width / 100, y: 30 * canvas.height / 100 }; var dots = { nb: 1000,//Dot的總數 distance: 50, d_radius: 100, array: [] }; //創建顏色類,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); } //創建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對象的移動 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對象的連接 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();//創建從當前點回到起始點的路徑 } } } } } function createDots() {//創建nb個Dot對象 for (i = 0; i < dots.nb; i++) { dots.array.push(new Dot()); } } function drawDots() {//引用Dot原型鏈,使用draw方法,在canvas上畫出Dot對象 for (i = 0; i < dots.nb; i++) { var dot = dots.array[i]; dot.draw(); } } function animateDots() { ctx.clearRect(0, 0, canvas.width, canvas.height);//清除畫佈,否則線條會連在一起 moveDots(); connectDots(); drawDots(); requestAnimationFrame(animateDots); } createDots();//使用創建Dot類函數 requestAnimationFrame(animateDots);//使用canvas獨有的60Hz刷新屏幕畫佈的方法 document.querySelector('canvas').addEventListener('mousemove', function (e) { mousePosition.x = e.pageX; mousePosition.y = e.pageY; }) document.querySelector('canvas').addEventListener('mouseleave', function (e) {//鼠標離開時,連接自動返回到畫佈中心 mousePosition.x = canvas.width / 2; mousePosition.y = canvas.height / 2; }) }
2.然後在需要使用動態背景的html頁面引入js文件就可以瞭
效果如下:
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- JavaScript+Canvas實現帶跳動效果的粒子動畫
- JS+Canvas實現接球小遊戲的示例代碼
- JavaScript實現環繞鼠標旋轉效果
- layui實現登陸界面驗證碼
- JavaScript+html實現前端頁面隨機二維碼驗證