网页技术交流
 
发新帖
楼主: 零五零八
查看: 522|回复: 0

[使用教程] Canvas波浪花环的示例代码

[复制链接]
零五零八 发表于 2020-11-10 11:17:04 | 显示全部楼层
本帖最后由 零五零八 于 2020-11-10 11:20 编辑

JS中的Canvas动画


几天没写博客了,今天又忙到很晚,教大家做一个波浪花环吧


1.png

2.png


3.png


效果图如上所示:


  老规矩先把代码给大家,新建一个html文档(新建一个txt文本文档,把后缀名改为“ .html”),以记事本打开,把复制好的代码粘贴进去,“ 保存 ”,退出,双击或右键选择浏览器打开。祝大家前端学习愉快,在今后的日子中与君共勉


  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Document</title>
  6.     <style>
  7.         body {
  8.             background: #111;
  9.             padding:0;
  10.             margin:0;
  11.             overflow:hidden;
  12.         }
  13.     </style>
  14. </head>
  15. <body>
  16.     <div id="wrapper"></div>
  17. </body>
  18. <script>
  19. (function(){
  20.     'use strict';
  21.     let wrapper, canvas, ctx, width, height,
  22.     Tau=Math.PI*2, PI180=Math.PI/180,
  23.     systems=[];

  24. /* PlanetarySystem */
  25.     let PlanetarySystem = function(id='pSys'){
  26.         Object.defineProperty(this, 'id',               { value:id, writable:true} );
  27.         Object.defineProperty(this, 'x',                { value:0, writable:true });
  28.         Object.defineProperty(this, 'y',                { value:0, writable:true });
  29.         Object.defineProperty(this, 'allBodies',        { value:[], writable:true });
  30.         Object.defineProperty(this, 'allBodiesLookup',  { value:{}, writable:true });    // fast id lookup for children
  31.         Object.defineProperty(this, 'numBodies',        { value:0, writable:true });
  32.     }
  33.     PlanetarySystem.prototype.addBody = function(vo) {
  34.         vo.parentSystem = this;
  35.         vo.parentBody = vo.parentBody === null ? this : this.allBodiesLookup[vo.parentBody];
  36.         let body = new PlanetaryBody(vo);
  37.         body.update();
  38.         this.allBodies.push(body);
  39.         this.allBodiesLookup[vo.id] = body;
  40.         this.numBodies += 1;
  41.     }
  42.     PlanetarySystem.prototype.setSpeedFactor = function(value){
  43.         let body;
  44.         for(let i=0; i<this.numBodies; i++){
  45.             body = this.allBodies[i];
  46.             body.setSpeedFactor(value);
  47.         }
  48.     }
  49.     PlanetarySystem.prototype.update = function(){
  50.         let body;
  51.         for(let i=0; i<this.numBodies; i++){
  52.             body = this.allBodies[i];
  53.             body.update();
  54.         }
  55.     }
  56. /* PlanetaryBody */
  57.     let PlanetaryBody = function(vo){
  58.         Object.defineProperty(this, 'id',                   { value:vo.id, writable:true} );
  59.         Object.defineProperty(this, 'diameter',             { value:vo.diameter, writable:true });
  60.         Object.defineProperty(this, 'colour',               { value:vo.colour, writable:true });
  61.         Object.defineProperty(this, 'x',                    { value:0, writable:true });
  62.         Object.defineProperty(this, 'y',                    { value:0, writable:true });
  63.         Object.defineProperty(this, 'vx',                   { value:0, writable:true });
  64.         Object.defineProperty(this, 'vy',                   { value:0, writable:true });
  65.         Object.defineProperty(this, 'degrees',              { value:vo.degrees, writable:true });
  66.         Object.defineProperty(this, 'speedBase',            { value:vo.speed, writable:true });
  67.         Object.defineProperty(this, 'speed',                { value:vo.speed , writable:true });
  68.         Object.defineProperty(this, 'orbitalRadius',        { value:vo.orbitalRadius, writable:true });
  69.         Object.defineProperty(this, 'parentSystem',         { value:vo.parentSystem, writable:true });
  70.         Object.defineProperty(this, 'parentBody',           { value:vo.parentBody, writable:true });

  71.         return this;
  72.     }
  73.     PlanetaryBody.prototype.update = function(){
  74.         let angle = this.degrees * PI180;
  75.         this.degrees += this.speed;
  76.         this.vx = this.orbitalRadius * Math.cos(angle);
  77.         this.vy = this.orbitalRadius * Math.sin(angle);
  78.         // update position
  79.         if(this.parentBody != null){
  80.             this.x = this.vx + this.parentBody.x;
  81.             this.y = this.vy + this.parentBody.y;
  82.         }
  83.     }

  84. /* init() */
  85.     function init(){
  86.         wrapper = document.querySelector('#wrapper');
  87.         canvas = createCanvas('canvas', width, height);
  88.         wrapper.appendChild(canvas);
  89.         ctx = canvas.getContext('2d');
  90.         setupEvents();
  91.         resizeCanvas();

  92.         /* Define new PlanetarySystem and set values */
  93.         let system1 = new PlanetarySystem('pSys1');
  94.         systems.push(system1);
  95.         system1.x = width * .5;
  96.         system1.y = height * .5;
  97.         system1.addBody({id:'sun', diameter:5, degrees:0, speed:0, colour:'#FDFE1D', orbitalRadius:0, parentBody:null});
  98.         for(let loop=30, i=0; i<loop; i+=1){
  99.             system1.addBody({   id:             'ball'+i,
  100.                                 diameter:       5,
  101.                                 degrees:        0,
  102.                                 speed:          2 + (loop * 0.15) - (i* 0.2),
  103.                                 colour:         '#FDFE1D',
  104.                                 orbitalRadius:  7*(i+1),
  105.                                 parentBody:     'sun'});
  106.         }
  107.     }
  108.      
  109. /* Methods */
  110.     function createCanvas(id, w, h){
  111.         let tCanvas = document.createElement('canvas');
  112.         tCanvas.width = w;
  113.         tCanvas.height = h;
  114.         tCanvas.id = id;
  115.         return tCanvas;
  116.     }

  117.     function setupEvents(){
  118.         window.onresize = resizeCanvas;
  119.     }
  120.     function resizeCanvas(){
  121.         let rect = wrapper.getBoundingClientRect();
  122.         width = window.innerWidth;
  123.         height = window.innerHeight - rect.top -2;
  124.         canvas.width = width;
  125.         canvas.height = height;
  126.         for(let i=0; i<systems.length; i++){
  127.             systems[i].x = width * .5;
  128.             systems[i].y = height * .5;
  129.         }
  130.     }

  131.     function update(){
  132.         for(let loop=systems.length, i=0; i<loop; i++){
  133.             systems[i].update();
  134.         }
  135.     }

  136.     function draw(){
  137.         let system;
  138.         let prev = null;
  139.         for(let i=0; i<systems.length; i++){
  140.             system = systems[i];
  141.             let planetaryBody;
  142.             for(let loop=system.numBodies, j=1; j<loop; j+=1) {
  143.                 planetaryBody = system.allBodies[j];
  144.                 ctx.beginPath();
  145.                 ctx.arc(planetaryBody.x, planetaryBody.y, planetaryBody.diameter, 0, Tau, false);
  146.                 ctx.fillStyle = planetaryBody.colour;
  147.                 ctx.fill();
  148.                 if(j>1){
  149.                     ctx.strokeStyle = planetaryBody.colour;
  150.                     ctx.lineWidth = 2;
  151.                     ctx.beginPath();
  152.                     ctx.moveTo(planetaryBody.x, planetaryBody.y);
  153.                     ctx.lineTo(prev.x, prev.y);
  154.                     ctx.stroke();
  155.                 }
  156.                 prev = {x:planetaryBody.x, y:planetaryBody.y};
  157.             }
  158.         }
  159.     }

  160.     function animate(){
  161.         ctx.fillStyle = 'rgba(0,0,0, .05)';
  162.         ctx.fillRect(0, 0, width, height);
  163.         update();
  164.         draw();
  165.         requestAnimationFrame(animate);
  166.     }
  167.     init();
  168.     animate();
  169. }());
  170. </script>
  171. </html>
复制代码





快速回复 返回顶部 返回列表