HTML5-canvas-basics, html5canvas

Source: Internet
Author: User

HTML5-canvas-basics, html5canvas

<Canvas> New Elements

<Canvas> elements are used to draw images, which are completed by scripts (usually JavaScript.

<Canvas> labels are only graphical containers. You must use scripts to draw images.

You can use Canva to draw paths, boxes, circles, characters, and add images in multiple ways.

 

Note:By default, the <canvas> element has no borders or content.

  Painter

<! -- FillStyle is a fill style. If the fillStyle attribute is not set, the default fill color is black, and fillRect is a rectangle. The parameter is X, Y, Width, and Height. --> <! DOCTYPE html> // Ctx. moveTo (0, 0); // defines the start Coordinate
// Ctx. lineTo (100,100); // defines the end Coordinate
// Ctx. stroke (); // draw a line
            })        </script>    

Running result:

  Circle

<! -- Arc circle, the parameter is X, Y, radius, start angle, end angle, clockwise/counterclockwise (optional) XY refers to the distance of the dot --> <! DOCTYPE html> // Ctx. fillText ("Hello World", 10, 50); // draw a solid font
// Ctx. strokeText ("Hello World",); // draw a hollow font
}) </Script> <style type = "text/css"> canvas {border: 1px solid #000 ;} </style>

Running result:

 Gradient

CreateLinearGradient (X, y, x1, y1)-Create a line gradient

CreateRadialGradient (X, y, r, x1, y1, r1)-Create a radial/circular gradient

// Create a gradient var grd = ctx. createLinearGradient (, 0); grd. addColorStop (0, "red"); grd. addColorStop (1, "blue"); // fill in the gradient ctx. fillStyle = grd; ctx. fillRect (, 80 );

Running result:

 Radial/circular gradient

Var c = document. getElementById ("myCanvas"); var ctx = c. getContext ("2d"); // create a gradient var grd = ctx. createRadialGradient (60,100,); grd. addColorStop (0, "red"); grd. addColorStop (1, "white"); // fill the gradient ctx. fillStyle = grd; ctx. fillRect (, 80 );

Running result:

Image

<! DOCTYPE html> 

Running result:

(Canvas image on the right)

Related Article

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.