UNPKG

1.84 kBJavaScriptView Raw
1import domEngine from '../engine/dom.js';
2import canvasEngine from '../engine/canvas.js';
3import { bindEvents } from '../internal/events.js';
4import play from '../internal/play.js';
5import seek from '../internal/seek.js';
6import { formatMode, resetSpace } from '../utils.js';
7
8/* eslint-disable no-invalid-this */
9export default function(opt) {
10 this._ = {};
11 this.container = opt.container || document.createElement('div');
12 this.media = opt.media;
13 this._.visible = true;
14
15 /* eslint-disable no-undef */
16 /* istanbul ignore next */
17 if (process.env.ENGINE === 'dom') {
18 this.engine = 'dom';
19 this._.engine = domEngine;
20 }
21 /* istanbul ignore next */
22 if (process.env.ENGINE === 'canvas') {
23 this.engine = 'canvas';
24 this._.engine = canvasEngine;
25 }
26 /* istanbul ignore else */
27 if (!process.env.ENGINE) {
28 this.engine = (opt.engine || 'DOM').toLowerCase();
29 this._.engine = this.engine === 'canvas' ? canvasEngine : domEngine;
30 }
31 /* eslint-enable no-undef */
32 this._.requestID = 0;
33
34 this._.speed = Math.max(0, opt.speed) || 144;
35 this._.duration = 4;
36
37 this.comments = opt.comments || [];
38 this.comments.sort(function(a, b) {
39 return a.time - b.time;
40 });
41 for (var i = 0; i < this.comments.length; i++) {
42 this.comments[i].mode = formatMode(this.comments[i].mode);
43 }
44 this._.runningList = [];
45 this._.position = 0;
46
47 this._.paused = true;
48 if (this.media) {
49 this._.listener = {};
50 bindEvents.call(this, this._.listener);
51 }
52
53 this._.stage = this._.engine.init(this.container);
54 this._.stage.style.cssText += 'position:relative;pointer-events:none;';
55
56 this.resize();
57 this.container.appendChild(this._.stage);
58
59 this._.space = {};
60 resetSpace(this._.space);
61
62 if (!this.media || !this.media.paused) {
63 seek.call(this);
64 play.call(this);
65 }
66 return this;
67}