asp.net.ph

Skip Navigation Links

TextRectangle Object

Language References


HTML 2, 3.2, 4, 4.01, 5

Specifies a rectangle that contains a line of text in an element or TextRange object.

Remarks

A collection of TextRectangle objects is retrieved by applying the getClientRects method to an element or text range object. The getClientRects method returns a collection of rectangles, exposing for each rectangle the left, top, right, and bottom coordinates relative to the client.

In the Gettysburg Address below, four TextRectangle objects are contained in the B object ( bolded text ).

Four score and seven years ago our fathers
brought forth . . . a new nation, conceived in liberty
and dedicated to the proposition that all men are
created equal.
Now we are engaged in a great civil war . . .

The rectangle objects are:

  • The rectangle around "our fathers"
  • The rectangle around "brought forth . . . a new nation, conceived in liberty"
  • The rectangle around "and dedicated to the proposition that all men are"
  • The rectangle around "created equal"

Note that if the window is resized, the TextRectangle objects are not updated. Since the objects are a "snapshot" of the layout, the objects should be updated following an onresize event.

Members

TextRectangle Members

Example

The example below demonstrates the use of the getClientRects and getBoundingRect methods to highlight text lines in an object.

Sample Code

The next example shows how the TextRectangle collection and getBoundingClientRect method can be harnessed to determine position within an element.

In each line, the left-justified text does not extend to the right margin of the box that contains the text. Using the collection, it is possible to determine the coordinates of the rectangle that surrounds only the content in each line.

By reading these rectangles and then comparing the coordinates against the rectangle around the ball image, the sample instructs the ball to move over the text only and not to the end of the line.

Sample Code

Note that the timers are reset and the loaded functions are fired for each resize.

<body . . . onresize="end ( ); loadDone ( )" ... >
See Also

getBoundingClientRect, getClientRects, TextRectangle Collection



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note