Comments: As we all know, currently not all browsers support html5. Even html5 browsers do not necessarily support all html5 new features. We recommend that you use firefox (developer's favorite) or chrome. All my examples are developed based on firefox. From today on, we will start a series of courses on html5 canvas. This series is my summary after reading HTML5 Canvas: Native Interactivity and Animation for the Web. If you are interested, you can download the original English books and read them. This book introduces canvas game development to show us the powerful functions of canvas. I think it is quite good. by reading this book, I learned a lot about canvas. In fact, canvas does not have a lot of APIs. The key is to learn and use it, and learn to make incredible results for the combined use of APIs. This book is the best choice for you to learn canvas. Unfortunately, he has no Chinese version yet, and friends with poor English can only wait.
As we all know, currently not all browsers support html5. Even html5 browsers do not necessarily support all html5 new features. Therefore, you should select a relatively new browser as your debugging environment. We recommend that you use firefox (developer's favorite) or chrome browser. All my examples are developed based on firefox.
I will not introduce the basic html5-related knowledge here. I have a lot of tutorials on html5 on the Internet. To learn html5, you need to have a good javascript Foundation. You can go to Uncle Tom's blog to learn: http://www.cnblogs.com/tomxu/archive/2011/12/15/2288411.html. In fact, this series of courses is quite difficult. You should be a js expert if you learn more than 50 articles.
Now we officially start our canvas course. The first example is "hello canvas ".
First, add the canvas label to the body, as shown below::
The Code is as follows:
<Canvas id = "canvasOne" width = "500" height = "300">
Your browser does not support HTML5 Canvas.
</Canvas>
The text section in the canvas is displayed when the browser does not support the canvas object.
The canvas tag is defined. When we need to operate on it through js, we can implement it through getElementById.
Var theCanvas = document. getElementById ("canvasOne"); now we are used to developing tasks using jquery. How can we obtain canvas objects using jquery?
Var canvas = $ ('# canvasOne '). get (0); or var canvas = $ ('# canvasOne') [0]; I don't know if get (0) and [0] do not exist. If get () is not used () method or [] subscript, your js Code will not be able to operate the canvas normally. Because $ ('# canvasOne') obtains a jquery object, and what we actually want to operate on is an html dom object. Here there is a problem of converting a jquery object into a dom object. The conversion is completed through get () or the following method. To convert a dom object to a jquery object, you can use the $ () method. I don't know if my friends only go to Baidu.
To ensure code robustness, we need to determine whether your browser supports canvas objects. You can use the following code.
The Code is as follows:
If (! TheCanvas |! TheCanvas. getContext ){
Return;
}
However, we recommend that you use the modernizr. js library to complete this job. This is a very popular html5 js library and provides many useful methods.
The Code is as follows:
Function canvasSupport (){
Return Modernizr. canvas;
}
Canvas supports 2d rendering and is implemented using the following code::
Var context = theCanvas. getContext ("2d ");
Now we can use the context object to draw images on the canvas.
The Code is as follows:
// Set the region color
Context. fillStyle = "# ffffaa ";
// Draw a region
Context. fillRect (0, 0,500,300 );
// Set the font
Context. font = "20px _ sans ";
// Sets the vertical alignment mode.
Context. textBaseline = "top ";
// Draw text
Context. fillText ("Hello World! ", 195, 80 );
// Set the border color
Context. strokeStyle = "#000000 ";
// Draw a border
Context. strokeRect (5, 5,490,290 );
The following describes how to draw a piece. Because of the asynchronous download of the image, we use the following method to ensure that the image has been downloaded when you draw an image using canvas:
The Code is as follows:
Var helloWorldImage = new Image ();
HelloWorldImage. src = "helloworld.gif ";
HelloWorldImage. onload = function (){
Context. drawImage (helloWorldImage, 160,130 );
}
When the image below is complete, the onload event will be triggered, and the image will be drawn using the context object here.
Download the demo to see the complete code. demo: html5canvas.helloworld.zip