Canvas draw simple animation, canvas draw

Source: Internet
Author: User

Canvas draw simple animation, canvas draw

It is relatively easy to create an animation in the canvas. It is actually a process of changing coordinates, erasure, and re-painting.

1. Use the setInterval method to set the animation interval.

SetInterval (code, millisec) setInterval method inherent in html, this method accepts two parameters, the first function represents the function that executes the animation, the second parameter is the interval, the Unit is (MS ).

2. functions used for drawing

1) The animation effect is realized by constantly changing the coordinates of X and Y.

2) use the clearRect method to erase the entire or local canvas.

ClearRect:

Context. clearRect (x, y, width, height );

X refers to the abscissa of our starting point, y refers to the ordinate of our starting point, width refers to the length of the wiping sub, and height refers to the height of the wiping sub.

 

<! DOCTYPE html> 

 

Var context; var I, j; var width, height; function draw (id) {var canvas = document. getElementById (id); context = canvas. getContext ('2d '); width = canvas. width; height = canvas. height; context. fillStyle = 'green'; context. fillRect (100, width, height); setInterval (painting,); I = 0; j = 0;} function painting () {// Example 1: // context. fillStyle = 'red'; // context. fillRect (I, I, 10, 10); // context. fillRect (I, 200-j, 10, 10); // I ++; // j ++; // Example 2: context. fillStyle = 'white'; context. clearRect (I, 20, 1, 10); I ++ ;}

 

Related Article

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.