Examples of canvas circular motion and elliptical motion in HTML5

Source: Internet
Author: User
Tags cos sin


A. Circular motion

1.1 Thought Analysis:

The equations of the circle are:

(x0, y0) is the center of the circle; (x, y) is the point on the circle.
(x-x0) ^ 2 + (y-y0) ^ 2 = r ^ 2
cos (angle) ^ 2 + sin (angle) ^ 2 = 1
Therefore, the combination of the above two-type, can be:

x = R * cos (angle) + x0
y = R * sin (angle) + y0

Therefore, the sine function is used to compute the y-coordinate and the cosine function is used to compute the x-coordinate.

1.2 Examples:

Compatible functions for Cancelrequestanimframe
Window.cancelrequestanimframe = (function () {
return Window.cancelanimationframe | |
Window.webkitcancelrequestanimationframe | |
Window.mozcancelrequestanimationframe | |
Window.ocancelrequestanimationframe | |
Window.mscancelrequestanimationframe | |
cleartimeout;
} )();

Window.onload = function () {
var canvas = document.getElementById ("Canvas"),
Context = Canvas.getcontext ("2d"),
Ball = new ball (2),
Angle = 0,
CenterX = CANVAS.WIDTH/2,
CenterY = CANVAS.HEIGHT/2,
Radius = 50,
Speed = 0.05,
timer = null;

ball.linewidth = 0;

(function Drawframe () {
Timer = window.requestanimationframe (drawframe, canvas);
if (Angle > Math.PI * 2 && timer) {
Window.cancelrequestanimframe (timer);
timer = null;
}
Context.clearrect (0, 0, canvas.width, canvas.height);
BALL.Y = centery + math.sin (angle) * RADIUS;
ball.x = CenterX + math.cos (angle) * RADIUS;
Angle + = speed;

Ball.draw (context);
})();
}

Two. Elliptical motion


2.1 Thought Analysis:

The equation of the ellipse is:

(x0, y0) is the center position of the ellipse; (x, y) is the point on the ellipse
[(x-x0)/RadiusX] ^ 2 + [(y-y0)/radiusy] ^ 2 = 1
cos (angle) ^ 2 + sin (angle) ^ 2 = 1
Therefore, the combination of the above two-type, can be:

x = radiusx * cos (angle) + x0
y = radiusy * sin (angle) + y0
The coordinate value of ellipse motion can be obtained.

2.2 Examples:

Compatible functions for Cancelrequestanimframe
Window.cancelrequestanimframe = (function () {
return Window.cancelanimationframe | |
Window.webkitcancelrequestanimationframe | |
Window.mozcancelrequestanimationframe | |
Window.ocancelrequestanimationframe | |
Window.mscancelrequestanimationframe | |
cleartimeout;
} )();

Window.onload = function () {
var canvas = document.getElementById ("Canvas"),
Context = Canvas.getcontext ("2d"),
Ball = new ball (2),
Angle = 0,
CenterX = CANVAS.WIDTH/2,
CenterY = CANVAS.HEIGHT/2,
RadiusX = 150,
RadiusY = 100,
Speed = 0.05,
timer = null;

ball.linewidth = 0;

(function Drawframe () {
Timer = window.requestanimationframe (drawframe, canvas);
if (Angle > Math.PI * 2 && timer) {
Window.cancelrequestanimframe (timer);
timer = null;
}
Context.clearrect (0, 0, canvas.width, canvas.height);
BALL.Y = centery + math.sin (angle) * RADIUSY;
ball.x = CenterX + math.cos (angle) * RADIUSX;
Angle + = speed;

Ball.draw (context);
})();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.