html灰网格背景代码,HTML5网格纹理背景

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

$(function() {

var mainCanvas = document.getElementById('bg');

var mainContext = mainCanvas.getContext('2d');

var theWidth = window.innerWidth;

var theHeight = window.innerHeight;

mainCanvas.width = theWidth;

mainCanvas.height = theHeight;

var howManyCircles = 40;

var circles = new Array();

var requestAnimationFrame = window.requestAnimationFrame ||

window.mozRequestAnimationFrame ||

window.webkitRequestAnimationFrame ||

window.msRequestAnimationFrame;

function Circle(radius, speed, size, xPos, yPos, opacity, circleColor) {

this.radius = radius;

this.speed = speed;

this.size = size;

this.xPos = xPos;

this.yPos = yPos;

this.opacity = opacity;

this.circleColor = circleColor;

this.counter = 0;

var signHelper = Math.floor(Math.random() * 2);

if (signHelper == 1) {

this.sign = -1;

} else {

this.sign = 1;

}

}

Circle.prototype.update = function () {

this.counter += this.sign * this.speed/20;

mainContext.beginPath();

mainContext.arc(this.xPos + Math.cos(this.counter / 100) * this.radius,

this.yPos + Math.sin(this.counter / 100) * this.radius,

this.size,

0,

Math.PI * 2,

false);

mainContext.closePath();

mainContext.fillStyle = this.circleColor + this.opacity + ')'; //dark blue

mainContext.fill();

};

// Register an event listener to

// call the resizeCanvas() function each time

// the window is resized.

window.addEventListener('resize', resizeCanvas, false);

// Draw canvas border for the first time.

resizeCanvas();

function setupCircles() {

//console.log("setup");

for (var i = 0; i < howManyCircles; i++) {

var opacity = .25 + Math.random() * .40;

var speed = 1 + Math.random() * 8;

var size = (1.6 - opacity) * (130 + Math.random() * 200);

var radius = 100 + Math.random() * 100;

var randomX = Math.round(Math.random() * (theWidth + size)) - size/2;

var randomY = Math.round(Math.random() * (theHeight + size)) - size/2;

var colorRadomizer = (i/howManyCircles) * 100;

if(colorRadomizer <= 40){

var circleColor = 'rgba(26, 99, 142,'; //blue

}else if((colorRadomizer > 40) && (colorRadomizer <= 80)) {

var circleColor = 'rgba(0, 125, 105,'; //green

}else{

var circleColor = 'rgba(21, 216, 223,'; //aqua

size /= 2;

radius /= 2;

opacity /= 2;

}

var circle = new Circle(radius, speed, size, randomX, randomY, opacity, circleColor);

circles.push(circle);

}

drawAndUpdate();

}

function drawAndUpdate() {

//console.log("draw & update " + theWidth + " " + theHeight);

requestAnimationFrame(drawAndUpdate);

mainContext.clearRect(0, 0, theWidth, theHeight);

for (var i = 0; i < circles.length; i++) {

var myCircle = circles[i];

myCircle.update();

}

}

// Runs each time the DOM window resize event fires.

// Resets the canvas dimensions to match window,

// then draws the new borders accordingly.

function resizeCanvas() {

//console.log("resized");

theWidth = window.innerWidth;

theHeight = window.innerHeight;

mainCanvas.width = theWidth;

mainCanvas.height = theHeight;

setupCircles();

}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值