[js高手之路]html5 canvas动画教程 - 边界判断与小球粒子模拟喷泉,散弹效果

本文主要是介绍[js高手之路]html5 canvas动画教程 - 边界判断与小球粒子模拟喷泉,散弹效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

备注:本文后面的代码,如果加载了ball.js,那么请使用这篇文章[js高手之路] html5 canvas动画教程 - 匀速运动的ball.js代码.

本文,我们要做点有意思的效果,首先,来一个简单的边界判断,所谓边界判断:就是把物体的运动限定在一个范围内.我们先来一个简单的实例,在canvas上生成一个小球,小球的初始位置是在canvas的正中间,然后通过键盘的上下左右来移动小球的位置,如果小球碰到canvas的左边,那么不能再向左运动,其他方向同理.

要实现这个效果,只需要一个边界判断,即可。

 1 function checkBorder() {
 2     if ( ball.x < ball.radius ) { //碰到左边界
 3         ball.x = ball.radius;
 4     } else if ( ball.y < ball.radius ) { //碰到上边界
 5         ball.y = ball.radius;
 6     } else if ( ball.x > width - ball.radius ) { //碰到右边界
 7         ball.x = width - ball.radius;
 8     }else if ( ball.y > height - ball.radius ){
 9         ball.y = height - ball.radius;
10     }
11 }

左边界:只要判断小球的圆心x 如果小于小球的半径,那肯定是碰到了左边界,我们就让小球的中心x等于小球的半径。

右边界:只要判断小球的圆心x 如果大于canvas的宽度减去小球的半径,那肯定是碰到了右边界,我们就让小球的中心x等于canvas的宽度减去小球的半径

其他上下边界跟左右是同理。

完整的实例代码:

 1 <head>
 2     <meta charset='utf-8' />
 3     <style>
 4         #canvas {
 5             border: 1px dashed #aaa;
 6         }
 7     </style>
 8     <script src="./ball.js"></script>
 9     <script>
10         window.onload = function () {
11             var oCanvas = document.querySelector("#canvas"),
12                 oGc = oCanvas.getContext('2d'),
13                 width = oCanvas.width, height = oCanvas.height,
14                 ball = new Ball(width / 2, height / 2);
15                 ball.fill( oGc );
16             addEventListener("keydown", function (ev) {
17                 oGc.clearRect(0, 0, width, height);
18                 var oEvent = ev || event;
19                 switch (oEvent.keyCode) {
20                     case 37:
21                         ball.x -= 5;
22                         checkBorder();
23                         ball.fill(oGc);
24                         break;
25                     case 39:
26                         ball.x  = 5;
27                         checkBorder();
28                         ball.fill(oGc);
29                         break;
30                     case 38:
31                         ball.y -= 5;
32                         checkBorder();
33                         ball.fill(oGc);
34                         break;
35                     case 40:
36                         ball.y  = 5;
37                         checkBorder();
38                         ball.fill(oGc);
39                         break;
40                 }
41             }, false);
42             function checkBorder() {
43                 if ( ball.x < ball.radius ) { //碰到左边界
44                     ball.x = ball.radius;
45                 } else if ( ball.y < ball.radius ) { //碰到上边界
46                     ball.y = ball.radius;
47                 } else if ( ball.x > width - ball.radius ) { //碰到右边界
48                     ball.x = width - ball.radius;
49                 }else if ( ball.y > height - ball.radius ){
50                     ball.y = height - ball.radius;
51                 }
52             }
53         }
54     </script>
55 </head>
56 
57 <body>
58     <canvas id="canvas" width="1200" height="600"></canvas>
59 </body>

边界穿透:

如果小球向左运动,且完全超出左边界,我们就让他从右边出来,如果小球向右运动,且完全超出右边界,我们就让他从左边出来。上下方向同理

 1 <head>
 2     <meta charset='utf-8' />
 3     <style>
 4         #canvas {
 5             border: 1px dashed #aaa;
 6         }
 7     </style>
 8     <script src="./ball.js"></script>
 9     <script>
