forked from malteschmitz/slideshow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslideshow.coffee
259 lines (227 loc) · 7.29 KB
/
slideshow.coffee
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
###
Slideshow 0.1
(c) Malte Schmitz, December 2013
MIT Licence
###
class this.Slideshow
# options
# * presentationUrl contains the fully qualified URL to the pdf file
# * workerSrc contains the relative file name of pdf.worker.js
# * pleaseWaitString may contain a translation of "Please wait!"
# * loadingString may contain a translation of "Loading the presentation..."
constructor: (@options) ->
@options.pleaseWaitString = @options.pleaseWaitString ? 'Please wait!'
@options.loadingString = @options.loadingString ? 'Loading the presentation...'
PDFJS.workerSrc = @options.workerSrc if @options.workerSrc?
@zoom = 2
@currentPage = 1
@initializeElements()
@attachGuiHandlers()
@loadPresentation().then @renderCurrentPage
initializeElements: ->
# create the canvas element and attach it to the slideshow
@canvas = $('<canvas></canvas>')
@context = @canvas[0].getContext('2d')
$('#slideshow').append(@canvas)
# display initial loading text on the canvas
@canvas[0].height = 544
@canvas[0].width = 725
@context.font = "24pt \"Helvetica Neue\",Helvetica,Arial,sans-serif"
@context.fillText @options.pleaseWaitString, 10, 50
@context.font = "16pt \"Helvetica Neue\",Helvetica,Arial,sans-serif"
@context.fillText @options.loadingString, 10, 100
# create pointer element
@pointer = $('<div></div>')
@pointer.css
backgroundColor: "#FF0000",
position: "absolute"
@pointer.hide()
$("#slideshow").append(@pointer)
# select player element
@player = $('#player')
attachGuiHandlers: ->
$("#prev").click @prevPage
$("#next").click @nextPage
$("#zoom-default").click @zoomDefault
$("#zoom-out").click @zoomOut
$("#zoom-in").click @zoomIn
$(document).keydown @keydownHandler
$('#slideshow').mousemove @hideCursor
$("#fullscreen").click @toggleFullScreen
$(document).on 'webkitfullscreenchange mozfullscreenchange fullscreenchange', @fullScreenChangeHandler
keydownHandler: (event) =>
switch event.which
when 189 # -
@zoomOut()
event.preventDefault()
when 48 # 0
@zoomDefault()
event.preventDefault()
when 187 # +
@zoomIn()
event.preventDefault()
when 37, 33 # Left, Page Up
@prevPage()
event.preventDefault()
when 39, 34 # Right, Page Down
@nextPage()
event.preventDefault()
when 13 # Enter
@toggleFullScreen()
event.preventDefault()
loadPresentation: ->
# Asynchronously download PDF as an ArrayBuffer
PDFJS.getDocument(@options.presentationUrl).then (pdfDoc) =>
pdfDoc.getDestinations().then (destinations) => @destinations = destinations
@numPages = pdfDoc.numPages
@pagePromises = []
@pagesRefMap = {}
for pageNum in [1..@numPages]
pagePromise = pdfDoc.getPage pageNum
pagePromise.then (pdfPage) =>
pageRef = pdfPage.ref
refStr = pageRef.num + ' ' + pageRef.gen + ' R'
@pagesRefMap[refStr] = pdfPage.pageNumber
@pagePromises.push(pagePromise)
renderPage: (page) =>
if @isFullScreen
viewport = page.getViewport 1
z = Math.min screen.width / viewport.width, screen.height / viewport.height
viewport = page.getViewport z
else
viewport = page.getViewport @zoom
@canvas[0].height = viewport.height
@canvas[0].width = viewport.width
# Render PDF page into canvas context
page.render(canvasContext: @context, viewport: viewport)
viewport
renderCurrentPage: =>
# Use promises to render the current page
@pagePromises?[@currentPage - 1]?.then @renderPage
# Update page counters
$('#page_num').text @currentPage
$('#page_count').text @numPages
# Always hide pointer after rendering
@hidePointer()
gotoPage: (pageNum) ->
if 0 < pageNum <= @numPages
@currentPage = pageNum
@renderCurrentPage()
false
prevPage: => @gotoPage(@currentPage - 1)
nextPage: => @gotoPage(@currentPage + 1)
zoomDefault: =>
if @zoom != 2
@zoom = 2
@renderCurrentPage()
false
zoomOut: =>
if @zoom > .3
@zoom = @zoom / 1.2
@renderCurrentPage()
false
zoomIn: =>
if @zoom < 10
@zoom = @zoom * 1.2
@renderCurrentPage()
false
toggleFullScreen: =>
if @isFullScreen
@cancelFullScreen()
else
@requestFullScreen()
false
cancelFullScreen: ->
method = document.cancelFullScreen or document.webkitCancelFullScreen or
document.mozCancelFullScreen or document.exitFullscreen
if method
method.call(document)
else
alert "Your browser does not support the full screen API."
requestFullScreen: ->
element = document.getElementById('slideshow')
method = element.requestFullScreen or element.webkitRequestFullScreen or
element.mozRequestFullScreen or element.msRequestFullScreen
if method
method.call element
else
alert "Your browser does not support the full screen API."
fullScreenChangeHandler: =>
@isFullScreen = document.fullScreenElement? or !!document.mozFullScreen or !!document.webkitIsFullScreen
if @isFullScreen
$('#slideshow').addClass('fullscreen')
@hideCursor()
else
$('#slideshow').removeClass('fullscreen')
@showCursor()
@renderCurrentPage()
showCursor: =>
clearTimeout(@hideCursorTimeout) if @hideCursorTimeout?
delete @hideCursorTimeout
$('#slideshow').removeClass('no-cursor')
hideCursor: =>
if @cursorJustHidden
# prevent the hiding action to trigger mousemove
delete @cursorJustHidden
return
@showCursor()
if @isFullScreen
@hideCursorTimeout = setTimeout =>
$('#slideshow').addClass('no-cursor')
@cursorJustHidden = true
, 2000
hidePointer: ->
if @pointerTimeout?
clearTimeout(@pointerTimeout)
delete @pointerTimeout
@pointer.hide()
showPointer: (x,y) ->
if @currentAnimationTarget? and
x == @currentAnimationTarget[0] and
y == @currentAnimationTarget[1]
# prevent strange animation bugs if
# the same animation gets started more than once
return
# store current parameters for the test above
@currentAnimationTarget = [x,y]
# compute absolut coordinates
offset = @canvas.offset()
radius = .03 * @canvas.width()
x = offset.left + @canvas.width() * x
y = offset.top + @canvas.height() * y
# display pointer
@pointer.stop(true, true)
if @pointerTimeout?
@pointer.animate
top: y - radius,
left: x - radius,
,
duration: 300,
done: => delete @currentAnimationTarget,
clearTimeout(@pointerTimeout)
delete @pointerTimeout
else
@pointer.show()
@pointer.css
top: y,
left: x,
opacity: 0,
borderRadius: 0,
width: 0,
height: 0,
@pointer.animate
top: y - radius,
left: x - radius,
width: 2 * radius,
height: 2 * radius,
borderRadius: radius,
opacity: .64,
,
duration: 600,
done: => delete @currentAnimationTarget,
easing: "easeOutElastic",
# start timeout to hide pointer
@pointerTimeout = setTimeout =>
@pointer.fadeOut duration: 600
delete @pointerTimeout
, 4000