• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    Canvas波浪花环的示例代码

    发布者: Error | 发布时间: 2025-6-16 12:34| 查看数: 129| 评论数: 0|帖子模式

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





    效果图如上所示:
    老规矩先把代码给大家,新建一个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>
    复制代码
    到此这篇关于Canvas波浪花环的示例代码的文章就介绍到这了,更多相关Canvas 波浪花环内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!

    来源:https://www.jb51.net/html5/741786.html
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

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