HTML5 | Canvas Basic Operations

Source: Internet
Author: User
Tags save file

<canvas>- define the use JavaScript the image is drawn.

P.S.

  • no use Set the size of the canvas, either in <canvas> use width and height Defined individually, either in js canvas The default canvas size is 300 x 150
  • GetContext is a DOM the method of the object, which is the native JS method, cannot be used JQuery Object Direct Access

————————————————————————————————————————————

Demo1-Draw Rectangle

Steps:

    1. Get the canvas element
    2. Get context
    3. Fill and draw borders
    4. Set drawing style
    5. Specify Brush width
    6. Set color values
    7. Draw a rectangle

<<canvas.js>>

functionDraw (Elem) {//1. Get the canvas element    varCanvas =document.getElementById (Elem); Canvas.width= "600"; Canvas.height= "500"; //2. Get Context    varcontext = Canvas.getcontext (' 2d '); //3. Fill and draw the borderContext.fillstyle = ' #000 '; //4. Set drawing styleContext.strokestyle = ' #f60 '; //5. Specify the brush widthContext.linewidth = 3; //7. Draw a rectangleContext.fillrect (100,0,300,400); Context.strokerect (130,100,180,120);}

<<index.html>>

<!DOCTYPE HTML><HTML><Head>    <title>Canvas</title>    <Scripttype= "Text/javascript"src= "Canvas.js"></Script></Head><Bodyonload= "Draw (' canvas ')">    <!--drawing with the draw function -    <!--Defining the canvas -    <!--<canvas id= "Canvas" style= "border:1px black Solid;" width= "600px" height= "500px" ></canvas> -    <CanvasID= "Canvas"style= "border:1px black solid;"></Canvas></Body></HTML>

————————————————————————————————————————————

Demo2-Draw a circle

Steps:

    1. Start creating a path
    2. Create a graphics path
    3. Create complete Close path
    4. Invoke drawing method to draw by drawing style

<<canvas.js>>

functionDraw (Elem) {varCanvas = document.getElementById (' Canvas '); Canvas.width= 600; Canvas.height= 500; varcontext = Canvas.getcontext (' 2d '); Context.fillstyle= "#eeefff"; Context.fillrect (0, 0, 600, 500);  for(vari = 0; I < 10; i++) {Context.beginpath ();//Create start PathContext.arc (i *, I *, I *, 0, Math.PI * 2,true);//Create a graphics pathContext.closepath ();//create complete close pathContext.fillstyle = "Rgba (255,0,0,0.25)";//Path StyleContext.fill (); }}

————————————————————————————————————————————

Demo3-Draw Text

    1. Set Text font

      P.S. When you set a font, you can set several different fonts, in priority order, and if there is no current font, use the next

    2. Set the vertical alignment of text
    3. Set Text horizontal alignment

<<canvas.js>>

functionDraw (Elem) {varCanvas = document.getElementById (' Canvas '); Canvas.width= 800; Canvas.height= 300; Context= Canvas.getcontext (' 2d '); Context.fillstyle= "Green"; Context.fillrect (0, 0, 800, 300); //Set Text colorContext.fillstyle = "#fff"; Context.strokestyle= "#fff"; //Set FontContext.font = "Bold 40px ' Kaiti ', ' SimSun ', ' Microsoft Yahei '"; //Set AlignmentContext.textalign = ' Start '; Context.textbaseline= ' Bottom '; //Set ContentContext.filltext (' Draw text ', 200, 50);//Fill ModeContext.stroketext (' Draw text ', 100, 150);//Contour ModeContext.filltext (' 1111111111111111111111111111111111111111111 ', 200, 250, 500);//prevents overflow, which is compressed when the width is exceeded}

————————————————————————————————————————————

Demo4-Save File

Canvas API the save file is dynamically exported through the paint state to a Data URL The process of the data that the address points to

Todataurl () Method

<<canvas.js>>

1 functionDraw (Elem) {2     varCanvas =document.getElementById (elem);3Canvas.width = 800;4Canvas.height = 500;5     varcontext = Canvas.getcontext (' 2d ');6Context.fillstyle = "#eeefff";7Context.fillrect (0,0,800,500);8window.location = Canvas.todataurl (' image/jpeg '));9}

————————————————————————————————————————————

Demo5-Simple animations

HTML DOM setinterval () Method

<<canvas.js>>

functionDraw (Elem) {varCanvas =document.getElementById (Elem); Canvas.width= 800; Canvas.height= 500; varcontext = Canvas.getcontext (' 2d '); Context.fillstyle= "#eeefff"; Context.fillrect (0, 0, 800, 500); Context.fillstyle= ' #f90 '; //Canvas Erase partContext.clearrect (canvas.width-50, canvas.height-50); //execute the painting () function once every 100ms to draw a new small box    vari = 0; varPaintid = setinterval (painting, 100); functionpainting () {++i; Context.fillrect (+ i * 15, 80, 10, 10); if(i = = 20) {clearinterval (Paintid); }    }}

HTML5 | Canvas Basic Operations

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.