Click to return to the DHTML, HTML     
TextRange Object     TFOOT Element | TFOOT Obj...     DHTML Objects    
Web Workshop  |  DHTML, HTML & CSS

TextRectangle Object


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

Remarks

Apply the getClientRects method to an element or text range object to retrieve a collection of TextRectangle objects. 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 following excerpt from the Gettysburg Address, four TextRectangle objects are contained in the B element (bold 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 four TextRectangle objects are:

  1. "our fathers"
  2. "brought forth . . . a new nation, conceived in liberty"
  3. "and dedicated to the proposition that all men are"
  4. "created equal"

If you resize the window containing this text, the TextRectangle objects do not update. Since the objects are a snapshot of the layout, the objects should update following after an onresize event occurs.

This object is available in script as of Microsoft® Internet Explorer 5.

Members

Example

This example uses the getClientRects and getBoundingClientRect methods to highlight text lines in an object.

Sample Code

<HEAD>
<SCRIPT>
var rcts;
var keyCount=0;

function Highlight(obj) {            
    rcts = obj.getClientRects();
    rctLength= rcts.length;

    if (keyCount > rctLength-1) {
        idBeige.style.display="none";
        keyCount = 0
    }

    // set the rendering properties for the yellow DIV
    cdRight = rcts[keyCount].right + idBody.scrollLeft;
    cdLeft = rcts[keyCount].left + idBody.scrollLeft;
    cdTop = rcts[keyCount].top + idBody.scrollTop;
    cdBottom = rcts[keyCount].bottom + idBody.scrollTop;
    idYellow.style.top = cdTop;
    idYellow.style.width = (cdRight-cdLeft) - 5;
    idYellow.style.display = 'inline';

    // set the rendering properties for the beige DIV
    bndRight = obj.getBoundingClientRect().right +
    idBody.scrollLeft;
    bndLeft = obj.getBoundingClientRect().left +
    idBody.scrollLeft;
    bndTop = obj.getBoundingClientRect().top +
    idBody.scrollTop;
    idBeige.style.top = bndTop;
    idBeige.style.width = (bndRight-bndLeft) - 5;
    idBeige.style.height = cdTop - bndTop;
    if (keyCount>0){
        idBeige.style.display = 'inline';
    }
    keyCount++;
}
</SCRIPT>
</HEAD>

<BODY ID="idBody">
<DIV ID="oID_1" onclick="Highlight(this)"
    onkeydown="Highlight(this)">
A large block of text should go here. Click on this
block of text multiple times to see each line
highlight with every click of the mouse button.
Once each line has been highlighted, the process
will begin again starting with the first line.
</DIV>
<DIV STYLE="position:absolute; left:5; top:400;
z-index:-1; background-color:yellow; display:none"
ID="idYellow"></DIV>
<DIV STYLE="position:absolute; left:5; top:400;
z-index:-1; background-color:beige; display:none"
ID="idBeige"></DIV>
</BODY>

This feature requires Internet Explorer 5 or later. Click the icon below to install the latest version. Then reload this page to view the sample.
Microsoft Internet Explorer

This example uses the TextRectangle collection with the getClientRects and getBoundingClientRect methods to determine the position of the text rectangle 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 this collection, you can determine the coordinates of the rectangle that surrounds only the content in each line. The example code reads these rectangle coordinates and instructs the ball to move over the text only, and not to the end of the line.

<HEAD>
<SCRIPT>
var timid = -1;
var timoID_2 = -1;
var nLine;
var nPosInLine;
var oRcts;
var nDivLen;
var nEraser;

function LoadDone() {
    oTextRange = document.body.createTextRange();              
    // Get bounding rect of the range
    oRcts = oTextRange.getClientRects();
    nLine = 0;
    oBndRct = obj.getBoundingClientRect();
    nDivLen = oBndRct.right - oBndRct.left + 1;    
    MoveTo();
}

function MoveTo() {
    if (nLine >= oRcts.length) {
    nLine = 0;
}
    obj.style.top = oRcts[nLine].top;
    nPosInLine = oRcts[nLine].left;
    nEraser = 0;
    timoID_2 = setInterval("MoveToInLine()",60);    
}

function MoveToInLine() {
    if (nPosInLine >= oRcts[nLine].right - nDivLen) {
        clearInterval(timoID_2);
        timoID_2 = -1;
        obj.style.left = oRcts[nLine].right - nDivLen;
        nLine++;
        timid = setTimeout("MoveTo()", 100);
        return;
    }
    if (nEraser == 0) {
    nEraser = 1;
}
    else {
    nEraser = 0;
}
im.src = "/workshop/graphics/dot.gif";
    obj.style.left = nPosInLine;
    nPosInLine += 3;
}

function End() {
    if(timid != -1) {
    clearInterval(timid);
        timid = -1;
}
    if(timoID_2 != -1) {
    clearInterval(timoID_2);
        timoID_2 = -1;
}
}
</SCRIPT>
</HEAD>

<BODY ID="bodyid" onload="LoadDone()"
    onresize="End();LoadDone();" onunload="End()">
<P STYLE="text-align:center">
<B>The quick brown fox jumps over the lazy dog.</B>
</P>
<DIV ID="obj" STYLE="position:absolute">
<IMG ID="im" SRC="/workshop/graphics/dot.gif"
    BORDER=0 HEIGHT=16 WIDTH=16>
</DIV>
</BODY>
This feature requires Internet Explorer 5 or later. Click the icon below to install the latest version. Then reload this page to view the sample.
Microsoft Internet Explorer


Back to topBack to top

Did you find this topic useful? Suggestions for other topics? Write us!

© 1999 Microsoft Corporation. All rights reserved. Terms of use.