How to retrieve offsetWidth/clientWidth of an element

199 views
Skip to first unread message

Tom

unread,
Mar 14, 2012, 5:24:28 AM3/14/12
to General Dart Discussion
How to retrieve offsetWidth/clientWidth of an element? It seems to be
Element.rect but not sure. Thanks.

financeCoding

unread,
Mar 14, 2012, 11:21:16 PM3/14/12
to mi...@dartlang.org
Hi Tom, 

Element.rect returns a Future http://goo.gl/KTgkY , you can access http://goo.gl/lPZC1 in the following way http://goo.gl/O7bWF . Also check out Seth Ladd post about Futures http://goo.gl/JSSXP

Kind Regards,
Adam 

Tom

unread,
Mar 15, 2012, 10:57:01 PM3/15/12
to General Dart Discussion
Hi Adam,

Thanks.

It looks complication than it shall be. Any idea why not as simple as
element.offsetWidth?

On Mar 15, 11:21 am, financeCoding <financecod...@gmail.com> wrote:
> Hi Tom,
>
> Element.rect returns a Futurehttp://goo.gl/KTgkY, you can accesshttp://goo.gl/lPZC1in the following wayhttp://goo.gl/O7bWF. Also check
> out Seth Ladd post about Futureshttp://goo.gl/JSSXP

Seth Ladd

unread,
Mar 15, 2012, 11:42:11 PM3/15/12
to Tom, General Dart Discussion
Hi Tom,


Using a Future helps with web app performance by reducing synchronous code in the face of potentially expensive calls to the browser.

Seth

Tom

unread,
Mar 16, 2012, 1:02:23 AM3/16/12
to General Dart Discussion
Sure, it is expensive but it's developer's call whether and when to
use, isn't it?

On Mar 16, 11:42 am, Seth Ladd <sethl...@google.com> wrote:
> Hi Tom,
>
> offsetWidth can cause an expensive reflow:http://www.nczonline.net/blog/2009/02/03/speed-up-your-javascript-par...

Bob Nystrom

unread,
Mar 16, 2012, 1:49:02 PM3/16/12
to Tom, General Dart Discussion
On Thu, Mar 15, 2012 at 10:02 PM, Tom <tom....@gmail.com> wrote:
Sure, it is expensive but it's developer's call whether and when to
use, isn't it?

Good point. Trying to discourage developers from doing something that will hose their performance without them realizing it is nice, but completely preventing them from writing code they way they want is possibly going too far. This is something we've been discussing for a while. Here's another thread with some more context:


Cheers!

- bob

Jay Young

unread,
Mar 18, 2012, 12:04:50 PM3/18/12
to mi...@dartlang.org, Tom
How does that work under the covers?  The underlying library is still reading node.style.offsetWidth, which is a synchronous operation.  How does using a future prevent the browser from stopping the world to do reflows/repaints and calculations?  This seems like a synchronous operation unnecessarily wrapped in an async control flow (unless I misunderstand something, which is totally possible).

Jay Young

unread,
Mar 18, 2012, 12:06:07 PM3/18/12
to mi...@dartlang.org, Tom
On Sunday, March 18, 2012 12:04:50 PM UTC-4, Jay Young wrote:
...  The underlying library is still reading node.style.offsetWidth ...

Sorry, meant "element.offsetWidth".

Justin Fagnani

unread,
Mar 19, 2012, 1:44:05 AM3/19/12
to Jay Young, mi...@dartlang.org, Tom
You can check out the source yourself, it's in html_frog.dart which is now conveniently shown in the editor (I love that feature).

The implementation adds each measurement request to a queue and evaluates all the measurements at the end of the current event loop. So while the calls to element.offset* are synchronous and do trigger a layout, all the calls generated by Element.rect() are batched together.

-Justin

Jay Young

unread,
Mar 19, 2012, 1:50:15 AM3/19/12
to mi...@dartlang.org, Jay Young, Tom
Ah hah.  Brilliant!  I tried looking for the implementation but couldn't find it (using an old version of the editor, I guess).  Thanks.
Reply all
Reply to author
Forward
0 new messages