You are here: Reference > JavaScript > client-side > box and rectangle > objects > TextRectangles

TextRectangles collection

Browser support:
39.54
Represents a collection of TextRectangle objects that specifies the exact shape of an element or TextRange object.
The TextRectangles collection is supported in Firefox from version 3, in Opera from version 9.5 and in Safari from version 4. Use the offsetLeft, offsetTop, offsetWidth and offsetHeight properties in older versions of Firefox, Opera and Safari.
Each TextRectangle object specifies the bounding rectangle of a single line in pixels, relative to the upper-left corner of the browser's client area. The bounding rectangles include the padding, scrollBar, and the border, but excludes the margin.
Note: Since the shape of a block element is always rectangular, the TextRectangles collection contains only one TextRectangle object for block elements in Firefox, Opera, Google Chrome and Safari. This TextRectangle object specifies the bounding rectangle of the block element.
When the width of a block element is specified, then the TextRectangles collection contains only one TextRectangle object in Internet Explorer, too.
See Example 2 and 3 for details.
Note: Sometimes a single line is represented by more than one TextRectangle object in Internet Explorer. Example 1 illustrates it.
If you need the bounding rectangle of the entire element, use the getBoundingClientRect method.

Syntax:

Methods that return the object:
object.getClientRects ( )
The base interface, through which you can add new functionalities to the TextRectangles collection, is the ClientRectList interface.

Possible members:

Properties:
length
Returns an integer that specifies the number of objects in the current collection.

This property is read-only.
Methods:
[index]
Returns an object from the current collection by index.

Parameters:

index
Required. Zero-based integer that specifies the position of object to retrieve.

Return value:

Returns a TextRectangle object.
item (index)
Returns an object from the current collection by index.

Parameters:

index
Required. Zero-based integer that specifies the position of object to retrieve.

Return value:

Returns a TextRectangle object.

Example HTML code 1:

This example illustrates the use of the TextRectangles collection for an inline element:
<head>
    <script type="text/javascript">
        function GetShape () {
            var shapeTestObj = document.getElementById ("shapeTest");
            var message = "";
            if (shapeTestObj.getClientRects) {        // Internet Explorer, Firefox 3+, Google Chrome, Opera 9.5+, Safari 4+
                var rects = shapeTestObj.getClientRects ();
                for (i = 0; i < rects.length; i++) {
                    var rect = rects[i];
                    message += "top: " + rect.top + 
                               ", bottom: " + rect.bottom + 
                               ", left: " + rect.left + 
                               ", right: " + rect.right + "<br />";
                }
            }
            else {
                message = "Your browser does not support this example!";
            }

            var info = document.getElementById ("info");
            info.innerHTML = message;
        }
    </script>
</head>
<body onload="GetShape ()">
    <span id="shapeTest">
        You can see the exact shape of this element in the green field below.<br />
        This line is represented by two TextRectangle objects in Internet Explorer.
    </span>
    <br /><br /><br />
    <div id="info" style="background-color:#c0e0b0;"></div>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example illustrates the use of the TextRectangles collection for a block element:
<head>
    <script type="text/javascript">
        function GetShape () {
            var shapeTestObj = document.getElementById ("shapeTest");
            var message = "";
            if (shapeTestObj.getClientRects) {        // Internet Explorer, Firefox 3+, Google Chrome, Opera 9.5+, Safari 4+
                var rects = shapeTestObj.getClientRects ();
                for (i = 0; i < rects.length; i++) {
                    var rect = rects[i];
                    message += "top: " + rect.top + 
                               ", bottom: " + rect.bottom + 
                               ", left: " + rect.left + 
                               ", right: " + rect.right + "<br />";
                }
            }
            else {
                message = "Your browser does not support this example!";
            }

            var info = document.getElementById ("info");
            info.innerHTML = message;
        }
    </script>
</head>
<body onload="GetShape ()">
    <div id="shapeTest">
        You can see the exact shape of this element in the green field below.<br />
        The TextRectangles collection, returned by the getClientRects method, 
        contains only one TextRectangle object in Firefox, Opera, Google Chrome and Safari.<br />
        This object represents the bounding rectangle of the entire element.
    </div>
    <br /><br /><br />
    <div id="info" style="background-color:#c0e0b0;"></div>
</body>
Did you find this example helpful? yes no

Example HTML code 3:

This example illustrates the use of the TextRectangles collection for a block element with specified width:
<head>
    <script type="text/javascript">
        function GetShape () {
            var shapeTestObj = document.getElementById ("shapeTest");
            var message = "";
            if (shapeTestObj.getClientRects) {        // Internet Explorer, Firefox 3+, Google Chrome, Opera 9.5+, Safari 4+
                var rects = shapeTestObj.getClientRects ();
                for (i = 0; i < rects.length; i++) {
                    var rect = rects[i];
                    message += "top: " + rect.top + 
                               ", bottom: " + rect.bottom + 
                               ", left: " + rect.left + 
                               ", right: " + rect.right + "<br />";
                }
            }
            else {
                message = "Your browser does not support this example!";
            }

            var info = document.getElementById ("info");
            info.innerHTML = message;
        }
    </script>
</head>
<body onload="GetShape ()">
    <div id="shapeTest" style="width:450px">
        You can see the exact shape of this element in the green field below.<br />
        The TextRectangles collection, returned by the getClientRects method, 
        contains only one TextRectangle object.<br />
        This object represents the bounding rectangle of the entire element.
    </div>
    <br /><br /><br />
    <div id="info" style="background-color:#c0e0b0;"></div>
</body>
Did you find this example helpful? yes no

External links:

User Contributed Comments

Post Content

Post Content