Summary of text alignment using HTML5 Canvas,

Source: Internet
Author: User

Summary of text alignment using HTML5 Canvas,

Horizontal Alignment textAlign

Copy the content to the clipboard using JavaScript Code
  1. Context. textAlign = "center | end | left | right | start ";

The values and meanings are shown in the following table.

Value Description
Start Default value. The text starts at the specified position.
End The text ends at the specified position.
Center The center of the text is placed in the specified position.
Left Left-aligned text,
Right Right-aligned text.

Let's use an example to intuitively feel it.

Copy the content to the clipboard using JavaScript Code
  1. <! DOCTYPE html>
  2. <Html lang = "zh">
  3. <Head>
  4. <Meta charset = "UTF-8">
  5. <Title> textAlign </title>
  6. <Style>
  7. Body {background: url ("./images/bg3.jpg") repeat ;}
  8. # Canvas {border: 1px solid # aaaaaa; display: block; margin: 50px auto ;}
  9. </Style>
  10. </Head>
  11. <Body>
  12. <Div id = "canvas-warp">
  13. <Canvas id = "canvas">
  14. Does your browser support Canvas ?! Just change one !!
  15. </Canvas>
  16. </Div>
  17. <Script>
  18. Window. onload = function (){
  19. Var canvas = document. getElementById ("canvas ");
  20. Canvas. width = 800;
  21. Canvas. height = 600;
  22. Var context = canvas. getContext ("2d ");
  23. Context. fillStyle = "# FFF ";
  24. Context. fillRect (0, 0, 800,600 );
  25. // Create a blue line at location 400
  26. Context. strokeStyle = "blue ";
  27. Context. moveTo (400,100 );
  28. Context. lineTo (400,500 );
  29. Context. stroke ();
  30. Context. fillStyle = "#000 ";
  31. Context. font = "50px Arial ";
  32. // Display different textAlign values
  33. Context. textAlign = "start ";
  34. Context. fillText ("textAlign = start", 400,120 );
  35. Context. textAlign = "end ";
  36. Context. fillText ("textAlign = end", 400,200 );
  37. Context. textAlign = "left ";
  38. Context. fillText ("Fig = left", 400,280 );
  39. Context. textAlign = "center ";
  40. Context. fillText ("textAlign = center", 400,360 );
  41. Context. textAlign = "right ";
  42. Context. fillText ("textAlign = right", 400,480 );
  43. };
  44. </Script>
  45. </Body>
  46. </Html>

Running result:

Vertical Alignment textBaseline

Copy the content to the clipboard using JavaScript Code
  1. Context. textBaseline = "alphabetic | top | hanging | middle | ideographic | bottom ";

The values and meanings are shown in the following table.

Value Description
Alphabetic Default value. A text baseline is a normal letter baseline.
Top The text baseline is at the top of the em box.
Hanging A text baseline is a suspension baseline.
Middle The text baseline is the center of the em box.
Ideographic A text baseline is an ideographic baseline.
Bottom The text baseline is the bottom of the em box.

First, let's look at the positions represented by each Baseline through a graph.

Let's use an example to intuitively feel it.

Copy the content to the clipboard using JavaScript Code
  1. <! DOCTYPE html>
  2. <Html lang = "zh">
  3. <Head>
  4. <Meta charset = "UTF-8">
  5. <Title> textBaseline </title>
  6. <Style>
  7. Body {background: url ("./images/bg3.jpg") repeat ;}
  8. # Canvas {border: 1px solid # aaaaaa; display: block; margin: 50px auto ;}
  9. </Style>
  10. </Head>
  11. <Body>
  12. <Div id = "canvas-warp">
  13. <Canvas id = "canvas">
  14. Does your browser support Canvas ?! Just change one !!
  15. </Canvas>
  16. </Div>
  17. <Script>
  18. Window. onload = function (){
  19. Var canvas = document. getElementById ("canvas ");
  20. Canvas. width = 800;
  21. Canvas. height = 600;
  22. Var context = canvas. getContext ("2d ");
  23. Context. fillStyle = "# FFF ";
  24. Context. fillRect (0, 0, 800,600 );
  25. // Draw the Blue Line at position y = 300
  26. Context. strokeStyle = "blue ";
  27. Context. moveTo (0,300 );
  28. Context. lineTo (800,300 );
  29. Context. stroke ();
  30. Context. fillStyle = "#00 AAAA ";
  31. Context. font = "20px Arial ";
  32. // Place each word with different textBaseline values in y = 300
  33. Context. textBaseline = "top ";
  34. Context. fillText ("Top", 150,300 );
  35. Context. textBaseline = "bottom ";
  36. Context. fillText ("Bottom", 250,300 );
  37. Context. textBaseline = "middle ";
  38. Context. fillText ("Middle", 350,300 );
  39. Context. textBaseline = "alphabetic ";
  40. Context. fillText ("Alphabetic", 450,300 );
  41. Context. textBaseline = "hanging ";
  42. Context. fillText ("Hanging", 550,300 );
  43. };
  44. </Script>
  45. </Body>
  46. </Html>

Running result:

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.