h5动画怎么循环_HTML5动画–带有循环的模式

本文介绍了一种在HTML5中创建可循环动画的方法,通过使用Canvas元素和JavaScript,可以实现太阳光效和行驶车辆的动画效果,展示了如何利用循环模式进行动画设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

h5动画怎么循环

HTML5 animation - patterns with loops
HTML5 animation - patterns with loops

HTML5 animation – patterns with loops. I have prepared new HTML5 tutorial for you. This is not quite regular ‘html5 game development’ tutorial, but I like to show you another demonstration of html5 canvas animation. We will create patterns with loops and animate it. Today I have prepared nice demo with the sun and with three cars.

HTML5动画–带有循环的模式。 我已经为您准备了新HTML5教程。 这不是很常规的“ html5游戏开发”教程,但是我想向您展示html5 canvas动画的另一个演示。 我们将使用循环创建模式并对其进行动画处理。 今天,我准备了太阳和三辆汽车的精彩演示。

Here are our demo and downloadable package:

这是我们的演示和可下载的软件包:

现场演示

[sociallocker]

[社交储物柜]

打包下载

[/sociallocker]

[/ sociallocker]

Ok, download the source files and lets start coding !

好的,下载源文件并开始编码!

步骤1. HTML (Step 1. HTML)

Small code with canvas element (where we are going to experiment)

带画布元素的小代码(我们将在此处进行实验)

index.html (index.html)

<!DOCTYPE html>
<html lang="en" >
    <head>
        <meta charset="utf-8" />
        <title>HTML5 animation - patterns with loops | Script Tutorials</title>
        <link href="css/main.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="js/script.js"></script>
    </head>
    <body>
        <header>
            <h2>HTML5 animation - patterns with loops</h2>
            <a href="https://www.script-tutorials.com/html5-animation-patterns-with-loops/" class="stuts">Back to original tutorial on <span>Script Tutorials</span></a>
        </header>
        <div class="container">
            <div class="contr">
                <input type="radio" name="mode" onchange="bPlay = false" /><label>Pause</label>
                <input type="radio" name="mode" onchange="bPlay = true" checked /><label>Play</label>
            </div>
            <canvas id="panel" width="800" height="600"></canvas>
        </div>
    </body>
</html>

<!DOCTYPE html>
<html lang="en" >
    <head>
        <meta charset="utf-8" />
        <title>HTML5 animation - patterns with loops | Script Tutorials</title>
        <link href="css/main.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="js/script.js"></script>
    </head>
    <body>
        <header>
            <h2>HTML5 animation - patterns with loops</h2>
            <a href="https://www.script-tutorials.com/html5-animation-patterns-with-loops/" class="stuts">Back to original tutorial on <span>Script Tutorials</span></a>
        </header>
        <div class="container">
            <div class="contr">
                <input type="radio" name="mode" onchange="bPlay = false" /><label>Pause</label>
                <input type="radio" name="mode" onchange="bPlay = true" checked /><label>Play</label>
            </div>
            <canvas id="panel" width="800" height="600"></canvas>
        </div>
    </body>
</html>

步骤2. CSS (Step 2. CSS)

css / main.css (css/main.css)

We are going to skip of publishing styles today again (no need).

今天我们将再次跳过发布样式(不需要)。

步骤3. JS (Step 3. JS)

js / script.js (js/script.js)

