Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 697407 of TextMetrics

  • Revision slug: Web/API/TextMetrics
  • Revision title: TextMetrics
  • Revision id: 697407
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment

Revision Content

{{APIRef("Canvas")}}

The TextMetrics interface represents the dimension of a text in the canvas, as created by the {{domxref("CanvasRenderingContext2D.measureText()")}} method.

Properties

{{domxref("TextMetrics.width")}} {{readonlyInline}}
Is a double giving the calculated width of a segment of inline text in CSS pixels. It takes into account the current font of the context.
{{domxref("TextMetrics.actualBoundingBoxLeft")}} {{readonlyInline}}
Is a double giving the distance parallel to the baseline from the alignment point given by the {{domxref("CanvasRenderingContext2D.textAlign")}} property to the left side of the bounding rectangle of the given text, in CSS pixels.
{{domxref("TextMetrics.actualBoundingBoxRight")}} {{readonlyInline}}
Is a double giving the distance parallel to the baseline from the alignment point given by the {{domxref("CanvasRenderingContext2D.textAlign")}} property to the right side of the bounding rectangle of the given text, in CSS pixels.
{{domxref("TextMetrics.fontBoundingBoxAscent")}} {{readonlyInline}}
Is a double giving the distance from the horizontal line indicated by the {{domxref("CanvasRenderingContext2D.textBaseline")}} attribute to the top of the highest bounding rectangle of all the fonts used to render the text, in CSS pixels.
{{domxref("TextMetrics.fontBoundingBoxDescent")}} {{readonlyInline}}
Is a double giving the distance from the horizontal line indicated by the {{domxref("CanvasRenderingContext2D.textBaseline")}} attribute to the top of the bounding rectangle of all the fonts used to render the text, in CSS pixels.
{{domxref("TextMetrics.actualBoundingBoxAscent")}} {{readonlyInline}}
Is a double giving the distance from the horizontal line indicated by the {{domxref("CanvasRenderingContext2D.textBaseline")}} attribute to the top of the bounding rectangle used to render the text, in CSS pixels.
{{domxref("TextMetrics.actualBoundingBoxDescent")}} {{readonlyInline}}
Is a double giving the distance from the horizontal line indicated by the {{domxref("CanvasRenderingContext2D.textBaseline")}} attribute to the bottom of the bounding rectangle used to render the text, in CSS pixels.
{{domxref("TextMetrics.emHeightAscent")}} {{readonlyInline}}
Is a double giving the distance from the horizontal line indicated by the {{domxref("CanvasRenderingContext2D.textBaseline")}} property to the top of the em square in the line box, in CSS pixels.
{{domxref("TextMetrics.emHeightDescent")}} {{readonlyInline}}
Is a double giving the distance from the horizontal line indicated by the {{domxref("CanvasRenderingContext2D.textBaseline")}} property to the bottom of the em square in the line box, in CSS pixels.
{{domxref("TextMetrics.hangingBaseline")}} {{readonlyInline}}
Is a double giving the distance from the horizontal line indicated by the {{domxref("CanvasRenderingContext2D.textBaseline")}} property to the hanging baseline of the line box, in CSS pixels.
{{domxref("TextMetrics.alphabeticBaseline")}} {{readonlyInline}}
Is a double giving the distance from the horizontal line indicated by the {{domxref("CanvasRenderingContext2D.textBaseline")}} property to the alphabetic baseline of the line box, in CSS pixels.
{{domxref("TextMetrics.ideographicBaseline")}} {{readonlyInline}}
Is a double giving the distance from the horizontal line indicated by the {{domxref("CanvasRenderingContext2D.textBaseline")}} property to the ideographic baseline of the line box, in CSS pixels.

Specifications

