JS+canvas画布实现炫酷的旋转星空效果示例
程序员文章站
2023-11-20 17:40:16
本文实例讲述了js+canvas画布实现炫酷的旋转星空效果。分享给大家供大家参考,具体如下:
canvas是html5的新标签,其画布功能尤为强大。当然了canvas在i...
本文实例讲述了js+canvas画布实现炫酷的旋转星空效果。分享给大家供大家参考,具体如下:
canvas是html5的新标签,其画布功能尤为强大。当然了canvas在ie10以下浏览器是不兼容的,所以呢为了特效肯定是牺牲一定的兼容性。这里呢,分享一个基于canvas开发的浩瀚星河插件,其实这个源代码是网上下载的,我把它整合了一下,重新封装一些参数提供更多的可改项。
首先引入两个javascript脚本,一个是jquery插件,另一个是封装好的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,当然如果浏览器不支持canvas的话,运行程序没效果,并显示“该浏览器不支持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;//星星数量 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; //星星移动范围,值越大范围越小, } 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; //星星移动速度 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(); }
调用方法:这里没写任何样式所以默认情况下是全屏效果
$(function(){ //canvas的id名,星星颜色(hsla的hue色调),星星数量,星星半径比,星星移动范围(值越大范围越小),星星移动速度(值越大速度越慢),星星拖尾效果(0~1值越小拖尾越明显) canvas("starts",230,1000,60,2,50000,0.5); });
效果:
然后我再加3个canvas,改些参数做效果对比吧
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色调),星星数量,星星半径比,星星移动范围(值越大范围越小),星星移动速度(值越大速度越慢),星星拖尾效果(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"); });
就这样,四个浩瀚的宇宙出来了
更多关于javascript相关内容还可查看本站专题:《javascript+html5特效与技巧汇总》、《javascript图形绘制技巧总结》、《javascript错误与调试技巧总结》、《javascript数据结构与算法技巧总结》、《javascript遍历算法与技巧总结》及《javascript数学运算用法总结》
希望本文所述对大家javascript程序设计有所帮助。