var cursors = []; var worddivider = [' ', '-']; ibex.thread = function() { while (true) { ibex.sleep(1000); for (var i=0; cursors.length > i; i++) { cursors[i].blink = !cursors[i].blink; } } } // Returns the number of characters the pixel position pos is into text t. end is the pixel with of t. // t : string -- basis for character counting. // pxpos : int -- pixel position on the text from string t. // pxlen : int -- pixel width of text t (known in form of box length, so passed for speed). // tfont : string -- name of font used for width of text. var getpos = function(t, pxpos, pxlen, tfont) { // Short circuit extremes. if (0 >= pxpos) return 0; if (pxpos >= pxlen) return t.length; // Inital guess based on average character width. var guessch = ibex.math.min(t.length, ibex.math.floor(pxpos / (pxlen / t.length))); var guesspx = ibex.textwidth(tfont, t.substring(0, guessch)); if (guesspx > pxpos) { while (guesspx > pxpos) { // Textwidth of individual character must account for font kerning. guesspx -= ibex.textwidth(tfont, t.substring(guessch -1, guessch +1)) - ibex.textwidth(tfont, t.charAt(guessch)); guessch--; } } else if (pxpos > guesspx) { while (pxpos > guesspx) { guessch++; if (guessch >= t.length) break; guesspx += ibex.textwidth(tfont, t.substring(guessch -1, guessch+1)) - ibex.textwidth(tfont, t.charAt(guessch)); } guessch--; // Round down. } return guessch; }