JS中的Canvas动画
几天没写博客了,今天又忙到很晚,教大家做一个波浪花环吧
效果图如上所示:
老规矩先把代码给大家,新建一个html文档(新建一个txt文本文档,把后缀名改为“ .html
”),以记事本打开,把复制好的代码粘贴进去,“ 保存 ”,退出,双击或右键选择浏览器打开。
祝大家前端学习愉快,在今后的日子中与君共勉
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> body { background: #111; padding:0; margin:0; overflow:hidden; } </style> </head> <body> <div id="wrapper"></div> </body> <script> (function(){ 'use strict'; let wrapper, canvas, ctx, width, height, Tau=Math.PI*2, PI180=Math.PI/180, systems=[]; /* PlanetarySystem */ let PlanetarySystem = function(id='pSys'){ Object.defineProperty(this, 'id', { value:id, writable:true} ); Object.defineProperty(this, 'x', { value:0, writable:true }); Object.defineProperty(this, 'y', { value:0, writable:true }); Object.defineProperty(this, 'allBodies', { value:[], writable:true }); Object.defineProperty(this, 'allBodiesLookup', { value:{}, writable:true }); // fast id lookup for children Object.defineProperty(this, 'numBodies', { value:0, writable:true }); } PlanetarySystem.prototype.addBody = function(vo) { vo.parentSystem = this; vo.parentBody = vo.parentBody === null ? this : this.allBodiesLookup[vo.parentBody]; let body = new PlanetaryBody(vo); body.update(); this.allBodies.push(body); this.allBodiesLookup[vo.id] = body; this.numBodies += 1; } PlanetarySystem.prototype.setSpeedFactor = function(value){ let body; for(let i=0; i<this.numBodies; i++){ body = this.allBodies[i]; body.setSpeedFactor(value); } } PlanetarySystem.prototype.update = function(){ let body; for(let i=0; i<this.numBodies; i++){ body = this.allBodies[i]; body.update(); } } /* PlanetaryBody */ let PlanetaryBody = function(vo){ Object.defineProperty(this, 'id', { value:vo.id, writable:true} ); Object.defineProperty(this, 'diameter', { value:vo.diameter, writable:true }); Object.defineProperty(this, 'colour', { value:vo.colour, writable:true }); Object.defineProperty(this, 'x', { value:0, writable:true }); Object.defineProperty(this, 'y', { value:0, writable:true }); Object.defineProperty(this, 'vx', { value:0, writable:true }); Object.defineProperty(this, 'vy', { value:0, writable:true }); Object.defineProperty(this, 'degrees', { value:vo.degrees, writable:true }); Object.defineProperty(this, 'speedBase', { value:vo.speed, writable:true }); Object.defineProperty(this, 'speed', { value:vo.speed , writable:true }); Object.defineProperty(this, 'orbitalRadius', { value:vo.orbitalRadius, writable:true }); Object.defineProperty(this, 'parentSystem', { value:vo.parentSystem, writable:true }); Object.defineProperty(this, 'parentBody', { value:vo.parentBody, writable:true }); return this; } PlanetaryBody.prototype.update = function(){ let angle = this.degrees * PI180; this.degrees += this.speed; this.vx = this.orbitalRadius * Math.cos(angle); this.vy = this.orbitalRadius * Math.sin(angle); // update position if(this.parentBody != null){ this.x = this.vx + this.parentBody.x; this.y = this.vy + this.parentBody.y; } } /* init() */ function init(){ wrapper = document.querySelector('#wrapper'); canvas = createCanvas('canvas', width, height); wrapper.appendChild(canvas); ctx = canvas.getContext('2d'); setupEvents(); resizeCanvas(); /* Define new PlanetarySystem and set values */ let system1 = new PlanetarySystem('pSys1'); systems.push(system1); system1.x = width * .5; system1.y = height * .5; system1.addBody({id:'sun', diameter:5, degrees:0, speed:0, colour:'#FDFE1D', orbitalRadius:0, parentBody:null}); for(let loop=30, i=0; i<loop; i+=1){ system1.addBody({ id: 'ball'+i, diameter: 5, degrees: 0, speed: 2 + (loop * 0.15) - (i* 0.2), colour: '#FDFE1D', orbitalRadius: 7*(i+1), parentBody: 'sun'}); } } /* Methods */ function createCanvas(id, w, h){ let tCanvas = document.createElement('canvas'); tCanvas.width = w; tCanvas.height = h; tCanvas.id = id; return tCanvas; } function setupEvents(){ window.onresize = resizeCanvas; } function resizeCanvas(){ let rect = wrapper.getBoundingClientRect(); width = window.innerWidth; height = window.innerHeight - rect.top -2; canvas.width = width; canvas.height = height; for(let i=0; i<systems.length; i++){ systems[i].x = width * .5; systems[i].y = height * .5; } } function update(){ for(let loop=systems.length, i=0; i<loop; i++){ systems[i].update(); } } function draw(){ let system; let prev = null; for(let i=0; i<systems.length; i++){ system = systems[i]; let planetaryBody; for(let loop=system.numBodies, j=1; j<loop; j+=1) { planetaryBody = system.allBodies[j]; ctx.beginPath(); ctx.arc(planetaryBody.x, planetaryBody.y, planetaryBody.diameter, 0, Tau, false); ctx.fillStyle = planetaryBody.colour; ctx.fill(); if(j>1){ ctx.strokeStyle = planetaryBody.colour; ctx.lineWidth = 2; ctx.beginPath(); ctx.moveTo(planetaryBody.x, planetaryBody.y); ctx.lineTo(prev.x, prev.y); ctx.stroke(); } prev = {x:planetaryBody.x, y:planetaryBody.y}; } } } function animate(){ ctx.fillStyle = 'rgba(0,0,0, .05)'; ctx.fillRect(0, 0, width, height); update(); draw(); requestAnimationFrame(animate); } init(); animate(); }()); </script> </html>
标签:
Canvas,波浪花环,花环
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“Canvas波浪花环的示例代码”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新动态
2024年11月23日
2024年11月23日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]