-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.js
174 lines (122 loc) · 3.23 KB
/
base.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
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
//@ts-check
const url = new URL(location.href)
const path = url.searchParams.get("file")
const data = await fetch(path)
let type = "image/gif"
if(path.endsWith('.webp')) {
type = "image/webp"
}
const imgDecoder = new ImageDecoder({ data: data.body, type })
const canvas = document.querySelector("canvas")
/**
* @type { HTMLInputElement}
*/
const fpsScale = document.getElementById("fpsscale")
const details = document.getElementById("details")
const progressBar = document.getElementById("progressbar")
const progressLabel = document.getElementById("progresslabel")
const playBtn = document.getElementById("play")
const stopBtn = document.getElementById("stop")
const prevBtn = document.getElementById("prev")
const nextBtn = document.getElementById("next")
let open = false
details.ontoggle = () => {
open = details.open
}
let frameCount
let duration
let fpsScaleValue
let displayHeight
let displayWidth
let deviceFps = 60
fpsScale.onchange = () => {
let t = parseFloat(fpsScale.value)
if (t > 0) {
fpsScaleValue = t
}
}
let curIndex = 0
imgDecoder.completed.then(() => imgDecoder.decode()).then((result) => {
frameCount = imgDecoder.tracks.selectedTrack.frameCount
progressBar.max = frameCount - 1
duration = result.image.duration / 1000.0 / 1000.0
if (duration <= 0) {
duration = 1 / deviceFps
}
fpsScale.onchange()
displayHeight = result.image.displayHeight
displayWidth = result.image.displayWidth
canvas.width = displayWidth
canvas.height = displayHeight
canvas.getContext("2d").drawImage(result.image, 0, 0)
curIndex = 0
render()
})
let played = true
let lastTimestamp = 0
let delayResult = null
let aTime = 0
let bTime = 0
let timestamp1 = 0
const ctx = canvas.getContext("2d")
function resultProcess(result) {
bTime = Date.now()
if (played && bTime - aTime + timestamp1 - lastTimestamp < result.image.duration / 1000 / fpsScaleValue ) {
delayResult = result
requestAnimationFrame(render)
return
}
lastTimestamp = timestamp1
ctx.drawImage(result.image, 0, 0)
if (played) {
curIndex++
requestAnimationFrame(render)
}
}
function render(timestamp = 0) {
if (curIndex >= frameCount || curIndex < 0) {
curIndex = 0
}
aTime = Date.now()
timestamp1 = timestamp
if (delayResult === null) {
imgDecoder.decode({ frameIndex: curIndex }).then(resultProcess)
} else {
resultProcess(delayResult)
delayResult = null
}
if (open) {
progressBar.value = curIndex
progressLabel.textContent = `${curIndex+1}/${frameCount}`
}
}
progressBar.onchange = () => {
if(progressBar.value != curIndex) {
curIndex = progressBar.value
render()
}
progressLabel.textContent = `${+curIndex+1}/${frameCount}`
}
playBtn.onclick = () => {
let oldPlayed = played
played = true
if (!oldPlayed) {
render()
}
}
stopBtn.onclick = () => {
played = false
}
prevBtn.onclick = () => {
played = false
curIndex--
render()
}
nextBtn.onclick = () => {
played = false
curIndex++
if (curIndex >= frameCount) {
curIndex = frameCount - 1
}
render()
}