1090168-3-ref.html 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. </head>
  6. <body>
  7. <script type="text/javascript">
  8. var testFont = '40px sans-serif';
  9. function test(x, y, text, style, rotation, baseline) {
  10. var canvas = document.createElement("canvas");
  11. canvas.width = 400;
  12. canvas.height = 400;
  13. canvas.style.cssText = 'position:absolute;' + style;
  14. document.getElementsByTagName('body')[0].appendChild(canvas);
  15. var ctx = canvas.getContext('2d');
  16. ctx.globalAlpha = 0.5;
  17. ctx.beginPath();
  18. ctx.moveTo(x - 20, y); ctx.lineTo(x + 20, y);
  19. ctx.moveTo(x, y - 20); ctx.lineTo(x, y + 20);
  20. ctx.stroke();
  21. ctx.globalAlpha = 1.0;
  22. ctx.font = testFont;
  23. if (rotation != 0) {
  24. ctx.translate(x,y);
  25. ctx.rotate(rotation);
  26. ctx.translate(-x,-y);
  27. }
  28. if (baseline != '') {
  29. ctx.textBaseline = baseline;
  30. }
  31. ctx.fillText(text, x, y);
  32. }
  33. // Testcase: vertical text with various textBaselines
  34. // test(100, 50, 'Top', 'writing-mode:vertical-lr', 0, 'top');
  35. // test(150, 50, 'Middle', 'writing-mode:vertical-lr', 0, 'middle');
  36. // test(200, 50, 'Alphabetic', 'writing-mode:vertical-lr', 0, 'alphabetic');
  37. // test(250, 50, 'Bottom', 'writing-mode:vertical-lr', 0, 'bottom');
  38. // Reference: horizontal text with 90° rotation and the same baselines
  39. test(100, 50, 'Top', 'writing-mode:horizontal-tb', Math.PI/2, 'top');
  40. test(150, 50, 'Middle', 'writing-mode:horizontal-tb', Math.PI/2, 'middle');
  41. test(200, 50, 'Alphabetic', 'writing-mode:horizontal-tb', Math.PI/2, 'alphabetic');
  42. test(250, 50, 'Bottom', 'writing-mode:horizontal-tb', Math.PI/2, 'bottom');
  43. </script>