html海洋动态效果图,HTML5/SVG海洋波浪动画运动模拟

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

/**

* Constants

*/

const TWO_PI = Math.PI * 2;

/**

* Application Class

*/

class Application {

/**

* Application constructor

*/

constructor() {

this.canvas = document.getElementById("canvas");

this.context = this.canvas.getContext("2d");

this.width = this.canvas.width = window.innerWidth;

this.height = this.canvas.height = window.innerHeight;

this.center = {

x: this.width / 2,

y: this.height / 2

};

this.circleContainers = [];

//Resize listener for the canvas to fill browser window dynamically

window.addEventListener('resize', () => this.resizeCanvas(), false);

}

/**

* Simple resize function. Reinitializes everything on the canvas while changing the width/height

*/

resizeCanvas() {

this.width = this.canvas.width = window.innerWidth;

this.height = this.canvas.height = window.innerHeight;

this.center = {

x: this.width / 2,

y: this.height / 2

};

//Empty the previous container and fill it again with new CircleContainer objects

this.circleContainers = [];

this.initializeCircleContainers();

}

/**

* Create a number of CircleContainer objects based on the numberOfContainers variable

* @return void

*/

initializeCircleContainers() {

for (let x = 0; x < this.width + 100; x += 100) {

for (let y = 0; y < this.height + 100; y += 100) {

//Initialize a new instance of the CircleContainer class

let circleContainer = new CircleContainer(this.context, x, y);

//Let the CircleContainer initialize it's children

circleContainer.initializeCircles();

//Add the container to our array of CircleContainer objects

this.circleContainers.push(circleContainer);

}

}

}

/**

* Updates the application and every child of the application

* @return void

*/

update() {

for (let i = 0; i < this.circleContainers.length; i++) {

this.circleContainers[i].update();

}

}

/**

* Renders the application and every child of the application

* @return void

*/

render() {

//Clear the entire canvas every render

this.context.clearRect(0, 0, this.width, this.height);

//Trigger the render function on every child element

for (let i = 0; i < this.circleContainers.length; i++) {

this.circleContainers[i].render();

}

}

/**

* Update and render the application at least 60 times a second

* @return void

*/

loop() {

this.update();

this.render();

window.requestAnimationFrame(() => this.loop());

}

}

/**

* CircleContainer Class

*/

class CircleContainer {

/**

* CircleContainer constructor

* @param context - The context from the canvas object of the Application

* @param x

* @param y

*/

constructor(context, x, y) {

this.context = context;

this.position = {x, y};

this.numberOfCircles = 19;

this.circles = [];

this.baseRadius = 20;

this.bounceRadius = 150;

this.singleSlice = TWO_PI / this.numberOfCircles;

}

/**

* Create a number of Circle objects based on the numberOfCircles variable

* @return void

*/

initializeCircles() {

for (let i = 0; i < this.numberOfCircles; i++) {

this.circles.push(new Circle(this.position.x, this.position.y + Math.random(), this.baseRadius, this.bounceRadius, i * this.singleSlice));

}

}

/**

* Try to update the application at least 60 times a second

* @return void

*/

update() {

for (let i = 0; i < this.numberOfCircles; i++) {

this.circles[i].update(this.context);

}

}

/**

* Try to render the application at least 60 times a second

* @return void

*/

render() {

for (let i = 0; i < this.numberOfCircles; i++) {

this.circles[i].render(this.context);

}

}

}

/**

* Circle Class

*/

class Circle {

/**

* Circle constructor

* @param x - The horizontal position of this circle

* @param y - The vertical position of this circle

* @param baseRadius

* @param bounceRadius

* @param angleCircle

*/

constructor(x, y, baseRadius, bounceRadius, angleCircle) {

this.basePosition = {x, y};

this.position = {x, y};

this.speed = 0.01;

this.baseSize = 10;

this.size = 10;

this.angle = (x + y);

this.baseRadius = baseRadius;

this.bounceRadius = bounceRadius;

this.angleCircle = angleCircle;

}

/**

* Update the position of this object

* @return void

*/

update() {

this.position.x = this.basePosition.x + Math.cos(this.angleCircle) * (Math.sin(this.angle + this.angleCircle) * this.bounceRadius + this.baseRadius);

this.position.y = this.basePosition.y + Math.sin(this.angleCircle) * (Math.sin(this.angle + this.angleCircle) * this.bounceRadius + this.baseRadius);

this.size = Math.cos(this.angle) * 8 + this.baseSize;

this.angle += this.speed;

}

/**

* Renders this Circle object on the canvas

* @param context - The context from the canvas object of the Application

* @return void

*/

render(context) {

context.fillStyle = "hsl(195, 100%, "+this.size * 4+"%)";

context.beginPath();

context.arc(this.position.x, this.position.y, this.size, 0, TWO_PI);

context.fill();

}

}

/**

* Onload function is executed whenever the page is done loading, initializes the application

*/

window.onload = function () {

//Create a new instance of the application

const application = new Application();

//Initialize the CircleContainer objects

application.initializeCircleContainers();

//Start the initial loop function for the first time

application.loop();

};

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HTML5中的SVG可以用来创建各种炫酷的动画效果,包括多条波浪线条动画。下面是一个简单的示例代码,用于创建两条波浪线条动画: ```HTML <svg width="800" height="400"> <path id="wave1" fill="none" stroke="#00BFFF" stroke-width="5" d="M0 150 q30 30 60 0 t60 0 t60 0 t60 0 t60 0 t60 0 t60 0 v250 h-360 z"> <animateTransform attributeName="transform" attributeType="XML" type="translate" dur="10s" values="0; -400" repeatCount="indefinite" /> </path> <path id="wave2" fill="none" stroke="#00BFFF" stroke-width="5" d="M0 250 q30 -30 60 0 t60 0 t60 0 t60 0 t60 0 t60 0 t60 0 v150 h-360 z"> <animateTransform attributeName="transform" attributeType="XML" type="translate" dur="10s" values="0; -400" repeatCount="indefinite" /> </path> </svg> ``` 在这个例子中,我们创建了两个波浪线条,分别称为“wave1”和“wave2”。这些路径有一个“d”属性,用于定义路径的形状。这里我们使用了SVG的曲线命令“q”来创建波浪线条。我们还使用了SVG的路径命令“v”和“h”来定义线条的垂直和水平线段。 接下来,我们使用SVG动画功能,通过使用“animateTransform”元素来创建平移动画。我们将波浪线条沿着Y轴向下移动,并且在10秒钟内完成动画。我们还使用“repeatCount”属性使动画无限循环。 最后,我们将两个波浪线条添加到SVG元素中,并设置宽度和高度以适应我们的动画。我们还可以使用CSS样式来进一步自定义这些元素的外观。 这只是一个简单的示例,但你可以使用SVG的各种命令和动画功能来创建更复杂和炫酷的波浪线条动画

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值