Canvas Drawing Features

Source: Internet
Author: User
Tags border color

The basic process of JS drawing is as follows:

    1. JS through the ID to find the canvas element, the following code:
      var c=document.getElementById(‘myCanvas‘)
    2. Create a context object with the following code:
      var cxt=c.getContext(‘2d‘)
      2d is the only function parameter that might support 3D drawings in the future, and all the drawing work is done on the context object.

Canvas drawing-related objects

    1. Cxt.beginpath () Start drawing path
    2. Cxt.closepath () End drawing path
    3. Cxt.stroke () a real drawing
    4. Cxt.strokestyle= ' #fff ', set border color
    5. cxt.linewidth=2, setting the border size
    6. cxt.fillstyle= ' Red ' to set the content fill area color
    7. Cxt.arc (X,y,radius,startangle,endangle,true/false), draw a circular path with parameters of center coordinates (x, y), start and end angles, clockwise/counterclockwise
    8. Cxt.strokerect (X,y,width,height), drawing a rectangular path
    9. Cxt.fillrect (X,y,width,height), drawing a rectangular shape
    10. Cxt.createradialgradient (xstart,ystart,radiusstart,xend,yend,radiusend), Radial gradient range settings
    11. Cxt.createlineargradient (xstart,ystart,xend,yend), linear gradient range settings

HTML code:

<body>    <p>        <span onclick="Btn1_click ();" onmouseover="this.style.cursor= ' pointer ';" >Solid Circle</span>        <span onclick="Btn2_click ();" onmouseover="this.style.cursor= ' pointer ';" >Border Circle</span>        <span onclick="Btn3_click ();" onmouseover="this.style.cursor= ' pointer ';" >Connecting Circle</span>        <span onclick="Btn4_click ();" onmouseover="this.style.cursor= ' pointer ';" >Gradient Circle</span>    </P>    <canvas id="MyCanvas" width= "400px" height=" 400px "></Canvas></body>

JS Code:

<script type="Text/javascript">     function $(ID) {        returndocument.getElementById (ID); } function btn1_click(){        varcvs=$ (' MyCanvas ');varCxt=cvs.getcontext (' 2d '); Cxt.clearrect (0,0, -, -);        Cxt.beginpath (); Cxt.arc ( -, -, -,0,Math. pi*2,true);        Cxt.closepath (); cxt.fillstyle=' #eee ';    Cxt.fill (); } function btn2_click(){        varcvs=$ (' MyCanvas ');varCxt=cvs.getcontext (' 2d '); Cxt.clearrect (0,0, -, -);        Cxt.beginpath (); Cxt.arc ( -, -, -,0,Math. pi*2,true);        Cxt.closepath (); cxt.strokestyle=' #666 '; Cxt.linewidth=2;    Cxt.stroke (); } function btn3_click(){        varcvs=$ (' MyCanvas ');varCxt=cvs.getcontext (' 2d '); Cxt.clearrect (0,0, -, -);        Cxt.beginpath (); Cxt.arc ( -, -, -,0,Math. pi*2,true);        Cxt.closepath (); cxt.strokestyle=' #666 '; Cxt.linewidth=2; cxt.fillstyle=' #eee ';        Cxt.fill ();        Cxt.stroke ();        Cxt.beginpath (); Cxt.arc ( the, -, -,0,Math. pi*2,true);        Cxt.closepath (); cxt.strokestyle=' #666 '; Cxt.linewidth=2;    Cxt.stroke (); } function btn4_click(){        varcvs=$ (' MyCanvas ');varCxt=cvs.getcontext (' 2d '); Cxt.clearrect (0,0, -, -);        Cxt.beginpath (); Cxt.arc ( -, -, -,0,Math. pi*2,true);        Cxt.closepath (); cxt.strokestyle=' #666 '; Cxt.linewidth=2;varGnt=cxt.createradialgradient ( -, -,0, -, -, -); Gnt.addcolorstop (0,' #f00 '); Gnt.addcolorstop (0.3,' #0f0 '); Gnt.addcolorstop (1,' #00f ');        Cxt.fillstyle=gnt;    Cxt.fill (); }</script>


Canvas Drawing Features

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.