10         window.onload = function () {
11             var oCanvas = document.querySelector("#canvas"),
12                 oGc = oCanvas.getContext('2d'),
13                 width = oCanvas.width, height = oCanvas.height,
14                 ball = new Ball(width / 2, height / 2);
15             ball.fill(oGc);
16             addEventListener("keydown", function (ev) {
17                 oGc.clearRect(0, 0, width, height);
18                 var oEvent = ev || event;
19                 switch (oEvent.keyCode) {
20                     case 37:
21                         ball.x -= 5;
22                         checkBorder();
23                         ball.fill(oGc);
24                         break;
25                     case 39:
26                         ball.x  = 5;
27                         checkBorder();
28                         ball.fill(oGc);
29                         break;
30                     case 38:
31                         ball.y -= 5;
32                         checkBorder();
33                         ball.fill(oGc);
34                         break;
35                     case 40:
36                         ball.y  = 5;
37                         checkBorder();
38                         ball.fill(oGc);
39                         break;
40                 }
41             }, false);
42             function checkBorder() {
43                 if (ball.x < -ball.radius) { //完全超出左边界
44                     ball.x = width   ball.radius; //让球从右边出来
45                 } else if (ball.y < -ball.radius) { //完全超出上边界
46                     ball.y = height   ball.radius;//让球从下面出来
47                 } else if (ball.x > width   ball.radius) { //完全超出右边界
48                     ball.x = -ball.radius;//让球从左边出来
49                 } else if (ball.y > height   ball.radius) {//完全超出下边界
50                     ball.y = -ball.radius; //让球从上边出来
51                 }
52             }
53         }
54     </script>
55 </head>
56 
57 <body>
58     <canvas id="canvas" width="1200" height="600"></canvas>
59 </body>

散弹效果:

通过canvas的中心点,不停的向四周发射小球,形成散弹的效果. 

我不知道你们有没有这样的误区:不停的向四周发射小球,那是不是要不停的创造小球呢?如果你这样想,程序就算写出来了,肯定会卡死.

其实我们可以只创建,一定数量的小球,比如( 50, 60. ...100 ),然后当这些小球,完全超出的边界的时候,再把这些小球的圆心( x, y )设定到最开始的位置,再次随机x和y方向的速度,就可以达到目的了, 说白了就是,那个完全超出边界的小球,我们让他重新回到最初的地方,只是改变了他的颜色和速度,给人感觉就是那个发射小球的地方源源不断的在发射小球

完整的散弹效果:

 

 1 <head>
 2     <meta charset='utf-8' />
 3     <style>
 4         #canvas {
 5             border: 1px dashed #aaa;
 6         }
 7     </style>
 8     <script src="./ball.js"></script>
 9     <script>
10         window.onload = function () {
11             var oCanvas = document.querySelector("#canvas"),
12                 oGc = oCanvas.getContext('2d'),
13                 width = oCanvas.width, height = oCanvas.height,
14                 balls = [], n = 50;
15             function getRandColor() {
16                 return '#'   ( function( color ){
17                     return ( color  = '0123456789abcdef' [Math.floor( Math.random() * 16 )] ) && ( color.length == 6 ) ? color : arguments.callee( color );
18                 } )( '' );
19             }
20             for( var i = 0; i < n; i   ) {
21                 var ball = new Ball( width / 2, height / 2, 20, getRandColor() );
22                 ball.vx = ( Math.random() * 2 - 1 ) * 5;
23                 ball.vy = ( Math.random() * 2 - 1 ) * 5;
24                 balls.push( ball );
25             }
26             (function move(){
27                 oGc.clearRect( 0, 0, width, height );
28                 balls.forEach( function( ball ){
29                     if ( ball.x < -ball.radius
30                         || ball.x > width   ball.radius
31                         || ball.y < -ball.radius
32                         || ball.y > height   ball.radius ) {
33                             ball.x = width / 2;
34                             ball.y = height / 2;
35                             ball.vx = ( Math.random() * 2 - 1 ) * 5;
36                             ball.vy = ( Math.random() * 2 - 1 ) * 5;
37                     }
38                     ball.x  = ball.vx;
39                     ball.y  = ball.vy;
40                     ball.fill( oGc );
41                 } );
42                 requestAnimationFrame( move );
43             })();
44         }
45     </script>
46 </head>
47 <body>
48     <canvas id="canvas" width="1200" height="600"></canvas>
49 </body>