// variables
var canvas, ctx;
var bPlay = true;
var theSun;
var theCar1, theCar2, theCar3;
// define Sun constructor
function Sun(iCenterX, iCenterY, iRad, iRaysCnt, sRayColor, sSunColor){
    this.iCenterX = iCenterX;
    this.iCenterY = iCenterY;
    this.iRad = iRad;
    this.iRaysCnt = iRaysCnt;
    this.sRayColor = sRayColor;
    this.sSunColor = sSunColor;
    this.iExAngle = 0;
    this.bDir = true;
}
// Define Sun draw method
Sun.prototype.draw = function(){
    // change angle
    if (this.bDir) {
        this.iExAngle += 0.05;
    } else {
        this.iExAngle -= 0.05;
    }
    if (this.iExAngle > 3 || this.iExAngle < -3) {
        this.bDir = ! this.bDir;
    }
    // draw rays
    ctx.beginPath();
    for (var n = 0; n < this.iRaysCnt; n++) {
        var iAng1 = ((Math.PI * 2) / this.iRaysCnt) * (n + 1);
        var iAng2 = ((Math.PI * 2) / this.iRaysCnt) * (n);
        var iAng3 = ((Math.PI * 2) / this.iRaysCnt) * (n+0.5);
        var x1 = (this.iRad * Math.sin(iAng1 + this.iExAngle)) + this.iCenterX;
        var y1 = (this.iRad * Math.cos(iAng1 + this.iExAngle)) + this.iCenterY;
        var x2 = (this.iRad * Math.sin(iAng2 + this.iExAngle)) + this.iCenterX;
        var y2 = (this.iRad * Math.cos(iAng2 + this.iExAngle)) + this.iCenterY;
        var x3 = (2 * this.iRad * Math.sin(iAng3 + this.iExAngle)) + this.iCenterX;
        var y3 = (2 * this.iRad * Math.cos(iAng3 + this.iExAngle)) + this.iCenterY;
        ctx.moveTo(x1, y1);
        ctx.lineTo(x3, y3);
        ctx.lineTo(x2, y2);
    }
    ctx.closePath();
    // fill rays with color
    ctx.fillStyle = this.sRayColor;
    ctx.fill();
    // draw sun center
    ctx.beginPath();
    ctx.arc(this.iCenterX, this.iCenterY, this.iRad, 0, 2 * Math.PI, false);
    ctx.fillStyle = this.sSunColor;
    ctx.fill();
};
// define CarWheel constructor
function CarWheel(iCenterX, iCenterY, iRad, iCnt, iSpeed, sColor1, sColor2, sColor3){
    this.iCenterX = iCenterX;
    this.iCenterY = iCenterY;
    this.iRad = iRad;
    this.iCnt = iCnt;
    this.iSpeed = iSpeed;
    this.sColor1 = sColor1;
    this.sColor2 = sColor2;
    this.sColor3 = sColor3;
    this.iAngle = 0;
}
// Define CarWheel draw method
CarWheel.prototype.draw = function(){
    this.iAngle += 0.1 * this.iSpeed / 2;
    this.iCenterX += this.iSpeed;
    // draw wheel details
    ctx.beginPath();
    for (var n = 0; n < this.iCnt; n++) {
        var theta1 = ((Math.PI * 2) / this.iCnt) * (n + 1);
        var theta2 = ((Math.PI * 2) / this.iCnt) * (n);
        var x1 = (this.iRad * Math.sin(theta1-this.iAngle)) + this.iCenterX;
        var y1 = (this.iRad * Math.cos(theta1-this.iAngle)) + this.iCenterY;
        var x2 = (this.iRad * Math.sin(theta2-this.iAngle)) + this.iCenterX;
        var y2 = (this.iRad * Math.cos(theta2-this.iAngle)) + this.iCenterY;
        ctx.moveTo(this.iCenterX, this.iCenterY);
        ctx.bezierCurveTo(x1, y1, x2, y2, this.iCenterX, this.iCenterY);
    }
    ctx.closePath();
    // fill
    ctx.fillStyle = this.sColor2;
    ctx.fill();
    // stroke
    ctx.lineWidth = 1;
    ctx.strokeStyle = this.sColor1;
    ctx.stroke();
    // draw center
    ctx.beginPath();
    ctx.arc(this.iCenterX, this.iCenterY, this.iRad/2.5, 0, 2 * Math.PI, false);
    ctx.closePath();
    ctx.fillStyle = this.sColor3;
    ctx.fill();
};
// define Car constructor
function Car(iPosX, iPosY, iSpeed, iWidth, iHeight){
    this.iPosX = iPosX;
    this.iPosY = iPosY;
    this.iSpeed = iSpeed;
    this.iWidth = iWidth;
    this.iHeight = iHeight;
    this.theWheel1 = new CarWheel(this.iPosX + 41, this.iPosY + 52, 30, 8, this.iSpeed, 'black', 'brown', '#5e5e5e');
    this.theWheel2 = new CarWheel(this.iPosX + 191, this.iPosY + 52, 30, 8, this.iSpeed, 'black', 'brown', '#5e5e5e');
    // load image for car
    this.imageObj = new Image();
    this.imageObj.onload = function(){};
    this.imageObj.src = 'images/car.png';
}
// Define Car draw method
Car.prototype.draw = function(){
    this.iPosX += this.iSpeed;
    if (this.iPosX > ctx.canvas.width /*+ this.iWidth*/) {
        this.iPosX = -this.iWidth;
        this.theWheel1.iCenterX = this.iPosX + 41;
        this.theWheel2.iCenterX = this.iPosX + 191;
        this.theWheel1.iAngle = 0;
        this.theWheel2.iAngle = 0;
    }
    // draw car image
    ctx.drawImage(this.imageObj, this.iPosX, this.iPosY, this.iWidth, this.iHeight);
    // and animated wheels
    this.theWheel1.draw();
    this.theWheel2.draw();
};
// drawing functions
function clear() { // clear canvas function
    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
}
function drawScene() { // main drawScene function
    if (bPlay == 1) {
        clear(); // clear canvas
        // draw animated objects
        theSun.draw();
        theCar1.draw();
        theCar2.draw();
        theCar3.draw();
    }
}
function getRand(x, y) {
    return Math.floor(Math.random()*y)+x;
}
// binding onload event
if (window.attachEvent) {
    window.attachEvent('onload', main_init);
} else {
    if(window.onload) {
        var curronload = window.onload;
        var newonload = function() {
            curronload();
            main_init();
        };
        window.onload = newonload;
    } else {
        window.onload = main_init;
    }
}
function main_init() {
    // create canvas and context objects
    canvas = document.getElementById('panel');
    ctx = canvas.getContext('2d');
    // initialization of animated objects
    theSun = new Sun(725, 100, 25, 10, 'red', 'yellow');
    theCar1 = new Car(0, 300, 4, 226, 66);
    theCar2 = new Car(100, 400, 3, 226, 66);
    theCar3 = new Car(200, 500, 2, 226, 66);
    setInterval(drawScene, 40); // loop drawScene
}

