Skip to content

Commit cd89106

Browse files
committed
slideshow
1 parent c0318d4 commit cd89106

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed

examples/slideshow.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<title>smartcrop.js slideshow</title>
5+
<link rel=stylesheet href=style.css>
6+
</head>
7+
8+
<body>
9+
<h1>smartcrop.js slideshow</h1>
10+
<p><a href="https://github.com/jwagner/smartcrop.js">smartcrop.js</a> is a content aware image cropping library. On this page you can see how it can be
11+
used to automatically pick crops to create a <a href="http://en.wikipedia.org/wiki/Ken_Burns_effect">Ken Burns</a> effect on images.</p>
12+
<p>You can learn more about smartcrop.js on it's <a href="https://github.com/jwagner/smartcrop.js/">github page</a>.</p>
13+
14+
<div id=prev-slide></div>
15+
<div id=slide>loading, this can take a little while...</div>
16+
<script src=jquery.js></script>
17+
<script src=underscore.js></script>
18+
<script src="../smartcrop.js"></script>
19+
<script src=slideshow.js></script>
20+
<script>
21+
var _gaq = _gaq || [];
22+
_gaq.push(['_setAccount', 'UA-5205069-2']);
23+
_gaq.push(['_setDomainName', '29a.ch']);
24+
_gaq.push(['_setSiteSpeedSampleRate', 50]);
25+
_gaq.push(['_trackPageview']);
26+
27+
</script>
28+
<script src="//www.google-analytics.com/ga.js" async></script>
29+
</body>
30+
</html>

examples/slideshow.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
jQuery(function($){
2+
3+
var transitionend = 'transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd';
4+
5+
var slide = $('#slide'),
6+
prevSlide = $('#prev-slide'),
7+
images = [],
8+
width = slide.width(),
9+
height = slide.height();
10+
11+
for(var i = 0; i < 8; i++) {
12+
images.push({url: 'images/slideshow/' + i + '.jpg'});
13+
}
14+
15+
function slideShow(images){
16+
var analysed = 0,
17+
i = 0;
18+
function next(){
19+
showSlide(images[i], next);
20+
i = (i+1)%images.length;
21+
}
22+
23+
images = _.chain(images)
24+
.shuffle()
25+
.head(10)
26+
.each(function(i){
27+
i.img = new Image();
28+
i.img.onload = function(){
29+
var options = {width: width*0.1, height: height*0.1};
30+
SmartCrop.crop(i.img, _.extend({maxScale: 0.8, minScale: 0.7}, options), function(from){
31+
i.from = from;
32+
if(++analysed==images.length*2) next();
33+
});
34+
SmartCrop.crop(i.img, _.extend({minScale: 1.0}, options), function(to){
35+
i.to = to;
36+
if(++analysed==images.length*2) next();
37+
});
38+
};
39+
i.img.src = i.url;
40+
})
41+
.value();
42+
//showSlide(images[i], function(){});
43+
}
44+
45+
function showSlide(image, done){
46+
var img = image.img;
47+
function transform(crop){
48+
var s = width/crop.width,
49+
x = crop.x,
50+
y = crop.y,
51+
t = 'scale(' + s + ') translate(-' + x + 'px, -' + y + 'px)';
52+
return {
53+
'-webkit-transform': t,
54+
'transform': t,
55+
'-webkit-transform-origin': '0 0',
56+
'transform-origin': '0 0'
57+
};
58+
}
59+
60+
// zooming out usually works better, but some change is good too
61+
if(image.from.topCrop.score.total*3.9 > image.to.topCrop.score.total) {
62+
from = image.from;
63+
to = image.to;
64+
}
65+
else {
66+
from = image.to;
67+
to = image.from;
68+
}
69+
window.from = from;
70+
window.to = to;
71+
var last = $('img', slide);
72+
if(last[0]){
73+
prevSlide.empty().append(
74+
last.remove()
75+
);
76+
last.width();
77+
last.css('opacity', '0');
78+
}
79+
slide
80+
.empty()
81+
.append(img);
82+
$(img)
83+
.css(transform(from.topCrop))
84+
.css('opacity', 1)
85+
.width(); // reflow
86+
$(img)
87+
//.addClass('animate')
88+
.on(transitionend, _.once(done))
89+
.css(transform(to.topCrop));
90+
91+
img.onerror = done;
92+
img.src = image.url;
93+
}
94+
95+
slideShow(images);
96+
97+
});

examples/style.css

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,37 @@ img, canvas { padding: 8px; box-sizing: border-box; max-width: 100%; margin: aut
2626
height: 600px;
2727
overflow: auto;
2828
}
29+
30+
#slide, #prev-slide {
31+
max-width: 100%;
32+
width: 1280px;
33+
height: 720px;
34+
overflow: hidden;
35+
}
36+
#slide {
37+
background: #ccc;
38+
}
39+
#prev-slide {
40+
position: absolute;
41+
z-index: 1;
42+
}
43+
44+
#prev-slide img {
45+
transition: all 1s linear;
46+
-webkit-transition: all 1s linear;
47+
opacity: 0.9;
48+
max-width: none;
49+
padding: 0;
50+
margin: 0;
51+
}
52+
53+
#slide img {
54+
transition: all 5s linear;
55+
-webkit-transition: all 5s linear;
56+
/*transition-delay: 1s;*/
57+
/*-webkit-transition-delay: 1s; [> Safari <]*/
58+
opacity: 1;
59+
max-width: none;
60+
padding: 0;
61+
margin: 0;
62+
}

0 commit comments

Comments
 (0)