@leafer-in/video 0.0.0-stage → 2.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1 -3
- package/dist/video.cjs +313 -0
- package/dist/video.esm.js +305 -0
- package/dist/video.esm.min.js +2 -0
- package/dist/video.esm.min.js.map +1 -0
- package/dist/video.js +293 -0
- package/dist/video.min.cjs +2 -0
- package/dist/video.min.cjs.map +1 -0
- package/dist/video.min.js +2 -0
- package/dist/video.min.js.map +1 -0
- package/package.json +38 -3
- package/src/PaintVideo.ts +88 -0
- package/src/Video.ts +40 -0
- package/src/VideoDecoder.ts +118 -0
- package/src/data/VideoData.ts +9 -0
- package/src/index.ts +130 -0
- package/src/interface/IVideo.ts +12 -0
- package/types/index.d.ts +82 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-present, Chao (Leafer) Wan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
CHANGED
package/dist/video.cjs
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var draw = require("@leafer-ui/draw");
|
|
4
|
+
|
|
5
|
+
function __decorate(decorators, target, key, desc) {
|
|
6
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
7
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
8
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
12
|
+
function adopt(value) {
|
|
13
|
+
return value instanceof P ? value : new P(function(resolve) {
|
|
14
|
+
resolve(value);
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return new (P || (P = Promise))(function(resolve, reject) {
|
|
18
|
+
function fulfilled(value) {
|
|
19
|
+
try {
|
|
20
|
+
step(generator.next(value));
|
|
21
|
+
} catch (e) {
|
|
22
|
+
reject(e);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function rejected(value) {
|
|
26
|
+
try {
|
|
27
|
+
step(generator["throw"](value));
|
|
28
|
+
} catch (e) {
|
|
29
|
+
reject(e);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function step(result) {
|
|
33
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
34
|
+
}
|
|
35
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
|
|
40
|
+
var e = new Error(message);
|
|
41
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
class VideoDecoder {
|
|
45
|
+
get currentTime() {
|
|
46
|
+
const video = this.decoder;
|
|
47
|
+
return video.currentTime;
|
|
48
|
+
}
|
|
49
|
+
set currentTime(time) {
|
|
50
|
+
const video = this.decoder;
|
|
51
|
+
const targetTime = draw.MathHelper.within(time, 0, this.duration);
|
|
52
|
+
video.currentTime = targetTime;
|
|
53
|
+
}
|
|
54
|
+
get volume() {
|
|
55
|
+
const video = this.decoder;
|
|
56
|
+
return video.volume;
|
|
57
|
+
}
|
|
58
|
+
set volume(volume) {
|
|
59
|
+
const video = this.decoder;
|
|
60
|
+
const targetVolume = draw.MathHelper.within(volume, 0, 1);
|
|
61
|
+
video.volume = targetVolume;
|
|
62
|
+
if (targetVolume > 0 && video.muted) video.muted = false;
|
|
63
|
+
}
|
|
64
|
+
get paused() {
|
|
65
|
+
const video = this.decoder;
|
|
66
|
+
return video.paused;
|
|
67
|
+
}
|
|
68
|
+
get ended() {
|
|
69
|
+
const video = this.decoder;
|
|
70
|
+
return video.ended;
|
|
71
|
+
}
|
|
72
|
+
static register(type, Decoder, force) {
|
|
73
|
+
VideoDecoder.decoderMap[type] = {
|
|
74
|
+
Decoder: Decoder,
|
|
75
|
+
force: force
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
load() {
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
play() {
|
|
82
|
+
const video = this.decoder;
|
|
83
|
+
video.play();
|
|
84
|
+
}
|
|
85
|
+
pause() {
|
|
86
|
+
const video = this.decoder;
|
|
87
|
+
video.pause();
|
|
88
|
+
}
|
|
89
|
+
render(canvas, x, y, width, height, leaf, playOptions, _imageScaleX, _imageScaleY) {
|
|
90
|
+
let {paused: paused} = playOptions;
|
|
91
|
+
const video = this.decoder;
|
|
92
|
+
if (video.ended) {
|
|
93
|
+
video.pause();
|
|
94
|
+
playOptions.paused = true;
|
|
95
|
+
playOptions.loop = (playOptions.loop || 0) + 1;
|
|
96
|
+
}
|
|
97
|
+
if (paused === false && video.paused) {
|
|
98
|
+
this.play();
|
|
99
|
+
} else if (paused === true && !video.paused) {
|
|
100
|
+
this.pause();
|
|
101
|
+
} else {
|
|
102
|
+
playOptions.paused = video.paused;
|
|
103
|
+
}
|
|
104
|
+
canvas.drawImage(video, x, y, width, height);
|
|
105
|
+
if (!video.paused) leaf.forceRender();
|
|
106
|
+
}
|
|
107
|
+
destroyDecoder() {
|
|
108
|
+
const video = this.decoder;
|
|
109
|
+
if (video) {
|
|
110
|
+
video.pause();
|
|
111
|
+
video.src = "";
|
|
112
|
+
video.load();
|
|
113
|
+
this.decoder = null;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
close() {
|
|
117
|
+
this.destroyDecoder();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
VideoDecoder.decoderMap = {};
|
|
122
|
+
|
|
123
|
+
class VideoData extends draw.ImageData {
|
|
124
|
+
get __urlType() {
|
|
125
|
+
return "video";
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
class PaintVideo {
|
|
130
|
+
get playerOptions() {
|
|
131
|
+
const {fill: fill} = (this.element || this).__;
|
|
132
|
+
return draw.isArray(fill) && fill.find(item => item.type === "video");
|
|
133
|
+
}
|
|
134
|
+
get decoder() {
|
|
135
|
+
const {playerOptions: playerOptions} = this;
|
|
136
|
+
return playerOptions && playerOptions.image && playerOptions.image.view;
|
|
137
|
+
}
|
|
138
|
+
get duration() {
|
|
139
|
+
const video = this.decoder;
|
|
140
|
+
return video && video.duration;
|
|
141
|
+
}
|
|
142
|
+
get currentTime() {
|
|
143
|
+
const video = this.decoder;
|
|
144
|
+
return video && video.currentTime;
|
|
145
|
+
}
|
|
146
|
+
set currentTime(time) {
|
|
147
|
+
const video = this.decoder;
|
|
148
|
+
if (video) {
|
|
149
|
+
video.currentTime = time;
|
|
150
|
+
video.decoder.addEventListener("seeked", () => (this.element || this).forceRender(), {
|
|
151
|
+
once: true
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
get volume() {
|
|
156
|
+
const video = this.decoder;
|
|
157
|
+
return video && video.volume;
|
|
158
|
+
}
|
|
159
|
+
set volume(volume) {
|
|
160
|
+
const video = this.decoder;
|
|
161
|
+
if (video) video.volume = volume;
|
|
162
|
+
}
|
|
163
|
+
get paused() {
|
|
164
|
+
const video = this.decoder;
|
|
165
|
+
return video && video.paused;
|
|
166
|
+
}
|
|
167
|
+
get ended() {
|
|
168
|
+
const video = this.decoder;
|
|
169
|
+
return video && video.ended;
|
|
170
|
+
}
|
|
171
|
+
constructor(element) {
|
|
172
|
+
this.element = element;
|
|
173
|
+
}
|
|
174
|
+
togglePlay() {
|
|
175
|
+
const {playerOptions: playerOptions} = this;
|
|
176
|
+
if (playerOptions) {
|
|
177
|
+
if (playerOptions.paused !== false) this.play(); else this.pause();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
play() {
|
|
181
|
+
const {playerOptions: playerOptions} = this;
|
|
182
|
+
if (playerOptions && playerOptions.paused !== false) {
|
|
183
|
+
playerOptions.paused = false;
|
|
184
|
+
(this.element || this).forceRender();
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
pause() {
|
|
188
|
+
const {playerOptions: playerOptions} = this;
|
|
189
|
+
if (playerOptions) {
|
|
190
|
+
playerOptions.paused = true;
|
|
191
|
+
this.decoder.pause();
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
stop() {
|
|
195
|
+
const {playerOptions: playerOptions} = this;
|
|
196
|
+
if (playerOptions) {
|
|
197
|
+
playerOptions.nowIndex = 0;
|
|
198
|
+
this.currentTime = 0;
|
|
199
|
+
this.pause();
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
exports.Video = class Video extends draw.Image {
|
|
205
|
+
get __tag() {
|
|
206
|
+
return "Video";
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
__decorate([ draw.dataProcessor(VideoData) ], exports.Video.prototype, "__", void 0);
|
|
211
|
+
|
|
212
|
+
exports.Video = __decorate([ draw.useModule(PaintVideo), draw.registerUI() ], exports.Video);
|
|
213
|
+
|
|
214
|
+
draw.Plugin.add("video");
|
|
215
|
+
|
|
216
|
+
draw.PaintImage.video = function(paint, oldPaint) {
|
|
217
|
+
paint.film = true;
|
|
218
|
+
paint.nowIndex = 0;
|
|
219
|
+
if (draw.isObject(oldPaint)) {
|
|
220
|
+
paint.paused = oldPaint.paused;
|
|
221
|
+
paint.loop = oldPaint.loop;
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
draw.Creator.video = function(options) {
|
|
226
|
+
return new draw.LeaferVideo(options);
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
draw.Resource.loadVideo = function(key, format) {
|
|
230
|
+
return new Promise((resolve, reject) => {
|
|
231
|
+
const config = {
|
|
232
|
+
url: key
|
|
233
|
+
};
|
|
234
|
+
if (format) config.format = format;
|
|
235
|
+
const video = draw.Creator.video(config);
|
|
236
|
+
draw.Resource.set(key, video);
|
|
237
|
+
video.load(() => resolve(video), e => reject(e));
|
|
238
|
+
});
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
draw.LeaferVideo.prototype.render = function(canvas, x, y, width, height, leaf, paint, imageScaleX, imageScaleY) {
|
|
242
|
+
this.view.render(canvas, x, y, width, height, leaf, paint, imageScaleX, imageScaleY);
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
draw.Platform.origin.loadVideo = function(url, _crossOrigin, _leaferVideo) {
|
|
246
|
+
return __awaiter(this, void 0, void 0, function*() {
|
|
247
|
+
const video = createVideoElement(url);
|
|
248
|
+
const decoder = new VideoDecoder;
|
|
249
|
+
const {width: width, height: height, duration: duration} = yield waitVideoMetadata(video);
|
|
250
|
+
decoder.decoder = video;
|
|
251
|
+
decoder.width = width;
|
|
252
|
+
decoder.height = height;
|
|
253
|
+
decoder.duration = duration;
|
|
254
|
+
yield waitVideoFrameReady(video);
|
|
255
|
+
return decoder;
|
|
256
|
+
});
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
function createVideoElement(url, muted = false, loop = false) {
|
|
260
|
+
const video = document.createElement("video");
|
|
261
|
+
video.src = url;
|
|
262
|
+
video.muted = muted;
|
|
263
|
+
video.loop = loop;
|
|
264
|
+
video.playsInline = true;
|
|
265
|
+
video.crossOrigin = "anonymous";
|
|
266
|
+
return video;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function waitVideoMetadata(video) {
|
|
270
|
+
return new Promise(resolve => {
|
|
271
|
+
if (video.readyState >= 1) {
|
|
272
|
+
resolve({
|
|
273
|
+
width: video.videoWidth,
|
|
274
|
+
height: video.videoHeight,
|
|
275
|
+
duration: video.duration
|
|
276
|
+
});
|
|
277
|
+
} else {
|
|
278
|
+
video.addEventListener("loadedmetadata", () => {
|
|
279
|
+
resolve({
|
|
280
|
+
width: video.videoWidth,
|
|
281
|
+
height: video.videoHeight,
|
|
282
|
+
duration: video.duration
|
|
283
|
+
});
|
|
284
|
+
}, {
|
|
285
|
+
once: true
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function waitVideoFrameReady(video) {
|
|
292
|
+
return new Promise(resolve => {
|
|
293
|
+
if ("requestVideoFrameCallback" in video) {
|
|
294
|
+
video.requestVideoFrameCallback(() => resolve());
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
if (video.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA) {
|
|
298
|
+
resolve();
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
video.addEventListener("loadeddata", () => resolve(), {
|
|
302
|
+
once: true
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
exports.PaintVideo = PaintVideo;
|
|
308
|
+
|
|
309
|
+
exports.VideoData = VideoData;
|
|
310
|
+
|
|
311
|
+
exports.VideoDecoder = VideoDecoder;
|
|
312
|
+
|
|
313
|
+
exports.waitVideoFrameReady = waitVideoFrameReady;
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import { MathHelper, ImageData, isArray, dataProcessor, useModule, registerUI, Image, Plugin, PaintImage, isObject, Creator, LeaferVideo, Resource, Platform } from "@leafer-ui/draw";
|
|
2
|
+
|
|
3
|
+
function __decorate(decorators, target, key, desc) {
|
|
4
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
5
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
10
|
+
function adopt(value) {
|
|
11
|
+
return value instanceof P ? value : new P(function(resolve) {
|
|
12
|
+
resolve(value);
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
return new (P || (P = Promise))(function(resolve, reject) {
|
|
16
|
+
function fulfilled(value) {
|
|
17
|
+
try {
|
|
18
|
+
step(generator.next(value));
|
|
19
|
+
} catch (e) {
|
|
20
|
+
reject(e);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function rejected(value) {
|
|
24
|
+
try {
|
|
25
|
+
step(generator["throw"](value));
|
|
26
|
+
} catch (e) {
|
|
27
|
+
reject(e);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function step(result) {
|
|
31
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
32
|
+
}
|
|
33
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
|
|
38
|
+
var e = new Error(message);
|
|
39
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
class VideoDecoder {
|
|
43
|
+
get currentTime() {
|
|
44
|
+
const video = this.decoder;
|
|
45
|
+
return video.currentTime;
|
|
46
|
+
}
|
|
47
|
+
set currentTime(time) {
|
|
48
|
+
const video = this.decoder;
|
|
49
|
+
const targetTime = MathHelper.within(time, 0, this.duration);
|
|
50
|
+
video.currentTime = targetTime;
|
|
51
|
+
}
|
|
52
|
+
get volume() {
|
|
53
|
+
const video = this.decoder;
|
|
54
|
+
return video.volume;
|
|
55
|
+
}
|
|
56
|
+
set volume(volume) {
|
|
57
|
+
const video = this.decoder;
|
|
58
|
+
const targetVolume = MathHelper.within(volume, 0, 1);
|
|
59
|
+
video.volume = targetVolume;
|
|
60
|
+
if (targetVolume > 0 && video.muted) video.muted = false;
|
|
61
|
+
}
|
|
62
|
+
get paused() {
|
|
63
|
+
const video = this.decoder;
|
|
64
|
+
return video.paused;
|
|
65
|
+
}
|
|
66
|
+
get ended() {
|
|
67
|
+
const video = this.decoder;
|
|
68
|
+
return video.ended;
|
|
69
|
+
}
|
|
70
|
+
static register(type, Decoder, force) {
|
|
71
|
+
VideoDecoder.decoderMap[type] = {
|
|
72
|
+
Decoder: Decoder,
|
|
73
|
+
force: force
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
load() {
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
play() {
|
|
80
|
+
const video = this.decoder;
|
|
81
|
+
video.play();
|
|
82
|
+
}
|
|
83
|
+
pause() {
|
|
84
|
+
const video = this.decoder;
|
|
85
|
+
video.pause();
|
|
86
|
+
}
|
|
87
|
+
render(canvas, x, y, width, height, leaf, playOptions, _imageScaleX, _imageScaleY) {
|
|
88
|
+
let {paused: paused} = playOptions;
|
|
89
|
+
const video = this.decoder;
|
|
90
|
+
if (video.ended) {
|
|
91
|
+
video.pause();
|
|
92
|
+
playOptions.paused = true;
|
|
93
|
+
playOptions.loop = (playOptions.loop || 0) + 1;
|
|
94
|
+
}
|
|
95
|
+
if (paused === false && video.paused) {
|
|
96
|
+
this.play();
|
|
97
|
+
} else if (paused === true && !video.paused) {
|
|
98
|
+
this.pause();
|
|
99
|
+
} else {
|
|
100
|
+
playOptions.paused = video.paused;
|
|
101
|
+
}
|
|
102
|
+
canvas.drawImage(video, x, y, width, height);
|
|
103
|
+
if (!video.paused) leaf.forceRender();
|
|
104
|
+
}
|
|
105
|
+
destroyDecoder() {
|
|
106
|
+
const video = this.decoder;
|
|
107
|
+
if (video) {
|
|
108
|
+
video.pause();
|
|
109
|
+
video.src = "";
|
|
110
|
+
video.load();
|
|
111
|
+
this.decoder = null;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
close() {
|
|
115
|
+
this.destroyDecoder();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
VideoDecoder.decoderMap = {};
|
|
120
|
+
|
|
121
|
+
class VideoData extends ImageData {
|
|
122
|
+
get __urlType() {
|
|
123
|
+
return "video";
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
class PaintVideo {
|
|
128
|
+
get playerOptions() {
|
|
129
|
+
const {fill: fill} = (this.element || this).__;
|
|
130
|
+
return isArray(fill) && fill.find(item => item.type === "video");
|
|
131
|
+
}
|
|
132
|
+
get decoder() {
|
|
133
|
+
const {playerOptions: playerOptions} = this;
|
|
134
|
+
return playerOptions && playerOptions.image && playerOptions.image.view;
|
|
135
|
+
}
|
|
136
|
+
get duration() {
|
|
137
|
+
const video = this.decoder;
|
|
138
|
+
return video && video.duration;
|
|
139
|
+
}
|
|
140
|
+
get currentTime() {
|
|
141
|
+
const video = this.decoder;
|
|
142
|
+
return video && video.currentTime;
|
|
143
|
+
}
|
|
144
|
+
set currentTime(time) {
|
|
145
|
+
const video = this.decoder;
|
|
146
|
+
if (video) {
|
|
147
|
+
video.currentTime = time;
|
|
148
|
+
video.decoder.addEventListener("seeked", () => (this.element || this).forceRender(), {
|
|
149
|
+
once: true
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
get volume() {
|
|
154
|
+
const video = this.decoder;
|
|
155
|
+
return video && video.volume;
|
|
156
|
+
}
|
|
157
|
+
set volume(volume) {
|
|
158
|
+
const video = this.decoder;
|
|
159
|
+
if (video) video.volume = volume;
|
|
160
|
+
}
|
|
161
|
+
get paused() {
|
|
162
|
+
const video = this.decoder;
|
|
163
|
+
return video && video.paused;
|
|
164
|
+
}
|
|
165
|
+
get ended() {
|
|
166
|
+
const video = this.decoder;
|
|
167
|
+
return video && video.ended;
|
|
168
|
+
}
|
|
169
|
+
constructor(element) {
|
|
170
|
+
this.element = element;
|
|
171
|
+
}
|
|
172
|
+
togglePlay() {
|
|
173
|
+
const {playerOptions: playerOptions} = this;
|
|
174
|
+
if (playerOptions) {
|
|
175
|
+
if (playerOptions.paused !== false) this.play(); else this.pause();
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
play() {
|
|
179
|
+
const {playerOptions: playerOptions} = this;
|
|
180
|
+
if (playerOptions && playerOptions.paused !== false) {
|
|
181
|
+
playerOptions.paused = false;
|
|
182
|
+
(this.element || this).forceRender();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
pause() {
|
|
186
|
+
const {playerOptions: playerOptions} = this;
|
|
187
|
+
if (playerOptions) {
|
|
188
|
+
playerOptions.paused = true;
|
|
189
|
+
this.decoder.pause();
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
stop() {
|
|
193
|
+
const {playerOptions: playerOptions} = this;
|
|
194
|
+
if (playerOptions) {
|
|
195
|
+
playerOptions.nowIndex = 0;
|
|
196
|
+
this.currentTime = 0;
|
|
197
|
+
this.pause();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
let Video = class Video extends Image {
|
|
203
|
+
get __tag() {
|
|
204
|
+
return "Video";
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
__decorate([ dataProcessor(VideoData) ], Video.prototype, "__", void 0);
|
|
209
|
+
|
|
210
|
+
Video = __decorate([ useModule(PaintVideo), registerUI() ], Video);
|
|
211
|
+
|
|
212
|
+
Plugin.add("video");
|
|
213
|
+
|
|
214
|
+
PaintImage.video = function(paint, oldPaint) {
|
|
215
|
+
paint.film = true;
|
|
216
|
+
paint.nowIndex = 0;
|
|
217
|
+
if (isObject(oldPaint)) {
|
|
218
|
+
paint.paused = oldPaint.paused;
|
|
219
|
+
paint.loop = oldPaint.loop;
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
Creator.video = function(options) {
|
|
224
|
+
return new LeaferVideo(options);
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
Resource.loadVideo = function(key, format) {
|
|
228
|
+
return new Promise((resolve, reject) => {
|
|
229
|
+
const config = {
|
|
230
|
+
url: key
|
|
231
|
+
};
|
|
232
|
+
if (format) config.format = format;
|
|
233
|
+
const video = Creator.video(config);
|
|
234
|
+
Resource.set(key, video);
|
|
235
|
+
video.load(() => resolve(video), e => reject(e));
|
|
236
|
+
});
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
LeaferVideo.prototype.render = function(canvas, x, y, width, height, leaf, paint, imageScaleX, imageScaleY) {
|
|
240
|
+
this.view.render(canvas, x, y, width, height, leaf, paint, imageScaleX, imageScaleY);
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
Platform.origin.loadVideo = function(url, _crossOrigin, _leaferVideo) {
|
|
244
|
+
return __awaiter(this, void 0, void 0, function*() {
|
|
245
|
+
const video = createVideoElement(url);
|
|
246
|
+
const decoder = new VideoDecoder;
|
|
247
|
+
const {width: width, height: height, duration: duration} = yield waitVideoMetadata(video);
|
|
248
|
+
decoder.decoder = video;
|
|
249
|
+
decoder.width = width;
|
|
250
|
+
decoder.height = height;
|
|
251
|
+
decoder.duration = duration;
|
|
252
|
+
yield waitVideoFrameReady(video);
|
|
253
|
+
return decoder;
|
|
254
|
+
});
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
function createVideoElement(url, muted = false, loop = false) {
|
|
258
|
+
const video = document.createElement("video");
|
|
259
|
+
video.src = url;
|
|
260
|
+
video.muted = muted;
|
|
261
|
+
video.loop = loop;
|
|
262
|
+
video.playsInline = true;
|
|
263
|
+
video.crossOrigin = "anonymous";
|
|
264
|
+
return video;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function waitVideoMetadata(video) {
|
|
268
|
+
return new Promise(resolve => {
|
|
269
|
+
if (video.readyState >= 1) {
|
|
270
|
+
resolve({
|
|
271
|
+
width: video.videoWidth,
|
|
272
|
+
height: video.videoHeight,
|
|
273
|
+
duration: video.duration
|
|
274
|
+
});
|
|
275
|
+
} else {
|
|
276
|
+
video.addEventListener("loadedmetadata", () => {
|
|
277
|
+
resolve({
|
|
278
|
+
width: video.videoWidth,
|
|
279
|
+
height: video.videoHeight,
|
|
280
|
+
duration: video.duration
|
|
281
|
+
});
|
|
282
|
+
}, {
|
|
283
|
+
once: true
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function waitVideoFrameReady(video) {
|
|
290
|
+
return new Promise(resolve => {
|
|
291
|
+
if ("requestVideoFrameCallback" in video) {
|
|
292
|
+
video.requestVideoFrameCallback(() => resolve());
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
if (video.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA) {
|
|
296
|
+
resolve();
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
video.addEventListener("loadeddata", () => resolve(), {
|
|
300
|
+
once: true
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
export { PaintVideo, Video, VideoData, VideoDecoder, waitVideoFrameReady };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{MathHelper as e,ImageData as t,isArray as r,dataProcessor as o,useModule as n,registerUI as d,Image as i,Plugin as s,PaintImage as c,isObject as u,Creator as a,LeaferVideo as l,Resource as p,Platform as h}from"@leafer-ui/draw";function m(e,t,r,o){var n,d=arguments.length,i=d<3?t:null===o?o=Object.getOwnPropertyDescriptor(t,r):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)i=Reflect.decorate(e,t,r,o);else for(var s=e.length-1;s>=0;s--)(n=e[s])&&(i=(d<3?n(i):d>3?n(t,r,i):n(t,r))||i);return d>3&&i&&Object.defineProperty(t,r,i),i}function f(e,t,r,o){return new(r||(r=Promise))(function(n,d){function i(e){try{c(o.next(e))}catch(e){d(e)}}function s(e){try{c(o.throw(e))}catch(e){d(e)}}function c(e){var t;e.done?n(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(i,s)}c((o=o.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;class v{get currentTime(){return this.decoder.currentTime}set currentTime(t){const r=this.decoder,o=e.within(t,0,this.duration);r.currentTime=o}get volume(){return this.decoder.volume}set volume(t){const r=this.decoder,o=e.within(t,0,1);r.volume=o,o>0&&r.muted&&(r.muted=!1)}get paused(){return this.decoder.paused}get ended(){return this.decoder.ended}static register(e,t,r){v.decoderMap[e]={Decoder:t,force:r}}load(){}play(){this.decoder.play()}pause(){this.decoder.pause()}render(e,t,r,o,n,d,i,s,c){let{paused:u}=i;const a=this.decoder;a.ended&&(a.pause(),i.paused=!0,i.loop=(i.loop||0)+1),!1===u&&a.paused?this.play():!0!==u||a.paused?i.paused=a.paused:this.pause(),e.drawImage(a,t,r,o,n),a.paused||d.forceRender()}destroyDecoder(){const e=this.decoder;e&&(e.pause(),e.src="",e.load(),this.decoder=null)}close(){this.destroyDecoder()}}v.decoderMap={};class y extends t{get __urlType(){return"video"}}class g{get playerOptions(){const{fill:e}=(this.element||this).__;return r(e)&&e.find(e=>"video"===e.type)}get decoder(){const{playerOptions:e}=this;return e&&e.image&&e.image.view}get duration(){const e=this.decoder;return e&&e.duration}get currentTime(){const e=this.decoder;return e&&e.currentTime}set currentTime(e){const t=this.decoder;t&&(t.currentTime=e,t.decoder.addEventListener("seeked",()=>(this.element||this).forceRender(),{once:!0}))}get volume(){const e=this.decoder;return e&&e.volume}set volume(e){const t=this.decoder;t&&(t.volume=e)}get paused(){const e=this.decoder;return e&&e.paused}get ended(){const e=this.decoder;return e&&e.ended}constructor(e){this.element=e}togglePlay(){const{playerOptions:e}=this;e&&(!1!==e.paused?this.play():this.pause())}play(){const{playerOptions:e}=this;e&&!1!==e.paused&&(e.paused=!1,(this.element||this).forceRender())}pause(){const{playerOptions:e}=this;e&&(e.paused=!0,this.decoder.pause())}stop(){const{playerOptions:e}=this;e&&(e.nowIndex=0,this.currentTime=0,this.pause())}}let w=class extends i{get __tag(){return"Video"}};function T(e){return new Promise(t=>{"requestVideoFrameCallback"in e?e.requestVideoFrameCallback(()=>t()):e.readyState>=HTMLMediaElement.HAVE_CURRENT_DATA?t():e.addEventListener("loadeddata",()=>t(),{once:!0})})}m([o(y)],w.prototype,"__",void 0),w=m([n(g),d()],w),s.add("video"),c.video=function(e,t){e.film=!0,e.nowIndex=0,u(t)&&(e.paused=t.paused,e.loop=t.loop)},a.video=function(e){return new l(e)},p.loadVideo=function(e,t){return new Promise((r,o)=>{const n={url:e};t&&(n.format=t);const d=a.video(n);p.set(e,d),d.load(()=>r(d),e=>o(e))})},l.prototype.render=function(e,t,r,o,n,d,i,s,c){this.view.render(e,t,r,o,n,d,i,s,c)},h.origin.loadVideo=function(e,t,r){return f(this,void 0,void 0,function*(){const t=function(e,t=!1,r=!1){const o=document.createElement("video");return o.src=e,o.muted=t,o.loop=r,o.playsInline=!0,o.crossOrigin="anonymous",o}(e),r=new v,{width:o,height:n,duration:d}=yield function(e){return new Promise(t=>{e.readyState>=1?t({width:e.videoWidth,height:e.videoHeight,duration:e.duration}):e.addEventListener("loadedmetadata",()=>{t({width:e.videoWidth,height:e.videoHeight,duration:e.duration})},{once:!0})})}(t);return r.decoder=t,r.width=o,r.height=n,r.duration=d,yield T(t),r})};export{g as PaintVideo,w as Video,y as VideoData,v as VideoDecoder,T as waitVideoFrameReady};
|
|
2
|
+
//# sourceMappingURL=video.esm.min.js.map
|