我们可以在之前的基础上,加上重力的影响,实现喷泉的效果

这张图,看着是不是更像喷泉?

 

 1 <head>
 2     <meta charset='utf-8' />
 3     <style>
 4         #canvas {
 5             border: 1px dashed #aaa;
 6         }
 7     </style>
 8     <script src="./ball.js"></script>
 9     <script>
10         window.onload = function () {
11             var oCanvas = document.querySelector("#canvas"),
12                 oGc = oCanvas.getContext('2d'),
13                 width = oCanvas.width, height = oCanvas.height,
14                 balls = [], n = 50, gravity = 0.2;
15             function getRandColor() {
16                 return '#'   (function (color) {
17                     return (color  = '0123456789abcdef'[Math.floor(Math.random() * 16)]) && (color.length == 6) ? color : arguments.callee(color);
18                 })('');
19             }
20             for (var i = 0; i < n; i  ) {
21                 var ball = new Ball(width / 2, height / 2, 20, getRandColor());
22                 ball.vx = (Math.random() * 2 - 1) * 5;
23                 ball.vy = (Math.random() * 2 - 1) * 10;
24                 balls.push(ball);
25             }
26             (function move() {
27                 oGc.clearRect(0, 0, width, height);
28                 balls.forEach(function (ball) {
29                     if (ball.x < -ball.radius
30                         || ball.x > width   ball.radius
31                         || ball.y < -ball.radius
32                         || ball.y > height   ball.radius) {
33                         ball.x = width / 2;
34                         ball.y = height / 2;
35                         ball.vx = (Math.random() * 2 - 1) * 5;
36                         ball.vy = (Math.random() * 2 - 1) * 10;
37                     }
38                     ball.x  = ball.vx;
39                     ball.y  = ball.vy;
40                     ball.vy  = gravity;
41                     ball.fill(oGc);
42                 });
43                 requestAnimationFrame(move);
44             })();
45         }
46     </script>
47 </head>
48 
49 <body>
50     <canvas id="canvas" width="1200" height="600"></canvas>
51 </body>

还可以通过控制小球的vx, vy,就是x方向和y方向的速度,来限制小球朝某一个方向发射,下面的例子,我们只让小球朝着x轴的右边发射

 1 <head>
 2     <meta charset='utf-8' />
 3     <style>
 4         #canvas {
 5             border: 1px dashed #aaa;
 6         }
 7     </style>
 8     <script src="./ball.js"></script>
 9     <script>
10         window.onload = function () {
11             var oCanvas = document.querySelector("#canvas"),
12                 oGc = oCanvas.getContext('2d'),
13                 width = oCanvas.width, height = oCanvas.height,
14                 balls = [], n = 50;
15             function getRandColor() {
16                 return '#'   ( function( color ){
17                     return ( color  = '0123456789abcdef' [Math.floor( Math.random() * 16 )] ) && ( color.length == 6 ) ? color : arguments.callee( color );
18                 } )( '' );
19             }
20             for( var i = 0; i < n; i   ) {
21                 var ball = new Ball( width / 2, height / 2, 20, getRandColor() );
22                 ball.vx = Math.abs( ( Math.random() * 2 - 1 ) * 5 );
23                 ball.vy = ( Math.random() * 2 - 1 ) * 5;
24                 balls.push( ball );
25             }
26             (function move(){
27                 oGc.clearRect( 0, 0, width, height );
28                 balls.forEach( function( ball ){
29                     if ( ball.x < -ball.radius
30                         || ball.x > width   ball.radius
31                         || ball.y < -ball.radius
32                         || ball.y > height   ball.radius ) {
33                             ball.x = width / 2;
34                             ball.y = height / 2;
35                             ball.vx = Math.abs( ( Math.random() * 2 - 1 ) * 5 );
36                             ball.vy = ( Math.random() * 2 - 1 ) * 5;
37                     }
38                     ball.x  = ball.vx;
39                     ball.y  = ball.vy;
40                     ball.fill( oGc );
41                 } );
42                 requestAnimationFrame( move );
43             })();
44         }
45     </script>
46 </head>
47 <body>
48     <canvas id="canvas" width="1200" height="600"></canvas>
49 </body>

 

