Skip to content

Instantly share code, notes, and snippets.

@zmmbreeze
Created September 7, 2013 14:48
Show Gist options
  • Save zmmbreeze/6476196 to your computer and use it in GitHub Desktop.
Save zmmbreeze/6476196 to your computer and use it in GitHub Desktop.
use `getClientRects` to draw inline box
function drawInlineBox(el) {
el = typeof el === 'string' ?
document.getElementById(el) :
el;
var lines = document.createElement('lines');
lines.style.display = 'inline';
var children = [];
for (var i = 0, l = el.childNodes.length; i < l; i++) {
children[i] = el.childNodes[i];
}
for (i = 0, l = children.length; i < l; i++) {
lines.appendChild(children[i]);
}
el.appendChild(lines);
var me = this;
var rects = el.childNodes[0].getClientRects();
var rect;
for (var m = 0, n = rects.length; m < n; m++) {
rect = rects[m];
var div = document.createElement('div');
div.style.position = 'absolute';
div.style.top = rect.top + 'px';
div.style.left = rect.left + 'px';
div.style.width = (rect.right - rect.left) + 'px';
div.style.height = (rect.bottom - rect.top) + 'px';
div.style.border = '1px solid red';
el.appendChild(div);
}
};
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>drawInlineBox</title>
</head>
<body>
<div id="result">
</div>
<div class="main">
<h1>Lining.js</h1>
<p id="demo">Inspired <img src="./assets/80x60.gif"/>by <span>our <em>deep</em>a</span> experience<img style="vertical-align:-100px;" src="./assets/80x60.gif"/> designing for the web, we’ve given Writer for Mac a responsive design, changing the font size based on window width. This maintains the text’s typographic proportions, zooming in and out without reflowing the text. I don’t know why it took us so long to find this obvious solution. However, given that no one else has done it, the simplicity of this solution is perhaps not as obvious as it seems in hindsight. With all the custom features built into Writer for Mac, like Focus Mode and Auto Markdown, technically it was not as easy as you’d expect. But we’ve taken the time to address these details, so that this new feature feels obvious and natural.</p>
<script src="drawInlineBox.js"></script>
<script>
drawInlineBox('demo');
</script>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment