Html5 simulated flat throwing (simulating the flat throwing process) _ html5 tutorial skills-

Source: Internet
Author: User
An object is thrown along the horizontal direction at a certain initial speed. if the object is only subjected to gravity, this kind of movement is called flat throwing. This article describes how to use html5 to simulate the flat throwing of a ball, the specific code is as follows. If you are interested, you can refer to it and hope to help you throw an object at a certain initial speed along the horizontal direction. If the object is only subject to gravity, this kind of movement is called flat throwing. The flat throwing motion can be seen as the constant linear motion in the horizontal direction and the combination of the vertical freely falling motion. Because of the constant force of an object, the flat motion is a uniformly variable speed curve. The trajectory of the flat object is a parabolic curve. The flat movement is the curve movement. The flat movement time is only related to the vertical height of the throwing point. The horizontal displacement of the object landing is related to the time (vertical height) and the horizontal initial velocity.


The Code is as follows:





Html5 Shells
Script
// Box
Var box_x = 0;
Var box_y = 0;
Var box_width = 300;
Var box_height = 300;
// Ball
Var ball_x = 10;
Var ball_y = 10;
Var ball_radius = 10;
Var ball_vx = 10;
Var ball_vy = 0;
// Constant
Var g = 10; // note
Var rate = 0.9;
// Bound
Var bound_left = box_x + ball_radius;
Var bound_right = box_x + box_width-ball_radius;
Var bound_top = box_y + ball_radius;
Var bound_bottom = box_y + box_height-ball_radius;
// Context
Var ctx;
Function init ()
{
Ctx = document. getElementById ('canvas '). getContext ('2d ');
Ctx. lineWidth = ball_radius;
Ctx. fillStyle = "rgb (200,0, 50 )";
Move_ball ();
SetInterval (move_ball, 100 );
}
Function move_ball ()
{
Ctx. clearRect (box_x, box_y, box_width, box_height );
Move_and_check ();
Ctx. beginPath ();
Ctx. arc (ball_x, ball_y, ball_radius, 0, Math. PI * 2, true );
Ctx. fill ();
Ctx. strokeRect (box_x, box_y, box_width, box_height );
}
Function move_and_check ()
{
Var cur_ball_x = ball_x + ball_vx;
Var temp = ball_vy;
Ball_vy = ball_vy + g;
Var cur_ball_y = ball_y + ball_vy + g/2;
If (cur_ball_x {
Cur_ball_x = bound_left;
Ball_vx =-ball_vx* 0.9;
Ball_vy = ball_vy * 0.9;
}
If (cur_ball_x> bound_right)
{
Cur_ball_x = bound_right;
Ball_vx =-ball_vx* 0.9;
Ball_vy = ball_vy * 0.9;
}
If (cur_ball_y {
Cur_ball_y = bound_top;
Ball_vy =-ball_vy * 0.9;
Ball_vx = ball_vx * 0.9;
}
If (cur_ball_y> bound_bottom)
{
Cur_ball_y = bound_bottom;
Ball_vy =-ball_vy * 0.9;
Ball_vx = ball_vx * 0.9;
}
Ball_x = cur_ball_x;
Ball_y = cur_ball_y;
}
Script






Html5 simulates the flat throwing of a ball.

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.