getting computed clip rect

S

SAM

Chris Riesbeck a écrit :
Not from what I've tried so far, as documented in previous postings and
this demo file:

If I have a clip style for images in the head
and another clip style inline for one image, this inline style wins
(Fx, Safari)

Try on this page :
javascript:document.images[0].style.clip='rect(50px 120px 120px 50px)';
That works in my Safary
but not in my Fx (don't know why)
No, I just need an initial default clip rect -- really just it's width
and height -- that JS can use. See the magnifier demo:

Yes but you want so much (too much ?).
(style in a class, class in an external style sheet)

I've something with same effect (not from me) archived somewhere in my DD.
 
G

GTalbot

I simplified the demo file to just have the image, stylesheet, and text
showing the Javascript one-liners that don't work for me in IE and
Safari. Still at

http://www.cs.northwestern.edu/~riesbeck/demo/clip-demo.html

javascript:alert(document.images[0].style.clip)

javascript:alert(document.images[0].runtimeStyle.clip)

Those 2 empty alert must mean there is absence of support, which is
contrary to what MSDN claims. So, you can safely claim that there is a
bug here... or at the minimum an error at MSDN's
http://msdn.microsoft.com/en-us/library/ms533569(VS.85).aspx
Thanks. I thought there was some way to get to the stylesheets
themselves but failed to find the relevant W3C pages when searching
yesterday.

Since my pages will appear in contexts with many other stylesheets, and
I won't know which one my rule is in, I have to search the selector
texts in all the rules in all the stylesheets.

No. No need for selector text.

document.stylesheets[0]
refers to the first stylesheet.
You should place imported stylesheets first, then external (with <link
rel="stylesheet" href="...">) stylesheets, then local (embedded within
<style type="text/css"> ... </style>) stylesheet next. Ideally, the
order of stylesheet should go from very far to very close to the
document itself. Since we are talking about IE, you may actually have
to test, verify all this.

Best would be to avoid imported stylesheets in your case.

Regarding CSS rules, then it's in their order of appearance in the
stylesheet.

E.g.:
document.stylesheets[3].rules[5].style.borderWidth
should refer to the 4th declared stylesheets, its 6th rule and where
the border-width is defined, specified.

It works in IE 6+ (I've used it in a few DHTML webpages) ... but you
have to use rules[] for IE, not cssRules[].

By the way, you never answered if the following code worked for you in
your (previous) original webpage:

if(document.styleSheets)
{
if("cssRules" in document.styleSheets[0]) // DOM 2 Stylesheets
compliant
{
alert("document.styleSheets[0].cssRules[0].style.clip = " +
document.styleSheets[0].cssRules[0].style.clip);
}
else if("rules" in document.styleSheets[0]) // IE 6+
{
alert("document.styleSheets[0].rules[0].style.clip = " +
document.styleSheets[0].rules[0].style.clip);
};
}
else
{
alert("This script requires support for accessing the list of
stylesheets. Javascript support enabled is required too.");
};

Regards, Gérard
 
G

GTalbot

(...) You don't try to use an existing clip rect created by a class
style rule.

The actual application I'm doing is yet another "magnifier." The current
demo is here

http://www.cs.northwestern.edu/~riesbeck/demo/magnifier_v2/magnifier....


Ok. Well, from that JS magnifier demo webpage, I would access (get)
the clip declaration like this:

document.styleSheets[0].rules[2].style.clip

as this would access the
magnifier.css
file, then its 3rd rule and its clip declaration when IE would be
involved.


[addendum]
I tried

javascript:alert("document.styleSheets[0].rules[2].style.clip = " +
document.styleSheets[0].rules[2].style.clip);

in your magnifier webpage in IE 7 and it worked! It returned the
correct declarative values for the clip declaration.
[/addendum]

You could even change dynamically the clip declaration at run-time in
IE 7 also like this

document.styleSheets[0].rules[2].style.clip = "rect(12px 34px 56px
78px)";

but doing such should not be done when in intensive, very demanding
DHTML action like my dynamic clip property demo.

Regards, Gérard
 
D

dhtml

Did you read this?
My problem is getting the data, not constructing a new rect.

What data? I thought you were trying to get the "clip" value.
I didn't follow this part and how it might help. Are you saying a
class-defined clip rect on a DIV would be accessible to Javascript where
the same thing on an IMG would not be?

No, it is not.

The idea was to use a different approach. If you use the container
'clipLayer' approach, you don't have to use 'clip' at all.
http://www.w3.org/TR/CSS21/visufx.html#overflow

#clipLayer {
height: 100px;
width: 100px;
position: relative;
overflow: hidden;
}

#clipLayer img {
width: 120px;
height: 200px;
}


