Html5 canvas draws the instance code of rectangle and circle, html5canvas

Source: Internet
Author: User

Html5 canvas draws the instance code of rectangle and circle, html5canvas

Html5 canvas instance code for drawing Rectangles and circles

Copy the content to the clipboard using JavaScript Code
  1. <! DOCTYPE html>
  2. <Html>
  3. <Head>
  4. <Meta charset = "UTF-8">
  5. <Title> </title>
  6. </Head>
  7. <Body onload = "draw (), drawarc ()">
  8. <! -- Draw steps: Get the canvas Element-> Get context-> Fill and draw borders-> set the drawing style -->
  9. <! -- Use the path to draw other complex images: start to create a path-> close a path-> draw a graph -->
  10. <! -- Eg: Draw a rectangle -->
  11. Draw a rectangle: <canvas id = "ca"> </canvas> <br/>
  12. Draw a circle: <canvas id = "yuan"> </canvas>
  13. </Body>
  14. </Html>
  15. <Script>
  16. // Draw a rectangle
  17. Function draw (){
  18. Var canvas = document. getElementById ('CA'); // get the canvas Element
  19. If (canvas = null)
  20. Return false;
  21. Var context = canvas. getContext ('2d '); // obtain the context
  22. Context. fillStyle = '# EEEFF'; // fill color
  23. Context. fillRect (0, 0, 400,300); // fill the rectangle (rectangle 1)
  24. Context. fillStyle = 'red ';
  25. Context. strokeStyle = 'blue'; // border color
  26. Context. lineWidth = 1; // Border Width
  27. Context. fillRect (50, 50, 100,100); // fill the rectangle (Interior rectangle 2)
  28. Context. strokeRect (50, 50, 100,100); // draw a border
  29. }
  30. // Draw a circle
  31. Function drawarc (){
  32. Var canvas2 = document. getElementById ('yuanyuan '); // obtain the canvas Element.
  33. If (canvas2 = null)
  34. If (canvas2 = null)
  35. Return false;
  36. Var context2 = canvas2.getContext ('2d '); // obtain the context
  37. Context2.fillStyle = '# EEEEEF ';
  38. Context2.fillRect (0, 0, 400,300 );
  39. Var n = 0;
  40. For (var I = 0; I <10; I ++ ){
  41. Context2.beginPath (); // start to create a path
  42. Context2.arc (I * 25, I * 25, I * 10, 0, Math. PI * 2, true); // create a circular path
  43. Context2.closePath (); // close the path
  44. Context2.fillStyle = 'rgba (0.25, 0,) '; // set the color
  45. Context2.fill (); // fill the image
  46. }
  47. }
  48. </Script>

The above is the full content of the html5 canvas instance code for drawing Rectangles and circles. I hope you can support the steps.

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.