HTML5 + CSS3 draw a jagged rectangle and html5css3 draw a sawtooth

Source: Internet
Author: User

HTML5 + CSS3 draw a jagged rectangle and html5css3 draw a sawtooth

Recently, I have been typing Html5 + Css3 to share some of the things I think are worth learning.

How to draw a jagged rectangle:

We know that canvas can be used to draw images. canvas is a new tag that appears in HTML5. It is used to draw images on webpages. H5 canvas uses Javascript to draw images on webpages.
The above jagged rectangle is drawn using canvas.

Implementation Code:

Copy XML/HTML Code to clipboard
  1. <! Doctype html>
  2. <Html lang = "en">
  3. <Head>
  4. <Meta charset = "UTF-8">
  5. <Title> sawtooth chart </title>
  6. <Script type = "text/javascript">
  7. Window. addEventListener ("load", eventWindowLoaded, false );
  8. Function eventWindowLoaded (){
  9. Var x, y;
  10. Var theCanvas = document. getElementById ("canvas ");
  11. Var context = theCanvas. getContext ("2d ");
  12. Context. strokeStyle = '# CB9A61 ';
  13. Context. lineWidth = 10;
  14. Context. strokeRect (10, 10, theCanvas. width-20, theCanvas. height-20 );
  15. Context. fillStyle = "# FFFFFF ";
  16. For (x = 5; x <= canvas. width; xx = x + 10 ){
  17. Context. beginPath ();
  18. Context. arc (x, 5, 5, 0, Math. PI * 2, true );
  19. Context. arc (x, canvas. height-5, 5, 0, Math. PI * 2, true );
  20. Context. closePath ();
  21. Context. fill ();
  22. }
  23. For (y = 5; y <= canvas. height; yy = y + 10 ){
  24. Context. beginPath ();
  25. Context. arc (5, y, 5, 0, Math. PI * 2, true );
  26. Context. arc (canvas. width-5, y, 5, 0, Math. PI * 2, true );
  27. Context. closePath ();
  28. Context. fill ();
  29. }
  30. }
  31. </Script>
  32. </Head>
  33. <Body>
  34. <Div style = "position: absolute; top: 100px; left: 100px;">
  35. <Canvas id = "canvas" width = "400" height = "170" top = 50px; left = 50px;>
  36. </Div>
  37. </Body>
  38. </Html>

How to write the following shape, a rectangle is divided into two parts, separated by a diagonal line, two colors.

At first it was such an idea that a div was used to draw a diagonal line in the middle and divide it into two parts, two colors, but they were not implemented. Due to the weak front-end and limited capabilities, I thought of another solution.

With three Divs, the left and right are two divs. Setting width and height plays an important role in this part:

It is actually a rectangle, divided into two triangles, and finally achieved the above effect. In another way, it is so easy to implement, so it cannot be suspended on a tree.

The Code is as follows:

Copy XML/HTML Code to clipboard
  1. <! Doctype html>
  2. <Html lang = "en">
  3. <Body style = "margin: 0 0 0 0;">
  4. <Div id = "1" style = "background-color: #727171; width: 50px; height: 20px; float: left"> </div>
  5. <Div id = "2" style = "float: left; border-width: 10px; border-color: #727171 #9fa0a0 #9fa0a0 #727171; border-style: solid "> </div>
  6. <Div id = "3" style = "background-color: #9fa0a0; width: 50px; height: 20px; float: left"> </div>
  7. </Body>
  8. </Html>

In the future, I will continue to summarize some knowledge about HTML and CSS. The front-end knowledge looks simple, but it is actually a meticulous work that can train a person's patience. From Simplicity to complexity, from entry to depth, you can improve yourself a little bit.

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.