<div id='clipLayer'><img src="yiyoi.jpg"/></div>

The img will be partially hidden by its containing block.
 
D

dhtml

Ok. Well, from that JS magnifier demo webpage, I would access (get)
the clip declaration like this:

document.styleSheets[0].rules[2].style.clip

as this would access the
magnifier.css
file, then its 3rd rule and its clip declaration when IE would be
involved.

[addendum]
I tried

javascript:alert("document.styleSheets[0].rules[2].style.clip = " +
document.styleSheets[0].rules[2].style.clip);

I see a few problems with that approach:

1) can't read style attribute (which is more specific)
2) can't get specificity of rule, compared to other rules that may
override.
3) Requires that the style rule appear in a specific order in the
stylesheet. This rigidity creates a very fragile dependency. One
change to the stylesheet can break it.

Why do you feel this is better advice then using the clipTop and
friends?

Garrett
 
S

SAM

GTalbot a écrit :
if(document.styleSheets)
{
if("cssRules" in document.styleSheets[0]) // DOM 2 Stylesheets
compliant
{
alert("document.styleSheets[0].cssRules[0].style.clip = " +
document.styleSheets[0].cssRules[0].style.clip);
}
else if("rules" in document.styleSheets[0]) // IE 6+
{
alert("document.styleSheets[0].rules[0].style.clip = " +
document.styleSheets[0].rules[0].style.clip);
};
}
else
{
alert("This script requires support for accessing the list of
stylesheets. Javascript support enabled is required too.");
};

Thanks a lot for this code.

Firefox, Safari.3, iCab.4 : OK
Opera.9, iCab.3 : message 'requires support'
IE 5.2 (Mac) : JS error code
--> if("cssRules" in document.styleSheets[0])

Working with IE Mac :

if(document.styleSheets)
{
var d = document.styleSheets[0];
if(d.rules)
{
for ( var i=0, n = d.rules.length; i<n; i++)
{
if(d.rules)
{
alert('document.styleSheets[0].rules['+i+'].style.clip = ' +
document.styleSheets[0].rules.style.clip);
};
}
}
else
if(d.cssRules)
{
for ( var i=0, n = d.cssRules.length; i<n; i++)
{
if(d.cssRules)
{
alert("document.styleSheets[0].cssRules["+i+"].style.clip = "+
document.styleSheets[0].cssRules.style.clip);
}
}
}
}
else
{
alert("This script requires support for accessing the list of " +
"stylesheets. Javascript support enabled is required too.");
};
}


And now, what to do for Opera 9 ?
 
C

Chris Riesbeck

dhtml said:
Ok. Well, from that JS magnifier demo webpage, I would access (get)
the clip declaration like this:

document.styleSheets[0].rules[2].style.clip


Why do you feel this is better advice then using the clipTop and
friends?

I don't know about Gerard but I'd given up on clipTop and friends when
they returned nothing in IE on style and runtimeStyle. But you're
correct that I do get valid values when they're applied to currentStyle.

javascript:alert(document.images[0].currentStyle.clipTop)

etc on the demo page

http://www.cs.northwestern.edu/~riesbeck/demo/clip-demo.html

So that would leave just Safari of the browsers I've tried (FF, IE,
Opera) needing to access the stylesheets.
 
C

Chris Riesbeck

dhtml said:
Did you read this?

Yes, after Gerard pointed me to it. That's why the demo page tries
style, currentStyle, and runtimeStyle, with no luck

Which is why I'm confused (though happy) that clipTop et al. work on
currentStyle, not runtimeStyle. Seems inconsistent.
No, it is not.

The idea was to use a different approach. If you use the container
'clipLayer' approach, you don't have to use 'clip' at all.
http://www.w3.org/TR/CSS21/visufx.html#overflow

#clipLayer {
height: 100px;
width: 100px;
position: relative;
overflow: hidden;
}

#clipLayer img {
width: 120px;
height: 200px;
}