// variables
var canvas, ctx;
var bPlay = true;
var theSun;
var theCar1, theCar2, theCar3;
// define Sun constructor
function Sun(iCenterX, iCenterY, iRad, iRaysCnt, sRayColor, sSunColor){
    this.iCenterX = iCenterX;
    this.iCenterY = iCenterY;
    this.iRad = iRad;
    this.iRaysCnt = iRaysCnt;
    this.sRayColor = sRayColor;
    this.sSunColor = sSunColor;
    this.iExAngle = 0;
    this.bDir = true;
}
// Define Sun draw method
Sun.prototype.draw = function(){
    // change angle
    if (this.bDir) {
        this.iExAngle += 0.05;
    } else {
        this.iExAngle -= 0.05;
    }
    if (this.iExAngle > 3 || this.iExAngle < -3) {
        this.bDir = ! this.bDir;
    }
    // draw rays
    ctx.beginPath();
    for (var n = 0; n < this.iRaysCnt; n++) {
        var iAng1 = ((Math.PI * 2) / this.iRaysCnt) * (n + 1);
        var iAng2 = ((Math.PI * 2) / this.iRaysCnt) * (n);
        var iAng3 = ((Math.PI * 2) / this.iRaysCnt) * (n+0.5);
        var x1 = (this.iRad * Math.sin(iAng1 + this.iExAngle)) + this.iCenterX;
        var y1 = (this.iRad * Math.cos(iAng1 + this.iExAngle)) + this.iCenterY;
        var x2 = (this.iRad * Math.sin(iAng2 + this.iExAngle)) + this.iCenterX;
        var y2 = (this.iRad * Math.cos(iAng2 + this.iExAngle)) + this.iCenterY;
        var x3 = (2 * this.iRad * Math.sin(iAng3 + this.iExAngle)) + this.iCenterX;
        var y3 = (2 * this.iRad * Math.cos(iAng3 + this.iExAngle)) + this.iCenterY;
        ctx.moveTo(x1, y1);
        ctx.lineTo(x3, y3);
        ctx.lineTo(x2, y2);
    }
    ctx.closePath();
    // fill rays with color
    ctx.fillStyle = this.sRayColor;
    ctx.fill();
    // draw sun center
    ctx.beginPath();
    ctx.arc(this.iCenterX, this.iCenterY, this.iRad, 0, 2 * Math.PI, false);
    ctx.fillStyle = this.sSunColor;
    ctx.fill();
};
// define CarWheel constructor
function CarWheel(iCenterX, iCenterY, iRad, iCnt, iSpeed, sColor1, sColor2, sColor3){
    this.iCenterX = iCenterX;
    this.iCenterY = iCenterY;
    this.iRad = iRad;
    this.iCnt = iCnt;
    this.iSpeed = iSpeed;
    this.sColor1 = sColor1;
    this.sColor2 = sColor2;
    this.sColor3 = sColor3;
    this.iAngle = 0;
}
// Define CarWheel draw method
CarWheel.prototype.draw = function(){
    this.iAngle += 0.1 * this.iSpeed / 2;
    this.iCenterX += this.iSpeed;
    // draw wheel details
    ctx.beginPath();
    for (var n = 0; n < this.iCnt; n++) {
        var theta1 = ((Math.PI * 2) / this.iCnt) * (n + 1);
        var theta2 = ((Math.PI * 2) / this.iCnt) * (n);
        var x1 = (this.iRad * Math.sin(theta1-this.iAngle)) + this.iCenterX;
        var y1 = (this.iRad * Math.cos(theta1-this.iAngle)) + this.iCenterY;
        var x2 = (this.iRad * Math.sin(theta2-this.iAngle)) + this.iCenterX;
        var y2 = (this.iRad * Math.cos(theta2-this.iAngle)) + this.iCenterY;
        ctx.moveTo(this.iCenterX, this.iCenterY);
        ctx.bezierCurveTo(x1, y1, x2, y2, this.iCenterX, this.iCenterY);
    }
    ctx.closePath();
    // fill
    ctx.fillStyle = this.sColor2;
    ctx.fill();
    // stroke
    ctx.lineWidth = 1;
    ctx.strokeStyle = this.sColor1;
    ctx.stroke();
    // draw center
    ctx.beginPath();
    ctx.arc(this.iCenterX, this.iCenterY, this.iRad/2.5, 0, 2 * Math.PI, false);
    ctx.closePath();
    ctx.fillStyle = this.sColor3;
    ctx.fill();
};
// define Car constructor
function Car(iPosX, iPosY, iSpeed, iWidth, iHeight){
    this.iPosX = iPosX;
    this.iPosY = iPosY;
    this.iSpeed = iSpeed;
    this.iWidth = iWidth;
    this.iHeight = iHeight;
    this.theWheel1 = new CarWheel(this.iPosX + 41, this.iPosY + 52, 30, 8, this.iSpeed, 'black', 'brown', '#5e5e5e');
    this.theWheel2 = new CarWheel(this.iPosX + 191, this.iPosY + 52, 30, 8, this.iSpeed, 'black', 'brown', '#5e5e5e');
    // load image for car
    this.imageObj = new Image();
    this.imageObj.onload = function(){};
    this.imageObj.src = 'images/car.png';
}
// Define Car draw method
Car.prototype.draw = function(){
    this.iPosX += this.iSpeed;
    if (this.iPosX > ctx.canvas.width /*+ this.iWidth*/) {
        this.iPosX = -this.iWidth;
        this.theWheel1.iCenterX = this.iPosX + 41;
        this.theWheel2.iCenterX = this.iPosX + 191;
        this.theWheel1.iAngle = 0;
        this.theWheel2.iAngle = 0;
    }
    // draw car image
    ctx.drawImage(this.imageObj, this.iPosX, this.iPosY, this.iWidth, this.iHeight);
    // and animated wheels
    this.theWheel1.draw();
    this.theWheel2.draw();
};
// drawing functions
function clear() { // clear canvas function
    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
}
function drawScene() { // main drawScene function
    if (bPlay == 1) {
        clear(); // clear canvas
        // draw animated objects
        theSun.draw();
        theCar1.draw();
        theCar2.draw();
        theCar3.draw();
    }
}
function getRand(x, y) {
    return Math.floor(Math.random()*y)+x;
}
// binding onload event
if (window.attachEvent) {
    window.attachEvent('onload', main_init);
} else {
    if(window.onload) {
        var curronload = window.onload;
        var newonload = function() {
            curronload();
            main_init();
        };
        window.onload = newonload;
    } else {
        window.onload = main_init;
    }
}
function main_init() {
    // create canvas and context objects
    canvas = document.getElementById('panel');
    ctx = canvas.getContext('2d');
    // initialization of animated objects
    theSun = new Sun(725, 100, 25, 10, 'red', 'yellow');
    theCar1 = new Car(0, 300, 4, 226, 66);
    theCar2 = new Car(100, 400, 3, 226, 66);
    theCar3 = new Car(200, 500, 2, 226, 66);
    setInterval(drawScene, 40); // loop drawScene
}

Exactly 200 lines of code :-) Here you can see three kind of objects (classes): Sun, Car and CarWheel. Each type have own constructor, own set of properties and Draw function. Sun rays we will draw as triangles. Car itself, consist of image and two wheels. Sun and Wheels uses patterns with loops. To create the sun (as example) we can create a loop with X iterations (where X is amount of rays or sectors). And, we will draw each ray separately (in loop).

恰好200行代码:-)在这里您可以看到三种对象(类):Sun,Car和CarWheel。 每种类型都有自己的构造函数,自己的属性集和Draw函数。 太阳光线我们将绘制为三角形。 汽车本身包括图像和两个轮子。 Sun和Wheels使用带有循环的模式。 为了创建太阳(例如),我们可以创建一个具有X次迭代的循环(其中X是射线或扇形的数量)。 并且,我们将分别绘制每个射线(循环)。

现场演示

结论 (Conclusion)

Welcome back to read something new and interesting about HTML5. Good luck in your projects.

欢迎回来阅读有关HTML5的新知识。 在您的项目中祝您好运。

翻译自: https://www.script-tutorials.com/html5-animation-patterns-with-loops/

h5动画怎么循环

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值