JS+canvas畫布實(shí)現(xiàn)炫酷的旋轉(zhuǎn)星空效果示例
本文實(shí)例講述了JS+canvas畫布實(shí)現(xiàn)炫酷的旋轉(zhuǎn)星空效果。分享給大家供大家參考,具體如下:
canvas是html5的新標(biāo)簽,其畫布功能尤為強(qiáng)大。當(dāng)然了canvas在IE10以下瀏覽器是不兼容的,所以呢為了特效肯定是犧牲一定的兼容性。這里呢,分享一個(gè)基于canvas開發(fā)的浩瀚星河插件,其實(shí)這個(gè)源代碼是網(wǎng)上下載的,我把它整合了一下,重新封裝一些參數(shù)提供更多的可改項(xiàng)。
首先引入兩個(gè)javascript腳本,一個(gè)是jquery插件,另一個(gè)是封裝好的cosmos_canvas.js
<script src="js/jquery-1.11.0.js" type="text/javascript" charset="utf-8"></script> <script src="canvas/cosmos_canvas.js" type="text/javascript" charset="utf-8"></script>
HTML,當(dāng)然如果瀏覽器不支持canvas的話,運(yùn)行程序沒效果,并顯示“該瀏覽器不支持canvas”
<canvas id="starts">該瀏覽器不支持canvas</canvas>
cosmos_canvas.js腳本
function canvas(id,starscolor,starsamount,starsradius,movrange,speed,trailing){
//宇宙特效
"use strict";
var canvas = document.getElementById(id),
ctx = canvas.getContext('2d'),
w = canvas.width = window.innerWidth,
h = canvas.height = window.innerHeight,
hue = starscolor,//230
stars = [],
count = 0,
maxStars = starsamount;//星星數(shù)量
var canvas2 = document.createElement('canvas'),
ctx2 = canvas2.getContext('2d');
canvas2.width = 100;
canvas2.height = 100;
var half = canvas2.width / 2,
gradient2 = ctx2.createRadialGradient(half, half, 0, half, half, half);
gradient2.addColorStop(0.025, '#CCC');
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;
}
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 / movrange;
//星星移動(dòng)范圍,值越大范圍越小,
}
var Star = function() {
this.orbitRadius = random(maxOrbit(w, h));
this.radius = random(starsradius, this.orbitRadius) / 8;
//星星半徑大小
this.orbitX = w / 2;
this.orbitY = h / 2;
this.timePassed = random(0, maxStars);
this.speed = random(this.orbitRadius) / speed;
//星星移動(dòng)速度
this.alpha = random(2, 10) / 10;
count++;
stars[count] = this;
}
Star.prototype.draw = function() {
var x = Math.sin(this.timePassed) * this.orbitRadius + this.orbitX,
y = Math.cos(this.timePassed) * this.orbitRadius + this.orbitY,
twinkle = random(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;
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 = trailing; //尾巴
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();
};
window.requestAnimationFrame(animation);
}
animation();
}
調(diào)用方法:這里沒寫任何樣式所以默認(rèn)情況下是全屏效果
$(function(){
//canvas的id名,星星顏色(hsla的hue色調(diào)),星星數(shù)量,星星半徑比,星星移動(dòng)范圍(值越大范圍越小),星星移動(dòng)速度(值越大速度越慢),星星拖尾效果(0~1值越小拖尾越明顯)
canvas("starts",230,1000,60,2,50000,0.5);
});
效果:

然后我再加3個(gè)canvas,改些參數(shù)做效果對(duì)比吧
HTML
<canvas id="starts">該瀏覽器不支持canvas</canvas> <canvas id="starts1">該瀏覽器不支持canvas</canvas> <canvas id="starts2">該瀏覽器不支持canvas</canvas> <canvas id="starts3">該瀏覽器不支持canvas</canvas>
javascript
$(function(){
//canvas的id名,星星顏色(hsla的hue色調(diào)),星星數(shù)量,星星半徑比,星星移動(dòng)范圍(值越大范圍越小),星星移動(dòng)速度(值越大速度越慢),星星拖尾效果(0~1值越小拖尾越明顯)
canvas("starts",230,1000,60,2,50000,0.5);
canvas("starts1",160,800,70,2.5,55000,0.4);
canvas("starts2",80,600,80,3,60000,0.3);
canvas("starts3",0,400,90,3.5,65000,0.2);
$("canvas").width($(window).width()/2);
$("canvas").height($(window).height()/2);
$("canvas").css("float","left");
});
就這樣,四個(gè)浩瀚的宇宙出來了

更多關(guān)于JavaScript相關(guān)內(nèi)容還可查看本站專題:《JavaScript+HTML5特效與技巧匯總》、《JavaScript圖形繪制技巧總結(jié)》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章
關(guān)于火狐(firefox)及ie下event獲取的兩種方法
經(jīng)常有同事問我在火狐瀏覽器怎么獲取event的方法,大多是想獲取event.keyCode的功能,接下來為您介紹兩種實(shí)現(xiàn)方法,需要了解的朋友可以參考下2012-12-12
Javascript中匿名函數(shù)的多種調(diào)用方式總結(jié)
這篇文章主要是對(duì)Javascript中匿名函數(shù)的多種調(diào)用方式進(jìn)行了詳細(xì)的總結(jié)介紹。需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-12-12
用Javascript實(shí)現(xiàn)錨點(diǎn)(Anchor)間平滑跳轉(zhuǎn)
本文介紹的方法,實(shí)現(xiàn)了錨點(diǎn)(Anchor)間平滑跳轉(zhuǎn),效果非常不錯(cuò)。2009-09-09
關(guān)于include標(biāo)簽導(dǎo)致js路徑找不到的問題分析及解決
本文為大家詳細(xì)介紹下關(guān)于使用jsp:include標(biāo)簽及<%@ include標(biāo)簽時(shí)要注意的事項(xiàng)以及實(shí)測發(fā)現(xiàn)問題并解決問題的全過程,感興趣的各位可以參考下哈,希望對(duì)大家有所幫助2013-07-07
基于JavaScript代碼實(shí)現(xiàn)隨機(jī)漂浮圖片廣告
在網(wǎng)上有很多這樣的代碼,不過未必符合W3C標(biāo)準(zhǔn),因?yàn)樵陬^部加上<!DOCTYPE html>類似標(biāo)簽之后,漂浮效果就會(huì)失效,下面分享一個(gè)符合標(biāo)準(zhǔn)的漂浮代碼,使需要的朋友免去大量改造代碼的繁瑣2016-01-01
bootstrap select2插件用ajax來獲取和顯示數(shù)據(jù)的實(shí)例
今天小編就為大家分享一篇bootstrap select2插件用ajax來獲取和顯示數(shù)據(jù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08