<div id='clipLayer'><img src="yiyoi.jpg"/></div>

The img will be partially hidden by its containing block.

Ah. That looks like a more portable alternative. Will try it out. Thanks.
 
T

Thomas 'PointedEars' Lahn

Chris said:
Thomas said:
GTalbot said:
Chris Riesbeck wrote:
I have an image with a class and the class defines a clip
rectangle.

In Firefox 2 and 3, and Opera 9, I can access the rectangle with
document.defaultView.getComputedStyle().
[...] IE 7 and IE 8 final version will not support
document.defaultView nor getComputedStyle method.
How can you know this about IE 8 when it is still beta 1?
But that doesn't seem to work in Safari for Windows 3,
It works there. You must be doing something wrong.

I assume you're replying to me, not Gerald.

I replied to the both of you at the same time, with the quotation level
indicating to whose statement I am referring to. As it is accepted in
Usenet to save time when replying (or when one of the quoted authors has
been killfiled).

As you are using Thunderbird, I suggest you install and configure the Quote
Colors extension. It eases following discussions.
And I'd love to find out that I'm doing something wrong. As I've just
posted in this thread, the current demo of the problem is at

http://www.cs.northwestern.edu/~riesbeck/demo/clip-demo.html

and when I try executing

javascript:alert(document.defaultView.getComputedStyle(document.images[0],
null).clip)

in Safari (Windows and Mac) I get nothing. It works fine in Firefox and
Opera. I don't know to make the example any simpler.

According to the W3C Markup Validator[1] and CSS Validator[2], your HTML and
CSS code are still not Valid. You should fix that first and see if it helps.

[1]
<http://validator.w3.org/check?uri=h...ically)&doctype=Inline&group=0&ss=1&verbose=1>
[2]
Still? I hadn't noticed a previous comment about that.

Because you are obviously new to this newsgroup.
Recognized by what?

Most newsreader software.
I don't use Google Groups (except to search archives). I use Thunderbird
as my reader and News.Individual.Net as my server.

And that is good so. (Obviously this comment of mine was not made in
reference to you or your posting.)


PointedEars
 
D

dhtml

dhtml said:
Ok. Well, from that JS magnifier demo webpage, I would access (get)
the clip declaration like this:
document.styleSheets[0].rules[2].style.clip
Why do you feel this is better advice then using the clipTop and
friends?

I don't know about Gerard but I'd given up on clipTop and friends when
they returned nothing in IE on style and runtimeStyle. But you're
correct that I do get valid values when they're applied to currentStyle.

   javascript:alert(document.images[0].currentStyle.clipTop)

etc on the demo page

   http://www.cs.northwestern.edu/~riesbeck/demo/clip-demo.html

So that would leave just Safari of the browsers I've tried (FF, IE,
Opera) needing to access the stylesheets.

There is also getPropertyCSSValue, which should return a Rect, but
doesn't work (incorrectly returns null) in Current version Safari:
Version 3.1.1 (4525.18).
http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-Rect

javascript:
alert(document.defaultView.getComputedStyle(document.getElementById('spring'),
'').getPropertyCSSValue('clip'))

See also:
https://bugs.webkit.org/show_bug.cgi?id=17433
 
C

Chris Riesbeck

Chris said:
Ah. That looks like a more portable alternative. Will try it out. Thanks.

For those playing along, I have a version of the magnifier running using
dhtml's suggestion. Seems happy in FF, IE, Opera and Safari, thanks to
not using clip rectangles any more. That means no stylesheet searching,
though I still need to get the computed / current style.

http://www.cs.northwestern.edu/~riesbeck/demo/magnifier_v3/magnifier.html

Thanks to one and all.
 
L

Laurent vilday

Thomas 'PointedEars' Lahn a écrit :
I replied to the both of you at the same time, with the quotation level
indicating to whose statement I am referring to. As it is accepted in
Usenet to save time when replying (or when one of the quoted authors has
been killfiled).

LOL, you are so funny. Accepted or not (by whom by the way ?), replying
to 2 different people at the same time is a bad bad bad (did I said bad
?) practice. But I guess nothing apply to you, you, the ubber german genius.

And just don't answer to authors in your killfile.
Oh no I got a better one. Just don't answer to anyone.
PointedEars

Your signature is broken. You have been told millions times.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top