Skip to content

Commit fb4914d

Browse files
committed
Merge pull request #25 from WPMedia/master
Simplified changes to fix issues with aspect ratio not being set
2 parents e7f5505 + 4d9061b commit fb4914d

File tree

3 files changed

+88
-5
lines changed

3 files changed

+88
-5
lines changed

demos/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ <h3>
123123
how foresight.js is updated after page transitions.
124124
</p>
125125

126+
<h3>
127+
<a href="parent-element-known-dimension-and-aspect-ratio.html">Demo 12: Parent Known Dimension and Aspect Ratio</a>
128+
</h3>
129+
<p>
130+
This demo shows image-set() CSS can work with pixel css width and an known aspect ratio.
131+
Obviously this will not allow you to specify the height on the URL for dynamic images so
132+
you should set the aspect ratio not to auto.
133+
</p>
134+
126135

127136
<h3>
128137
<a href="resolve-method.html">foresight.resolve() Public Method</a>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<!doctype html>
2+
<head>
3+
<title>Foresight.js Demo: With Aspect Ratio and Only One Dimension Known from Parent</title>
4+
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
5+
<link href="demo.css" type="text/css" rel="stylesheet" />
6+
7+
<style>
8+
article {
9+
float:left;
10+
}
11+
aside {
12+
float:right;
13+
width:300px;
14+
}
15+
16+
.fs-img {
17+
width:100%;
18+
font-family: 'image-set( url(dynamic-images/|dynamic-images/{requestWidth}x{requestHeight}/) )';
19+
display: inline-block;
20+
border-left: 4px solid yellow;
21+
}
22+
23+
</style>
24+
25+
<script src="foresight-debugger.js"></script>
26+
<script src="../foresight.js"></script>
27+
28+
</head>
29+
<body>
30+
<section>
31+
32+
<h2>
33+
Demo: With Aspect Ratio and Only One Dimension Known from Parent
34+
</h2>
35+
36+
<article>
37+
<p>
38+
This demo shows image-set() CSS can work with pixel css width and an known aspect ratio.
39+
Obviously this will not allow you to specify the height on the URL for dynamic images so your
40+
server side component should determine the aspect ratio or you should set the aspect ratio not to auto.
41+
</p>
42+
<p>
43+
The primary content on the left side take of the available width.
44+
The content on the right side containing the image takes 300px of the available width.
45+
</p>
46+
<p>
47+
Adjusting the width of the browser will request various static images.
48+
</p>
49+
</article>
50+
51+
<aside>
52+
<img data-src="http://foresightjs.appspot.com/dynamic-images/dew-on-grass.jpg" data-aspect-ratio="1" class="fs-img">
53+
<noscript>
54+
<img src="http://foresightjs.appspot.com/dynamic-images/dew-on-grass.jpg">
55+
</noscript>
56+
<p>
57+
Photo by <a href="http://commons.wikimedia.org/wiki/File:Dew_on_grass_Luc_Viatour.jpg">Luc Viatour</a>
58+
</p>
59+
</aside>
60+
61+
</section>
62+
63+
64+
<hr style="clear:both">
65+
66+
67+
<ul>
68+
<li><a href="index.html">Demo Homepage</a>
69+
<li><a href="https://github.com/adamdbradley/foresight.js">Foresight.js Homepage</a>
70+
</ul>
71+
72+
</body>
73+
</html>

foresight.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
TRUE = true,
5858
FALSE = false,
5959
imageSetItemRegex = /url\((?:([a-zA-Z-_0-9{}\?=&\\/.:\s]+)|([a-zA-Z-_0-9{}\?=&\\/.:\s]+)\|([a-zA-Z-_0-9{}\?=&\\/.:\s]+))\)/g,
60+
STRIP_UNITS_REGEX = /[^\d]+/,
6061

6162
// used to keep track of the progress status for finding foresight
6263
// images in the DOM and connection test results
@@ -233,7 +234,7 @@
233234
getDataAttribute = function ( img, attribute, getInt, value ) {
234235
// get an <img> element's data- attribute value
235236
value = img.getAttribute( 'data-' + attribute );
236-
if ( getInt ) {
237+
if ( getInt && value!==null) {
237238
if ( !isNaN( value ) ) {
238239
return parseInt( value, 10 );
239240
}
@@ -510,12 +511,12 @@
510511
// If the aspect ratio is set then we will get the other value off the
511512
// aspect ratio
512513
if( img[ ASPECT_RATIO ] ) {
513-
if( img[ WIDTH_UNITS ] ){
514-
img[ BROWSER_WIDTH ] = img[ WIDTH_UNITS ];
515-
img[ BROWSER_HEIGHT ] = Math.round( img[ WIDTH_UNITS ] / img[ ASPECT_RATIO ] );
516-
} else if( img[ HEIGHT_UNITS ] ){
514+
if( img[ HEIGHT_UNITS ] ){
517515
img[ BROWSER_WIDTH ] = Math.round( img[ HEIGHT_UNTIS ] / img[ ASPECT_RATIO ] );
518516
img[ BROWSER_HEIGHT ] = img[ HEIGHT_UNITS ];
517+
} else {
518+
img[ BROWSER_WIDTH ] = img[ WIDTH_UNITS ] || computedWidthValue.replace(STRIP_UNITS_REGEX, "");
519+
img[ BROWSER_HEIGHT ] = Math.round( img[ BROWSER_WIDTH ] / img[ ASPECT_RATIO ] );
519520
}
520521
} else {
521522
img[ BROWSER_WIDTH ] = img[ WIDTH_UNITS ];

0 commit comments

Comments
 (0)