利用html5-canvas及javascript产生三维星空效果的代码

从网上搜集到的一段利用纯html5-canvas以及javascript生成三维星空效果的代码。代码的核心部分是“<script></script>”标签中包含的外部javascript代码-html5_3d_animation.js,即本文的第二段代码。代码的思想很简单:赋予每颗星星随机的x,y,z三个(三维)坐标,分别代表他们在星空(canvas)中的横坐标位置,纵坐标位置以及在三维空间中的尺度信息。利用canvas将每颗星星画在画布上。然后利用javascript中全局变量即局部变量的特点来计算星星的下一个位置信息,重绘。。。只需一些简单的修改,就可令代码产生不同的效果!

[javascript] view plain copy
  1. <html>  
  2. <head>  
  3. <title>Simple 3D on HTML5 canvas</title>  
  4. <meta http-equiv="content-type" content="text/html; charset=UTF-8">  
  5. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>  
  6. <script type="text/javascript" src="html5_3d_animation.js"></script>  
  7. <script language="javascript" type="text/javascript">  
  8.     $(function() {  
  9.         $("#html5_3d_animation").html5_3d_animation({  
  10.             window_width: '600',  
  11.             window_height: '300',  
  12.             window_background: '#00113F',  
  13.             star_count: '1000',  
  14.             star_color: '#FBFFAF',  
  15.             star_depth: '100'  
  16.         });  
  17.     });      
  18. </script>  
  19. </head>  
  20. <body>  
  21. <div class="wrap">  
  22.   <canvas id="html5_3d_animation">Internet Explorer Not  
  23.     Supported</canvas>  
  24.   <div id="show"></div>  
  25. </div>  
  26.   
  27. <div style="width:98%;margin:20px auto; padding:50px 0; clear:both; overflow:hidden;">  
  28. </body>  
  29. </html>  

html5_3d_animation.js

[javascript] view plain copy
  1. (function(a){  
  2.     a.fn.html5_3d_animation=function(p){  
  3.         var p=p||{};  
  4.   
  5.         var w_w=p&&p.window_width?p.window_width:"500";  
  6.         var w_h=p&&p.window_height?p.window_height:"400";  
  7.         var w_b=p&&p.window_background?p.window_background:"#000";  
  8.         var s_c=p&&p.star_count?p.star_count:"600";  
  9.         var s_color=p&&p.star_color?p.star_color:"#FFF";  
  10.         var s_d=p&&p.star_depth?p.star_depth:"250";  
  11.         var dom=a(this);  
  12.         var fov = parseInt(s_d);  
  13.         var SCREEN_WIDTH = parseInt(w_w);  
  14.         var SCREEN_HEIGHT = parseInt(w_h);  
  15.         var HALF_WIDTH = SCREEN_WIDTH/2;  
  16.         var HALF_HEIGHT = SCREEN_HEIGHT/2;  
  17.         var c_id = dom.attr("id");  
  18.         var numPoints = s_c;  
  19.         dom.attr({ width: w_w, height: w_h});  
  20.         setup();  
  21.         function setup()  
  22.         {  
  23.             function draw3Din2D(point3d)  
  24.             {  
  25.                 x3d = point3d[0];  
  26.                 y3d = point3d[1];  
  27.                 z3d = point3d[2];  
  28.                 var scale = fov/(fov+z3d);  
  29.                 var x2d = (x3d * scale) + HALF_WIDTH;  
  30.                 var y2d = (y3d * scale)  + HALF_HEIGHT;  
  31.   
  32.                 c.lineWidth= scale;  
  33.                 c.strokeStyle = s_color;  
  34.                 c.beginPath();  
  35.                 c.moveTo(x2d,y2d);  
  36.                 c.lineTo(x2d+scale,y2d);  
  37.                 c.stroke();  
  38.             }  
  39.   
  40.             var canvas = document.getElementById(c_id);  
  41.             var c = canvas.getContext('2d');  
  42.             var points = [];  
  43.   
  44.             function initPoints()  
  45.             {  
  46.                 for (i=0; i<numPoints; i++)  
  47.                 {  
  48.                     point = [(Math.random()*400)-200, (Math.random()*400)-200 , (Math.random()*400)-200 ];  
  49.                     points.push(point);  
  50.                 }  
  51.   
  52.             }  
  53.   
  54.             function render()  
  55.             {  
  56.                 c.fillStyle=w_b;  
  57.                 c.fillRect(0,0, SCREEN_WIDTH, SCREEN_HEIGHT);  
  58.   
  59.                 for (i=0; i<numPoints; i++)  
  60.                 {  
  61.                     point3d = points[i];  
  62.   
  63.                     z3d = point3d[2];  
  64.                     z3d-=4;  
  65.                     if(z3d<-fov) z3d +=400;  
  66.                     point3d[2] = z3d;  
  67.   
  68.                     draw3Din2D(point3d);  
  69.   
  70.                 }  
  71.                 var show = document.getElementById('show');  
  72.                 show.appendChild('p');  
  73.             }  
  74.   
  75.             initPoints();  
  76.   
  77.             var loop = setInterval(function(){  
  78.                 render();  
  79.             }, 50);  
  80.   
  81.         }  
  82.        }  
  83. })(jQuery);  
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答这个问题。以下是一个简单的 HTML5 canvasJavaScript 实现烟花效果的示例代码HTML 代码: ``` <canvas id="fireworks"></canvas> ``` JavaScript 代码: ``` var canvas = document.getElementById("fireworks"); var ctx = canvas.getContext("2d"); var particles = []; var numParticles = 100; var colors = ["#FF000", "#00FF00", "#000FF", "#FFFF00", "#00FFFF", "#FF00FF"]; function Particle(x, y, radius, color, vx, vy, ax, ay) { this.x = x; this.y = y; this.radius = radius; this.color = color; this.vx = vx; this.vy = vy; this.ax = ax; this.ay = ay; } Particle.prototype.draw = function() { ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, , 2 * Math.PI); ctx.fillStyle = this.color; ctx.fill(); } Particle.prototype.update = function() { this.vx += this.ax; this.vy += this.ay; this.x += this.vx; this.y += this.vy; this.radius -= .05; } function createParticles(x, y) { for (var i = ; i < numParticles; i++) { var radius = Math.random() * 5 + 2; var color = colors[Math.floor(Math.random() * colors.length)]; var angle = Math.random() * Math.PI * 2; var speed = Math.random() * 5 + 2; var vx = Math.cos(angle) * speed; var vy = Math.sin(angle) * speed; var ax = ; var ay = .1; particles.push(new Particle(x, y, radius, color, vx, vy, ax, ay)); } } function animate() { ctx.clearRect(, , canvas.width, canvas.height); for (var i = ; i < particles.length; i++) { particles[i].draw(); particles[i].update(); if (particles[i].radius <= ) { particles.splice(i, 1); } } requestAnimationFrame(animate); } canvas.addEventListener("click", function(event) { createParticles(event.clientX, event.clientY); }); animate(); ``` 这段代码创建了一个 canvas 元素,并在其中实现了一个烟花效果。当用户在 canvas 上点击时,会在点击位置创建一组粒子,这些粒子会以随机的颜色和速度向外扩散,并逐渐消失。这个效果通过创建一个 Particle 类来实现,每个粒子都是一个 Particle 的实例。在 animate 函数中,每个粒子都会被绘制和更新,同时过小的粒子会被从数组中删除。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值