10 minutes, draw a small loading interface using canvas (by the way, we will discuss the problem of drawing efficiency), canvasloading

Source: Internet
Author: User

10 minutes, draw a small loading interface using canvas (by the way, we will discuss the problem of drawing efficiency), canvasloading

First, use the canvas style defined.

<Canvas width = "1024" height = "720" id = "canvas" style = "border: 1px solid #808080; display: block; margin: 100px auto;> your browser does not support canvas. </canvas>

Here we mainly want to talk about width and height. do not define it in the style, otherwise it will be stretched. (For this, we recommend that you read the W3c documentation, not very familiar with it)

The height and width attributes determine the coordinate system of the width and height of the canvas, while the CSS attribute only determines the size of the box, which will be displayed.


 

var canvas = document.getElementById('canvas');        var ctx = canvas.getContext('2d');

 

Obtain the object, and then obtain the context for drawing.


Okay. Let's take a small example (draw an arc)

 

// Set the line width and color (I like to name him as a hollow plot)
        ctx.lineWidth = 2;        ctx.strokeStyle = '#176785';
// Arc is an arc shape. If fill is used, it is closed. Ctx. beginPath (); ctx. arc (512,360,100, 0.5 * Math. PI, 1 * Math. PI); ctx. stroke (); ctx. closePath ();

 

 

 

For ctx. beginPath (); and ctx. closePath (); I think it is the soul of canvas. Some good canvas engines are highly efficient, and they have a great relationship.

1. For example, some people draw a canvas with sertices, that is, they draw the same effect after N times on the canvas.

Let's look at the following example.

        ctx.beginPath();        ctx.arc(512, 360, 100, 0.5 * Math.PI, 1 * Math.PI);        ctx.closePath();        ctx.stroke();

 


This isCtx. closePath (); is used to close the lines, but the fill method has ctx by default. closePath (); this function, while ctx. beginPath (); is used to split the scope of a function.

2. The example shows the above. If you need to remove ctx. beginPath (); from the above two sections of code, the arc will be drawn twice, and the second will be drawn once. The implication is that if there is no ctx. beginPath ();, the code will draw 100 images, the first will draw 100 times, the first 99 times, in short, 100 + 99 + 98 + .....


3. The next two small examples.

// Define the color of a gradient, in fact, coordinates, x, y, w, h
Var color = ctx. createLinearGradient (512,460,512,260); // start color, and end color
Color. addColorStop (0, '#499989'); color. addColorStop (1, '#176785'); ctx. beginPath (); ctx. fillStyle = color; ctx. arc2 (512,360, 99, 0, 2 * Math. PI); ctx. fill (); ctx. closePath (); // draw the text var a = 12; ctx. font = "50px Arial"; ctx. fillStyle = "# fff ";
// Center ctx. textAlign = "center"; ctx. beginPath ();
// Text content, starting coordinate, width ctx. fillText (a, 510,375, 64); ctx. closePath ();

 

 




4. Next we will combine the above content.

 

// Timer var timeClear; // defines some numbers var arcNum = 0.02, textNum = 1; function load1 () {if (textNum> = 101) {return 0;} ctx. lineWidth = 2; ctx. strokeStyle = '#176785'; ctx. beginPath (); ctx. arc (512,360,100, (0.5 + arcNum-0.02) * Math. PI, (0.5 + arcNum) * Math. PI); ctx. stroke (); ctx. closePath (); // draw the gradient color var color = ctx. createLinearGradient (0, 512,460,512,260); color. addColorStop (0, '#499989'); color. addColorStop (1, '#176785'); ctx. beginPath (); ctx. fillStyle = color; ctx. arc (512,360, 99, (0.5-arcNum/2) * Math. PI, (0.5 + arcNum/2) * Math. PI); ctx. fill (); ctx. closePath (); // draw text ctx. font = "80px Arial"; ctx. fillStyle = "# fff"; ctx. textAlign = "center"; ctx. beginPath (); ctx. fillText (textNum, 510,385,124); ctx. closePath (); // ctx. clearRect (1024,720, 0.02); arcNum = arcNum +; textNum = textNum + 1;} timeClear = setInterval (load1, 60 );

 

It looks uncomfortable. I suggest you test it on your computer.

 

http://www.gbtags.com/gb/debug/7400a9ba-51a7-409b-b1f4-b01363b9c625.htm

 

 

A simple loading is finished, but there are many problems in it. For example, the fill and text are partially painted repeatedly and 99 times are drawn, in fact, I mainly want to use the circle fill to replace the clear text after each painting. If you change the text color, you may understand what I mean. This will then cause the bottom to be drawn 100 times, resulting in a jagged appearance at the bottom.

In addition, placing the circle in front is equivalent to clearRect to clear the text.


We don't know how to solve this problem.




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.