小球的开头地点是在canvas的正中间,小球的始发地点是在canvas的正中间

备考:本文后边的代码,假诺加载了ball.js,那么请使用那篇文章[js高手之路]
html伍 canvas动画教程 –
匀速运动
的ball.js代码.

备考:本文前边的代码,假若加载了ball.js,那么请使用那篇小说[js高手之路]
html五 canvas动画教程 –
匀速运动
的ball.js代码.

本文,咱们要做点有意思的法力,首先,来3个简约的边际判断,所谓边界判断:正是把物体的活动限定在一个范围内.大家先来三个简易的实例,在canvas上生成1个小球,小球的起来地方是在canvas的正中间,然后经过键盘的内外左右来移动小球的职位,要是小球境遇canvas的右边,那么不可能再向左运动,其余方向同理.

正文,大家要做点有意思的法力,首先,来一个大致的分界判断,所谓边界判断:正是把物体的位移限定在2个范围内.大家先来2个简单易行的实例,在canvas上生成3个小球,小球的开始地方是在canvas的正中间,然后经过键盘的上下左右来移动小球的职位,要是小球遭逢canvas的左手,那么无法再向左运动,别的可行性同理.

要落到实处这几个效应,只需求多个边界判断,即可。

要贯彻那几个职能,只需求2个边际判断,即可。

 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 }
 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
如若低于小球的半径,那肯定是遇上了左边界,大家就让小球的宗旨x等于小球的半径。

右侧界:只要判断小球的圆心x
假如超过canvas的急剧减去小球的半径,那必然是遇到了左边界,大家就让小球的焦点x等于canvas的宽窄减去小球的半径

左侧界:只要判断小球的圆心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 = 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>
 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的着力点,不停的向左近发射小球,形成散弹的效劳. 

因此canvas的主干点,不停的向左近发射小球,形成散弹的效益. 

本人不晓得你们有未有诸如此类的误区:不停的向四周发射小球,那是还是不是要不停的创造小球呢?假如您这么想,程序就算写出来了,肯定会卡死.

小编不知晓你们有未有这么的误区:不停的向四周发射小球,那是还是不是要不停的创导小球呢?借使你如此想,程序就算写出来了,肯定会卡死.

实在大家能够只创设,一定数额的小球,比如(
50, 60. …十0
),然后当那么些小球,完全超乎的边界的时候,再把那几个小球的圆心( x, y
)设定到最伊始的岗位,再度随机x和y方向的速度,就足以实现目标了,
说白了正是,那些完全超越边界的小球,大家让他重复赶回最初的地点,只是改变了她的颜料和速度,给人感觉到正是丰盛发射小球的地方接踵而来的在发射小球

实际我们得以只开创,一定数额的小球,比如(
50, 60. …100
),然后当那些小球,完全超出的分界的时候,再把那个小球的圆心( x, y
)设定到最起始的职位,再一次随机x和y方向的进度,就足以直达目标了,
说白了正是,那一个完全超越边界的小球,大家让她再一次归来最初的地方,只是改变了她的颜料和速度,给人感到正是格外发射小球的地方接踵而来的在发出小球

图片 1

图片 2

全部的散弹效果:

总体的散弹效果:

 

 

 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;
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>

我们得以在前边的根底上,加上海重机厂力的熏陶,完结喷泉的效能

我们得以在以前的底蕴上,加上海重机厂力的震慑,达成喷泉的效能

图片 3

图片 4

那张图,望着是还是不是更像喷泉?

那张图,瞧着是或不是更像喷泉?

图片 5

图片 6

 

 

 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>
 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轴的入手发射

还能透过决定小球的vx,
vy,正是x方向和y方向的快慢,来界定小球朝某3个样子发射,上面包车型地铁例证,大家只让小球朝着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>
 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>

 图片 7

 图片 8

相关文章