HTML5 Graphic Drawing

Source: Internet
Author: User

To draw a graphic in HTML5, first place a canvas element

<id= "Canvas"  width= " max" height= "  />

The canvas's code is actually placed in a JavaScript script, so you get the ID of the element first

var canvas =document.getelementbyid ("canvas"); if (canvas==null) return False;var Context=canvas.getcontext ("2d"); Context.fillstyle= "#eeeeff"; Context.fillrect (0,0,400,300); context.fillstyle= "Red"; context.strokestyle= "Blue"; Context.linewidth=1;context.fillrect (50,50,1001,100); Context.strokerect (50,50,100,100);

All code

<!DOCTYPE HTML><HTML><Head>   <MetaCharSet= "Utf-8"/>   <title>Example for HTML5</title>      </Head><Body><Divclass= "ShowMore">   <atitle= "This is a hint"ID= "tipping">Tips</a>   <CanvasID= "Canvas"width= "The "Height= "+"/></Div><Scripttype= "Text/javascript">varCanvas=document.getElementById ("Canvas");if(Canvas==NULL) Alert ("NULL");varContext=Canvas.getcontext ('2d'); Context.fillstyle="#eeeeff"; Context.fillrect (0,0, -, -); Context.fillstyle="Red"; Context.strokestyle="Blue"; Context.linewidth=1; Context.fillrect ( -, -, -, -); Context.strokerect ( -, -, -, -);</Script></Body></HTML>

I've also been learning about jquery, so how do you draw canvas in jquery? It is important to note that the JQuery object cannot directly use the methods in the canvas, so the first thing to do is to turn the jquery object into a DOM object before you can draw, and of course, the code is not much simpler.

<!DOCTYPE HTML><HTML><Head>   <MetaCharSet= "Utf-8"/>   <title>Example for HTML5</title>   <Scriptsrc= "Jquery.js"type= "Text/javascript"></Script>   </Head><Body><Divclass= "ShowMore">   <atitle= "This is a hint"ID= "tipping">Tips</a>   <CanvasID= "Canvas"width= "The "Height= "+"/></Div><Scripttype= "Text/javascript">$ (document). Ready (function(){varCanvas=$("#canvas")[0];if(Canvas==NULL) Alert ("NULL");varContext=Canvas.getcontext ('2d'); Context.fillstyle="#eeeeff"; Context.fillrect (0,0, -, -); Context.fillstyle="Red"; Context.strokestyle="Blue"; Context.linewidth=1; Context.fillrect ( -, -, -, -); Context.strokerect ( -, -, -, -);});</Script></Body></HTML>

Draw a Circle

for (Var i=0;i<8; i++) {Context.beginpath (); Context.arc (25*i,25*i,i*10,0,math.pi*2,true); Context.closepath (); Context.fillStyle = ' Rgba (255,0,0,0.25) '; Context.fill ();}

Draw a line

Context.beginpath ();                      The starting point of the path, set this point is mainly to prevent subsequent staining affecting the previous part of the context.strokestyle= "black";              Sets the thickness and color of the line Context.linewidth=10;context.moveto (20,20);                    Set line start, End Context.lineto (30,100); Context.stroke ();                         Draw

Loading pictures

var image=new image (); image.src= "1.png"; Image.onload=function () {context.drawimage (image,20,20,20,20)    ; are drawing objects, drawing the starting point x, y, image size w,h. };

HTML5 Graphic Drawing

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.