Canvas中如何绘制波浪花环,步骤方法是怎样
Admin 2022-07-01 群英技术资讯 849 次浏览
本篇内容介绍了“Canvas中如何绘制波浪花环,步骤方法是怎样”的有关知识,在实际项目的操作过程或是学习过程中,不少人都会遇到这样的问题,接下来就让小编带大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!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>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
利用CSS3如何做放大旋转动画?我们知道CSS3中animation属性能实现动画效果,这篇就给大家来讲讲,实现简单的旋转并且放大效果的方法,对大家学习和理解animation属性的使用会有帮助,文中示例介绍的很详细,感兴趣的朋友可以了解看看,下面让我们一起来学习一下吧!
这篇文章主要介绍了uni-app项目瀑布流布局的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
这篇文章主要介绍了AmazeUI图片轮播效果的示例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
这篇文章主要介绍了CSS解决inline-block的错位问题,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
在网上看到篇文章讲全球很多高手的CSS全局样式(reset.css),再一次体会到了什么叫“细节决定成败”。原文:http://perishablepress.com/press/2007/10/23/a-killer-collection-of-global-css-reset-styles/。原文是借着翻译插件看下来的,大概意思就是讲什么是Css,以及各浏览器的css规则的不同,而制定"Cs
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008