FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Nimage/src/viewer/render.ts at main · NeaByteLab/Nimage · GitHub
NeaByteLab
/
Nimage
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Nimage
/
src
/
viewer
/
render.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
375 lines (350 loc) · 11.7 KB
Breadcrumbs
Nimage
/
src
/
viewer
/
render.ts
Copy path
File metadata and controls
375 lines (350 loc) · 11.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import
type
*
as
Types
from
'@app/types.ts'
import
*
as
Viewer
from
'@app/viewer/index.ts'
import
Shared
from
'@app/shared.ts'
/**
* Viewer render layer.
*
@description
Draws the overlay and handles navigation.
*/
export
class
Render
extends
Viewer
.
State
{
/** Close the viewer overlay */
close
(
)
:
void
{
this
.
isOpen
=
false
this
.
stopSlideshow
(
)
this
.
resetTransform
(
)
this
.
overlay
.
classList
.
remove
(
'nimg-open'
)
document
.
body
.
style
.
overflow
=
''
if
(
document
.
fullscreenElement
!==
null
)
{
document
.
exitFullscreen
(
)
.
catch
(
(
)
=>
{
}
)
}
if
(
this
.
triggerElement
!==
null
)
{
this
.
triggerElement
.
focus
(
)
this
.
triggerElement
=
null
}
}
/** Destroy the viewer and free listeners */
destroy
(
)
:
void
{
this
.
close
(
)
this
.
abortController
.
abort
(
)
this
.
overlay
.
remove
(
)
this
.
popover
.
remove
(
)
}
/**
* Go to a specific item.
*
@description
Loads the item when the index is valid.
*
@param
targetIndex - Index of the item to show
*/
goTo
(
targetIndex
:
number
)
:
void
{
if
(
targetIndex
<
0
||
targetIndex
>=
this
.
items
.
length
)
{
return
}
this
.
currentIndex
=
targetIndex
this
.
loadImage
(
targetIndex
)
this
.
syncStrip
(
)
}
/** Advance to the next item */
next
(
)
:
void
{
if
(
this
.
currentIndex
<
this
.
items
.
length
-
1
)
{
this
.
currentIndex
+=
1
this
.
loadImage
(
this
.
currentIndex
)
}
else
if
(
this
.
options
.
loopEnabled
)
{
this
.
currentIndex
=
0
this
.
loadImage
(
this
.
currentIndex
)
}
else
{
return
}
this
.
syncStrip
(
)
}
/**
* Open a group in the viewer.
*
@description
Activates the overlay at the start index.
*
@param
startIndex - Index to open at
*
@param
groupName - Group name to open
*/
open
(
startIndex
=
0
,
groupName
=
'default'
)
:
void
{
const
groupItems
=
this
.
groups
.
get
(
groupName
)
if
(
groupItems
===
undefined
||
groupItems
.
length
===
0
)
{
return
}
const
activeElement
=
document
.
activeElement
this
.
triggerElement
=
activeElement
instanceof
HTMLElement
?
activeElement
:
null
this
.
items
=
groupItems
this
.
currentIndex
=
Shared
.
clamp
(
startIndex
,
0
,
groupItems
.
length
-
1
)
this
.
isOpen
=
true
this
.
hidePopover
(
)
this
.
buildStrip
(
)
this
.
overlay
.
classList
.
add
(
'nimg-open'
)
document
.
body
.
style
.
overflow
=
'hidden'
this
.
loadImage
(
this
.
currentIndex
)
this
.
updateNav
(
)
this
.
overlay
.
focus
(
)
}
/** Go back to the previous item */
prev
(
)
:
void
{
if
(
this
.
currentIndex
>
0
)
{
this
.
currentIndex
-=
1
this
.
loadImage
(
this
.
currentIndex
)
}
else
if
(
this
.
options
.
loopEnabled
)
{
this
.
currentIndex
=
this
.
items
.
length
-
1
this
.
loadImage
(
this
.
currentIndex
)
}
else
{
return
}
this
.
syncStrip
(
)
}
/**
* Replace a group with new items.
*
@description
Stores the items under the group name.
*
@param
groupName - Group name to set
*
@param
items - Gallery items for the group
*/
setGroup
(
groupName
:
string
,
items
:
Types
.
GalleryItem
[
]
)
:
void
{
this
.
groups
.
set
(
groupName
,
items
)
}
/** Apply the current transform to the image */
protected
applyTransform
(
)
:
void
{
this
.
imageElement
.
style
.
transform
=
`translate(
${
this
.
translateX
}
px,
${
this
.
translateY
}
px) scale(
${
this
.
currentScale
}
)`
this
.
imageElement
.
classList
.
toggle
(
'nimg-zoomed'
,
this
.
currentScale
>
1
)
}
/** Build the thumbnail strip */
protected
buildStrip
(
)
:
void
{
this
.
strip
.
innerHTML
=
''
this
.
thumbElements
=
this
.
items
.
map
(
(
galleryItem
,
itemIndex
)
=>
{
const
thumbNode
=
Shared
.
create
(
'img'
,
'nimg-thumb'
)
thumbNode
.
src
=
galleryItem
.
thumbSrc
thumbNode
.
alt
=
galleryItem
.
altText
thumbNode
.
loading
=
'lazy'
thumbNode
.
decoding
=
'async'
thumbNode
.
addEventListener
(
'click'
,
(
)
=>
{
this
.
goTo
(
itemIndex
)
}
,
{
signal
:
this
.
abortController
.
signal
}
)
this
.
strip
.
appendChild
(
thumbNode
)
return
thumbNode
}
)
this
.
syncStrip
(
)
}
/** Download the current image */
protected
downloadImage
(
)
:
void
{
const
galleryItem
=
this
.
items
[
this
.
currentIndex
]
if
(
galleryItem
===
undefined
)
{
return
}
const
downloadLink
=
document
.
createElement
(
'a'
)
downloadLink
.
href
=
galleryItem
.
imageSrc
downloadLink
.
download
=
(
galleryItem
.
altText
||
'image'
)
.
replace
(
/
[
^
\w
.
-
]
+
/
g
,
'_'
)
||
'image'
downloadLink
.
rel
=
'noopener'
document
.
body
.
appendChild
(
downloadLink
)
downloadLink
.
click
(
)
downloadLink
.
remove
(
)
}
/** Hide the hover preview popover */
protected
hidePopover
(
)
:
void
{
this
.
popover
.
classList
.
remove
(
'nimg-visible'
)
}
/**
* Load an image into the viewer.
*
@description
Shows a spinner then swaps in the image.
*
@param
targetIndex - Index of the item to load
*/
protected
loadImage
(
targetIndex
:
number
)
:
void
{
const
galleryItem
=
this
.
items
[
targetIndex
]
if
(
galleryItem
===
undefined
)
{
return
}
this
.
resetTransform
(
)
this
.
showSpinner
(
true
)
const
loaderImage
=
new
Image
(
)
loaderImage
.
decoding
=
'async'
loaderImage
.
onload
=
(
)
=>
{
this
.
imageElement
.
src
=
galleryItem
.
imageSrc
this
.
imageElement
.
alt
=
galleryItem
.
altText
this
.
showSpinner
(
false
)
}
loaderImage
.
onerror
=
(
)
=>
{
this
.
showSpinner
(
false
)
this
.
imageElement
.
alt
=
'Failed to load'
}
loaderImage
.
src
=
galleryItem
.
imageSrc
this
.
titleElement
.
textContent
=
galleryItem
.
altText
this
.
counterElement
.
textContent
=
`
${
targetIndex
+
1
}
/
${
this
.
items
.
length
}
`
this
.
preloadNeighbour
(
targetIndex
+
1
)
this
.
preloadNeighbour
(
targetIndex
-
1
)
}
/**
* Move the hover preview popover.
*
@description
Positions the popover near the pointer.
*
@param
pointerEvent - Mouse event driving the position
*/
protected
movePopover
(
pointerEvent
:
MouseEvent
)
:
void
{
if
(
!
this
.
popover
.
classList
.
contains
(
'nimg-visible'
)
)
{
return
}
const
edgePadding
=
16
const
popoverRect
=
this
.
popover
.
getBoundingClientRect
(
)
let
posX
=
pointerEvent
.
clientX
+
edgePadding
let
posY
=
pointerEvent
.
clientY
+
edgePadding
if
(
posX
+
popoverRect
.
width
>
globalThis
.
innerWidth
)
{
posX
=
pointerEvent
.
clientX
-
popoverRect
.
width
-
edgePadding
}
if
(
posY
+
popoverRect
.
height
>
globalThis
.
innerHeight
)
{
posY
=
pointerEvent
.
clientY
-
popoverRect
.
height
-
edgePadding
}
this
.
popover
.
style
.
left
=
`
${
Math
.
max
(
edgePadding
,
posX
)
}
px`
this
.
popover
.
style
.
top
=
`
${
Math
.
max
(
edgePadding
,
posY
)
}
px`
}
/**
* Preload a neighbouring image.
*
@description
Warms the browser cache for the item.
*
@param
targetIndex - Index of the neighbour to preload
*/
protected
preloadNeighbour
(
targetIndex
:
number
)
:
void
{
const
galleryItem
=
this
.
items
[
targetIndex
]
if
(
galleryItem
===
undefined
)
{
return
}
const
preloadImage
=
new
Image
(
)
preloadImage
.
src
=
galleryItem
.
imageSrc
}
/** Reset zoom scale and translation */
protected
resetTransform
(
)
:
void
{
this
.
currentScale
=
1
this
.
translateX
=
0
this
.
translateY
=
0
this
.
imageElement
.
classList
.
remove
(
'nimg-zoomed'
,
'nimg-dragging'
)
this
.
applyTransform
(
)
}
/** Run the slideshow progress bar */
protected
runProgress
(
)
:
void
{
if
(
this
.
progressFrame
!==
null
)
{
globalThis
.
cancelAnimationFrame
(
this
.
progressFrame
)
}
Shared
.
runProgress
(
this
.
progressBar
,
this
.
options
.
slideshowInterval
,
(
)
=>
this
.
slideshowTimer
!==
null
,
(
frameId
)
=>
{
this
.
progressFrame
=
frameId
}
)
}
/**
* Show the hover preview popover.
*
@description
Displays the full image near the pointer.
*
@param
pointerEvent - Mouse event driving the position
*
@param
imageSrc - Image source to preview
*/
protected
showPopover
(
pointerEvent
:
MouseEvent
,
imageSrc
:
string
)
:
void
{
if
(
this
.
isOpen
)
{
return
}
this
.
popoverImage
.
src
=
imageSrc
this
.
popover
.
classList
.
add
(
'nimg-visible'
)
this
.
movePopover
(
pointerEvent
)
}
/**
* Toggle the loading spinner.
*
@description
Shows or hides the spinner element.
*
@param
shouldShow - Whether the spinner is visible
*/
protected
showSpinner
(
shouldShow
:
boolean
)
:
void
{
this
.
spinner
.
classList
.
toggle
(
'nimg-visible'
,
shouldShow
)
}
/** Start the slideshow autoplay */
protected
startSlideshow
(
)
:
void
{
if
(
this
.
items
.
length
<
2
)
{
return
}
this
.
playButton
.
innerHTML
=
Shared
.
customIcons
.
pause
this
.
playButton
.
classList
.
add
(
'nimg-active'
)
this
.
runProgress
(
)
this
.
slideshowTimer
=
globalThis
.
setInterval
(
(
)
=>
{
this
.
next
(
)
this
.
runProgress
(
)
}
,
this
.
options
.
slideshowInterval
)
}
/** Stop the slideshow autoplay */
protected
stopSlideshow
(
)
:
void
{
if
(
this
.
slideshowTimer
!==
null
)
{
globalThis
.
clearInterval
(
this
.
slideshowTimer
)
}
this
.
slideshowTimer
=
null
if
(
this
.
progressFrame
!==
null
)
{
globalThis
.
cancelAnimationFrame
(
this
.
progressFrame
)
}
this
.
progressBar
.
style
.
width
=
'0'
this
.
playButton
.
innerHTML
=
Shared
.
customIcons
.
play
this
.
playButton
.
classList
.
remove
(
'nimg-active'
)
}
/** Sync thumbnail active state and labels */
protected
syncStrip
(
)
:
void
{
if
(
this
.
thumbElements
.
length
===
0
)
{
return
}
this
.
thumbElements
.
forEach
(
(
thumbNode
,
itemIndex
)
=>
{
const
isActive
=
itemIndex
===
this
.
currentIndex
thumbNode
.
classList
.
toggle
(
'nimg-active'
,
isActive
)
if
(
isActive
)
{
thumbNode
.
scrollIntoView
(
{
inline
:
'center'
,
block
:
'nearest'
,
behavior
:
'smooth'
}
)
}
}
)
const
activeItem
=
this
.
items
[
this
.
currentIndex
]
this
.
counterElement
.
textContent
=
`
${
this
.
currentIndex
+
1
}
/
${
this
.
items
.
length
}
`
this
.
titleElement
.
textContent
=
activeItem
!==
undefined
?
activeItem
.
altText
:
''
}
/** Toggle fullscreen for the overlay */
protected
toggleFullscreen
(
)
:
void
{
if
(
document
.
fullscreenElement
!==
null
)
{
document
.
exitFullscreen
(
)
.
catch
(
(
)
=>
{
}
)
}
else
{
this
.
overlay
.
requestFullscreen
(
)
.
catch
(
(
)
=>
{
}
)
}
}
/** Toggle the slideshow on or off */
protected
toggleSlideshow
(
)
:
void
{
if
(
this
.
slideshowTimer
!==
null
)
{
this
.
stopSlideshow
(
)
}
else
{
this
.
startSlideshow
(
)
}
}
/** Update navigation and strip visibility */
protected
updateNav
(
)
:
void
{
const
hasMultiple
=
this
.
items
.
length
>
1
this
.
prevButton
.
style
.
display
=
hasMultiple
?
''
:
'none'
this
.
nextButton
.
style
.
display
=
hasMultiple
?
''
:
'none'
this
.
playButton
.
style
.
display
=
hasMultiple
?
''
:
'none'
const
showStrip
=
hasMultiple
&&
this
.
options
.
thumbnailsEnabled
this
.
strip
.
style
.
display
=
showStrip
?
''
:
'none'
}
/**
* Change the image zoom scale.
*
@description
Zooms toward a point and clamps the scale.
*
@param
deltaScale - Amount to change the scale by
*
@param
pointerX - Optional pointer x to zoom toward
*
@param
pointerY - Optional pointer y to zoom toward
*/
protected
zoom
(
deltaScale
:
number
,
pointerX
?:
number
,
pointerY
?:
number
)
:
void
{
const
previousScale
=
this
.
currentScale
this
.
currentScale
=
Shared
.
clamp
(
this
.
currentScale
+
deltaScale
,
this
.
options
.
minZoom
,
this
.
options
.
maxZoom
)
if
(
this
.
currentScale
===
previousScale
)
{
return
}
if
(
pointerX
!==
undefined
&&
pointerY
!==
undefined
&&
previousScale
>
0
)
{
const
stageRect
=
this
.
stage
.
getBoundingClientRect
(
)
const
offsetX
=
pointerX
-
stageRect
.
left
-
stageRect
.
width
/
2
const
offsetY
=
pointerY
-
stageRect
.
top
-
stageRect
.
height
/
2
const
scaleRatio
=
this
.
currentScale
/
previousScale
this
.
translateX
=
offsetX
-
(
offsetX
-
this
.
translateX
)
*
scaleRatio
this
.
translateY
=
offsetY
-
(
offsetY
-
this
.
translateY
)
*
scaleRatio
}
if
(
this
.
currentScale
===
1
)
{
this
.
translateX
=
0
this
.
translateY
=
0
}
this
.
applyTransform
(
)
}
}
Back
|
FazBrowse Home
|
New Git URL