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;
z-index:-2;
}
</style>
<body>
<div class="canvaszz"> </div>
<canvas id="canvas" class="canvas"></canvas>
</body>
<script>
drawStars();
//畫星空背景
function drawStars() {
var canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d'),
w = canvas.width = window.innerWidth,
h = canvas.height = window.innerHeight,
hue = 217, //色調色彩
stars = [], //保存所有星星
count = 0, //用于計算星星
maxStars = 1300; //星星數量
//canvas2是用來創建星星的源影像,即母版,
//根據星星自身屬性的大小來設定
var canvas2 = document.createElement('canvas'),
ctx2 = canvas2.getContext('2d');
canvas2.width = 100;
canvas2.height = 100;
//創建徑向漸變,從坐標(half,half)半徑為0的圓開始,
//到坐標為(half,half)半徑為half的圓結束
var half = canvas2.width / 2,
gradient2 = ctx2.createRadialGradient(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 = Math.round(Math.sqrt(max * max + max * max));
//星星移動范圍,值越大范圍越小,
return diameter / 2;
}
var Star = function() {
//星星移動的半徑
this.orbitRadius = random(maxOrbit(w, h));
//星星大小,半徑越小,星星也越小,即外面的星星會比較大
this.radius = random(60, this.orbitRadius) / 8;
//所有星星都是以螢屏的中心為圓心
this.orbitX = w / 2;
this.orbitY = h / 2;
//星星在旋轉圓圈位置的角度,每次增加speed值的角度
//利用正弦余弦算出真正的x、y位置
this.timePassed = random(0, maxStars);
//星星移動速度
this.speed = random(this.orbitRadius) / 50000;
//星星影像的透明度
this.alpha = random(2, 10) / 10;
count++;
stars[count] = this;
}
Star.prototype.draw = function() {
//星星圍繞在以螢屏中心為圓心,半徑為orbitRadius的圓旋轉
var x = Math.sin(this.timePassed) * this.orbitRadius + this.orbitX,
y = Math.cos(this.timePassed) * this.orbitRadius + this.orbitY,
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次
window.requestAnimationFrame(animation);
}
animation();
}
</script>
</html>
如果有幫助到你的話
留個足跡再走吧 ( ?? ω ?? )? ~
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/251789.html
標籤:其他
上一篇:計算公式 - 四則運算實作
下一篇:JS中的防抖函式