Specification Status Comment
{{SpecName('HTML WHATWG', "the-canvas-element.html#textmetrics", "TextMetrics")}} {{Spec2('HTML WHATWG')}}  

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 4.0 {{CompatGeckoDesktop("1.8")}} 9.0 9.0 3.1
width 4.0 {{CompatGeckoDesktop("1.8")}} 9.0 9.0 3.1
actualBoundingBoxLeft,
actualBoundingBoxRight,
fontBoundingBoxAscent,
fontBoundingBoxDescent,
actualBoundingBoxAscent,
actualBoundingBoxDescent,
emHeightAscent,
emHeightDescent,
hangingBaseline,
alphabeticBaseline,
ideographicBaseline
{{CompatVersionUnknown}} [1] {{CompatNo}}
{{bug(1102584)}}
{{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 2.1 {{CompatGeckoMobile("1.8")}} {{CompatUnknown}} 10.0 3.2
width 2.1 {{CompatGeckoMobile("1.8")}} {{CompatUnknown}} 10.0 3.2
actualBoundingBoxLeft,
actualBoundingBoxRight,
fontBoundingBoxAscent,
fontBoundingBoxDescent,
actualBoundingBoxAscent,
actualBoundingBoxDescent,
emHeightAscent,
emHeightDescent,
hangingBaseline,
alphabeticBaseline,
ideographicBaseline
{{CompatUnknown}} {{CompatNo}}
{{bug(1102584)}}
{{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

Chrome-specific notes

  • To turn on advanced text metrics, set the flag ExperimentalCanvasFeatures to true.

See also

  • Creator method in {{domxref("CanvasRenderingContext2D")}}.
  • The {{HTMLElement("canvas")}} element and its associated interface, {{domxref("HTMLCanvasElement")}}

Revision Source

<div>
 {{APIRef("Canvas")}}</div>
<p>The <strong><code>TextMetrics</code></strong> interface represents the dimension of a text in the canvas, as created by the {{domxref("CanvasRenderingContext2D.measureText()")}} method.</p>
<h2 id="Properties">Properties</h2>
<dl>
 <dt>
  {{domxref("TextMetrics.width")}} {{readonlyInline}}</dt>
 <dd>
  Is a <code>double</code> giving the calculated width of a segment of inline text in CSS&nbsp;pixels. It takes into account the current font of the context.</dd>
 <dt>
  {{domxref("TextMetrics.actualBoundingBoxLeft")}} {{readonlyInline}}</dt>
 <dd>
  Is a <code>double</code> giving the distance parallel to the baseline from the alignment point given by the {{domxref("CanvasRenderingContext2D.textAlign")}} property to the left side of the bounding rectangle of the given text, in CSS pixels.</dd>
 <dt>
  {{domxref("TextMetrics.actualBoundingBoxRight")}} {{readonlyInline}}</dt>
 <dd>
  Is a <code>double</code> giving the distance parallel to the baseline from the alignment point given by the {{domxref("CanvasRenderingContext2D.textAlign")}} property to the right side of the bounding rectangle of the given text, in CSS pixels.</dd>
 <dt>
  {{domxref("TextMetrics.fontBoundingBoxAscent")}} {{readonlyInline}}</dt>
 <dd>
  Is a <code>double</code> giving the distance from the horizontal line indicated by the {{domxref("CanvasRenderingContext2D.textBaseline")}} attribute to the top of the highest bounding rectangle of all the fonts used to render the text, in CSS pixels.</dd>
 <dt>
  {{domxref("TextMetrics.fontBoundingBoxDescent")}} {{readonlyInline}}</dt>
 <dd>
  Is a <code>double</code> giving the distance from the horizontal line indicated by the {{domxref("CanvasRenderingContext2D.textBaseline")}} attribute to the top of the bounding rectangle of all the fonts used to render the text, in CSS pixels.</dd>
 <dt>
  {{domxref("TextMetrics.actualBoundingBoxAscent")}} {{readonlyInline}}</dt>
 <dd>
  Is a <code>double</code> giving the distance from the horizontal line indicated by the {{domxref("CanvasRenderingContext2D.textBaseline")}} attribute to the top of the bounding rectangle used to render the text, in CSS pixels.</dd>
 <dt>
  {{domxref("TextMetrics.actualBoundingBoxDescent")}} {{readonlyInline}}</dt>
 <dd>
  Is a <code>double</code> giving the distance from the horizontal line indicated by the {{domxref("CanvasRenderingContext2D.textBaseline")}} attribute to the bottom of the bounding rectangle used to render the text, in CSS pixels.</dd>
 <dt>
  {{domxref("TextMetrics.emHeightAscent")}} {{readonlyInline}}</dt>
 <dd>
  Is a <code>double</code> giving the distance from the horizontal line indicated by the {{domxref("CanvasRenderingContext2D.textBaseline")}} property to the top of the <em>em</em> square in the line box, in CSS pixels.</dd>
 <dt>
  {{domxref("TextMetrics.emHeightDescent")}} {{readonlyInline}}</dt>
 <dd>
  Is a <code>double</code> giving the distance from the horizontal line indicated by the {{domxref("CanvasRenderingContext2D.textBaseline")}} property to the bottom of the <em>em</em> square in the line box, in CSS pixels.</dd>
 <dt>
  {{domxref("TextMetrics.hangingBaseline")}} {{readonlyInline}}</dt>
 <dd>
  Is a <code>double</code> giving the distance from the horizontal line indicated by the {{domxref("CanvasRenderingContext2D.textBaseline")}} property to the hanging baseline of the line box, in CSS pixels.</dd>
 <dt>
  {{domxref("TextMetrics.alphabeticBaseline")}} {{readonlyInline}}</dt>
 <dd>
  Is a <code>double</code> giving the distance from the horizontal line indicated by the {{domxref("CanvasRenderingContext2D.textBaseline")}} property to the alphabetic baseline of the line box, in CSS pixels.</dd>
 <dt>
  {{domxref("TextMetrics.ideographicBaseline")}} {{readonlyInline}}</dt>
 <dd>
  Is a <code>double</code> giving the distance from the horizontal line indicated by the {{domxref("CanvasRenderingContext2D.textBaseline")}} property to the ideographic baseline of the line box, in CSS pixels.</dd>
</dl>
<h2 id="Specifications" name="Specifications">Specifications</h2>
<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('HTML WHATWG', "the-canvas-element.html#textmetrics", "TextMetrics")}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<div>
 {{CompatibilityTable}}</div>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>4.0</td>
    <td>{{CompatGeckoDesktop("1.8")}}</td>
    <td>9.0</td>
    <td>9.0</td>
    <td>3.1</td>
   </tr>
   <tr>
    <td><code>width</code></td>
    <td>4.0</td>
    <td>{{CompatGeckoDesktop("1.8")}}</td>
    <td>9.0</td>
    <td>9.0</td>
    <td>3.1</td>
   </tr>
   <tr>
    <td><code>actualBoundingBoxLeft</code>,<br />
     <code>actualBoundingBoxRight</code>,<br />
     <code>fontBoundingBoxAscent</code>,<br />
     <code>fontBoundingBoxDescent</code>,<br />
     <code>actualBoundingBoxAscent</code>,<br />
     <code>actualBoundingBoxDescent</code>,<br />
     <code>emHeightAscent</code>,<br />
     <code>emHeightDescent</code>,<br />
     <code>hangingBaseline</code>,<br />
     <code>alphabeticBaseline</code>,<br />
     <code>ideographicBaseline</code></td>
    <td>{{CompatVersionUnknown}} [1]</td>
    <td>{{CompatNo}}<br />
     {{bug(1102584)}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>2.1</td>
    <td>{{CompatGeckoMobile("1.8")}}</td>
    <td>{{CompatUnknown}}</td>
    <td>10.0</td>
    <td>3.2</td>
   </tr>
   <tr>
    <td><code>width</code></td>
    <td>2.1</td>
    <td>{{CompatGeckoMobile("1.8")}}</td>
    <td>{{CompatUnknown}}</td>
    <td>10.0</td>
    <td>3.2</td>
   </tr>
   <tr>
    <td><code>actualBoundingBoxLeft</code>,<br />
     <code>actualBoundingBoxRight</code>,<br />
     <code>fontBoundingBoxAscent</code>,<br />
     <code>fontBoundingBoxDescent</code>,<br />
     <code>actualBoundingBoxAscent</code>,<br />
     <code>actualBoundingBoxDescent</code>,<br />
     <code>emHeightAscent</code>,<br />
     <code>emHeightDescent</code>,<br />
     <code>hangingBaseline</code>,<br />
     <code>alphabeticBaseline</code>,<br />
     <code>ideographicBaseline</code></td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatNo}}<br />
     {{bug(1102584)}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
   </tr>
  </tbody>
 </table>
</div>
<h3 id="Chrome-specific_notes">Chrome-specific notes</h3>
<ul>
 <li>To turn on advanced text metrics, set the flag <code>ExperimentalCanvasFeatures</code> to <code>true</code>.</li>
</ul>
<h2 id="See_also">See also</h2>
<ul>
 <li>Creator method in {{domxref("CanvasRenderingContext2D")}}.</li>
 <li>The {{HTMLElement("canvas")}} element and its associated interface, {{domxref("HTMLCanvasElement")}}</li>
</ul>
Revert to this revision