HTML星空动图背景(canvas+JS)
HTML 星空动图背景(canvas+JS)
前⼏天看项⽬的时候看到⼀个星空动图,当时还以为是拿了⼀张GIF做背景,结果看代码半天没到,后来仔细阅读代码才发现原来是⽤canvas和JS做的,然后我这⼀抠啊,终于抠下来了。
先看效果(不晓得怎么截动图……)
(喜欢做⽹页动画的朋友可以拿去琢磨琢磨)
话不多说上代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<style>
body{
margin:0 auto;
overflow:hidden;
}
.canvaszz{
position:absolute;
background-image: url(img/21.jpg);
background-size: 100%;
width:100%;
height:100%;
z-index:-1;
filter:alpha(opacity=20);
opacity: 0.4;
}
.canvas {
position:absolute;
display:inline-block;
width:100%;
height:auto;
vertical-align:baseline;
vertical-align:baseline;
z-index:-2;
}
</style>
<body>
<div class="canvaszz"> </div>
<canvas id="canvas" class="canvas"></canvas>
</body>
<script>
drawStars();
//画星空背景
function drawStars() {
var canvas = ElementById('canvas'),
ctx = Context('2d'),
w = canvas.width = window.innerWidth,
h = canvas.height = window.innerHeight,
hue = 217, //⾊调⾊彩
stars = [], //保存所有星星
count = 0,  //⽤于计算星星canvas动画
maxStars = 1300; //星星数量
//canvas2是⽤来创建星星的源图像,即母版,
//根据星星⾃⾝属性的⼤⼩来设置
var canvas2 = ateElement('canvas'),
ctx2 = Context('2d');
canvas2.width = 100;
canvas2.height = 100;
/
/创建径向渐变,从坐标(half,half)半径为0的圆开始,
//到坐标为(half,half)半径为half的圆结束
var half = canvas2.width / 2,
gradient2 = ateRadialGradient(half, half, 0, half, half, half);  gradient2.addColorStop(0.025, '#CCC');
//hsl是另⼀种颜⾊的表⽰⽅式,
//h->hue,代表⾊调⾊彩,0为red,120为green,240为blue
//s->saturation,代表饱和度,0%-100%
//l->lightness,代表亮度,0%为black,100%位white
gradient2.addColorStop(0.1, 'hsl(' + hue + ', 61%, 33%)');
gradient2.addColorStop(0.25, 'hsl(' + hue + ', 64%, 6%)');
gradient2.addColorStop(1, 'transparent');
ctx2.fillStyle = gradient2;
ctx2.beginPath();
ctx2.arc(half, half, half, 0, Math.PI * 2);
ctx2.fill();
// End cache
function random(min, max) {
if(arguments.length < 2) {
max = min;
min = 0;
}
if(min > max) {
var hold = max;
max = min;
min = hold;
}
//返回min和max之间的⼀个随机值
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function maxOrbit(x, y) {
var max = Math.max(x, y),
diameter = und(Math.sqrt(max * max + max * max));
diameter = und(Math.sqrt(max * max + max * max));
/
/星星移动范围,值越⼤范围越⼩,
return diameter / 2;
}
var Star = function() {
//星星移动的半径
//星星⼤⼩,半径越⼩,星星也越⼩,即外⾯的星星会⽐较⼤
this.radius = random(60, bitRadius) / 8;
//所有星星都是以屏幕的中⼼为圆⼼
/
/星星在旋转圆圈位置的⾓度,每次增加speed值的⾓度
//利⽤正弦余弦算出真正的x、y位置
this.timePassed = random(0, maxStars);
//星星移动速度
this.speed = bitRadius) / 50000;
//星星图像的透明度
this.alpha = random(2, 10) / 10;
count++;
stars[count] = this;
}
Star.prototype.draw = function() {
/
/星星围绕在以屏幕中⼼为圆⼼,半径为orbitRadius的圆旋转
var x = Math.sin(this.timePassed) * bitRadius + bitX,
y = s(this.timePassed) * bitRadius + bitY,
twinkle = random(10);
//星星每次移动会有1/10的⼏率变亮或者变暗
if(twinkle === 1 && this.alpha > 0) {
//透明度降低,变暗
this.alpha -= 0.05;
} else if(twinkle === 2 && this.alpha < 1) {
//透明度升⾼,变亮
this.alpha += 0.05;
}
ctx.globalAlpha = this.alpha;
//使⽤canvas2作为源图像来创建星星,
//位置在x - this.radius / 2, y - this.radius / 2
//⼤⼩为 this.radius
ctx.drawImage(canvas2, x - this.radius / 2, y - this.radius / 2, this.radius, this.radius);    //没旋转⼀次,⾓度就会增加
this.timePassed += this.speed;
}
//初始化所有星星
for(var i = 0; i < maxStars; i++) {
new Star();
}
function animation() {
//以新图像覆盖已有图像的⽅式进⾏绘制背景颜⾊
ctx.globalCompositeOperation = 'source-over';
ctx.globalAlpha = 0.5; //尾巴
ctx.fillStyle = 'hsla(' + hue + ', 64%, 6%, 2)';
ctx.fillRect(0, 0, w, h)
//源图像和⽬标图像同时显⽰,重叠部分叠加颜⾊效果
ctx.globalCompositeOperation = 'lighter';
for(var i = 1, l = stars.length; i < l; i++) {
stars[i].draw();
};
};
//调⽤该⽅法执⾏动画,并且在重绘的时候调⽤指定的函数来更新动画    //回调的次数通常是每秒60次
}
animation();
}
</script>
</html>
如果有帮助到你的话
留个⾜迹再⾛吧 ( ·ω ·)✧ ~

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。