这篇关于[js高手之路]html5 canvas动画教程 - 边界判断与小球粒子模拟喷泉,散弹效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1076907

相关文章

Nginx实现前端灰度发布

《Nginx实现前端灰度发布》灰度发布是一种重要的策略,它允许我们在不影响所有用户的情况下,逐步推出新功能或更新,通过灰度发布,我们可以测试新版本的稳定性和性能,下面就来介绍一下前端灰度发布的使用,感... 目录前言一、基于权重的流量分配二、基于 Cookie 的分流三、基于请求头的分流四、基于请求参数的分

基于Canvas的Html5多时区动态时钟实战代码

《基于Canvas的Html5多时区动态时钟实战代码》:本文主要介绍了如何使用Canvas在HTML5上实现一个多时区动态时钟的web展示,通过Canvas的API,可以绘制出6个不同城市的时钟,并且这些时钟可以动态转动,每个时钟上都会标注出对应的24小时制时间,详细内容请阅读本文,希望能对你有所帮助...

HTML5 data-*自定义数据属性的示例代码

《HTML5data-*自定义数据属性的示例代码》HTML5的自定义数据属性(data-*)提供了一种标准化的方法在HTML元素上存储额外信息,可以通过JavaScript访问、修改和在CSS中使用... 目录引言基本概念使用自定义数据属性1. 在 html 中定义2. 通过 JavaScript 访问3.

CSS模拟 html 的 title 属性(鼠标悬浮显示提示文字效果)

《CSS模拟html的title属性(鼠标悬浮显示提示文字效果)》:本文主要介绍了如何使用CSS模拟HTML的title属性,通过鼠标悬浮显示提示文字效果,通过设置`.tipBox`和`.tipBox.tipContent`的样式,实现了提示内容的隐藏和显示,详细内容请阅读本文,希望能对你有所帮助... 效

前端bug调试的方法技巧及常见错误

《前端bug调试的方法技巧及常见错误》:本文主要介绍编程中常见的报错和Bug,以及调试的重要性,调试的基本流程是通过缩小范围来定位问题,并给出了推测法、删除代码法、console调试和debugg... 目录调试基本流程调试方法排查bug的两大技巧如何看控制台报错前端常见错误取值调用报错资源引入错误解析错误

Vue中动态权限到按钮的完整实现方案详解

《Vue中动态权限到按钮的完整实现方案详解》这篇文章主要为大家详细介绍了Vue如何在现有方案的基础上加入对路由的增、删、改、查权限控制,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、数据库设计扩展1.1 修改路由表(routes)1.2 修改角色与路由权限表(role_routes)二、后端接口设计

C++实现回文串判断的两种高效方法

《C++实现回文串判断的两种高效方法》文章介绍了两种判断回文串的方法:解法一通过创建新字符串来处理,解法二在原字符串上直接筛选判断,两种方法都使用了双指针法,文中通过代码示例讲解的非常详细,需要的朋友... 目录一、问题描述示例二、解法一:将字母数字连接到新的 string思路代码实现代码解释复杂度分析三、

Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx

Vue ElementUI中Upload组件批量上传的实现代码

《VueElementUI中Upload组件批量上传的实现代码》ElementUI中Upload组件批量上传通过获取upload组件的DOM、文件、上传地址和数据,封装uploadFiles方法,使... ElementUI中Upload组件如何批量上传首先就是upload组件 <el-upl

前端知识点之Javascript选择输入框confirm用法

《前端知识点之Javascript选择输入框confirm用法》:本文主要介绍JavaScript中的confirm方法的基本用法、功能特点、注意事项及常见用途,文中通过代码介绍的非常详细,对大家... 目录1. 基本用法2. 功能特点①阻塞行为:confirm 对话框会阻塞脚本的执行,直到用户作出选择。②