This repository has been archived by the owner on Aug 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shrink.js
57 lines (49 loc) · 1.91 KB
/
shrink.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
shrink.js copyright (c) 2011 Alex Chaffee <alex@stinky.com>
"I plan to incorporate it into Showoff proper as an option once I
get a chance -- which has a vanilla MIT license. I hereby give you
permission to use it in your training materials in the meantime."
*/
// TODO: make it bind to a good event, not a timeout
setTimeout(function() {
$(".content").bind("showoff:show", function (event) {
var content = $(event.target);
var slide = content.parent(".slide");
var slideHeight = $(slide).innerHeight();
var contentHeight = $(content).height();
console.log("contentHeight", contentHeight, "slideHeight", slideHeight)
var fudge = 20;
var tooBig = (contentHeight + fudge > slideHeight);
if (tooBig) {
// shrink text
var ratio = slideHeight / contentHeight - .15; // extra 15% for luck
var percent = "" + parseInt(ratio * 100) + "%";
console.log("Shrinking by " + percent);
content.css("font-size", percent);
// shrink images
content.find('img').each(function(i, element) {
var newHeight = parseInt($(element).height() * ratio);
var newWidth = parseInt($(element).width() * ratio);
$(element).css('height', newHeight).css('width', newWidth);
});
}
// shrink pre (non-wrapping) text
// (do this after the page has shrunk, in case that fixed it already)
content.find('pre').each(function(i, element) {
var pre = $(element);
var code = $(pre.find("code"));
var codeWidth = code.width();
var preWidth = pre.width();
var nominalWidth = preWidth -
parseInt(pre.css('padding-left')) -
parseInt(pre.css('padding-right'));
if (codeWidth > nominalWidth) {
var ratio = nominalWidth / codeWidth - .035; // extra 3.5% for luck
var percent = "" + parseInt(ratio * 100) + "%";
console.log("Shrinking code by " + percent);
code.css('font-size', percent);
pre.css('line-height', percent);
}
});
});
}, 3000);