Project

General

Profile

canvas_w_96px_square.html

Greg Shah, 08/27/2015 01:19 PM

Download (3.2 KB)

 
1
<!DOCTYPE html>
2
<html lang="en">
3
   <head>
4
      <meta charset="utf-8" />
5
      <title>Canvas Drawing Example</title>
6
      
7
      <script>
8
         var canvas;
9
         var ctx;
10
         
11
         var fontCanvas;
12
         var fontCtx;
13
         
14
         function textHeight(font, text, width)
15
         {
16
            var height = 128;
17
      
18
            fontCtx.save();
19
      
20
            fontCanvas.setAttribute('height', height);
21
            fontCanvas.setAttribute('width', width);
22
            fontCtx.fillStyle = 'black';
23
            fontCtx.fillRect(0, 0, width, height);
24
            fontCtx.textBaseline = 'top';
25
            fontCtx.fillStyle = 'white';
26
            fontCtx.font = font;
27
            fontCtx.fillText(text, 0, 0);
28
      
29
            var pixels = fontCtx.getImageData(0, 0, width, height).data;
30
      
31
            // row numbers where we first find letter end where it ends 
32
            var beg = -1;
33
            var end = -1;
34
      
35
            for (var row = 0; row < height; row++)
36
            {
37
               for (var col = 0; col < width; col++)
38
               {
39
                  var index = (row * width + col) * 4;
40
                  
41
                  // if pixel is not white (background color)
42
                  if (pixels[index] === 0)
43
                  {
44
                     // we haven't met white (font color) pixel
45
                     // on the row and the letters was detected
46
                     if (col === (width - 1) && beg !== -1)
47
                     {
48
                        end = row;
49
                        row = height;
50
                        break;
51
                     }
52
                  }
53
                  else
54
                  {
55
                     // we find top of letter
56
                     if (beg === -1)
57
                     {
58
                        beg = row;
59
                     }
60
                     // ..letters body
61
                     break;
62
                  }
63
               }
64
            }
65
      
66
            fontCtx.restore();
67
            
68
            return end - beg;
69
         }
70
         
71
         window.onload=function()
72
         {
73
            canvas = document.getElementById("bogus");
74
            ctx = canvas.getContext('2d');
75
            
76
            canvas.top = 150;
77
            canvas.left = 150;
78
            canvas.width = 700;
79
            canvas.height = 300;
80
            
81
            ctx.lineWidth = 1;
82
            ctx.imageSmoothingEnabled = false;
83
            ctx.translate(0.5, 0.5);
84
            
85
            ctx.fillRect(10, 10, 96, 96);
86
            
87
            var font = "96px sans-serif";
88
            var text = "WITM";
89
            
90
            ctx.font = font;
91
            ctx.textBaseline = "top";
92
            ctx.fillText(text, 130, 10);
93
            
94
            fontCanvas = document.createElement('canvas');
95
            fontCtx = fontCanvas.getContext('2d');
96
            var width = ctx.measureText(text).width;
97
            var ht = textHeight(font, text, width);
98
            
99
            ctx.font = "16px sans-serif";
100
            ctx.fillText("textHeight = " + ht + "px", 130 + 30 + width, 53);  
101
         };
102
      </script>
103
   </head>
104
   <body>
105
      <canvas id="bogus" style="position:'absolute';">No HTML5 support in your browser!</canvas>
106
   </body>
107
</html>