UNPKG

1.87 kBJavaScriptView Raw
1/* eslint no-invalid-this: 0 */
2export default function(cmt) {
3 var that = this;
4 var ct = this.media ? this.media.currentTime : Date.now() / 1000;
5 var pbr = this.media ? this.media.playbackRate : 1;
6 function willCollide(cr, cmt) {
7 if (cmt.mode === 'top' || cmt.mode === 'bottom') {
8 return ct - cr.time < that._.duration;
9 }
10 var crTotalWidth = that._.width + cr.width;
11 var crElapsed = crTotalWidth * (ct - cr.time) * pbr / that._.duration;
12 if (cr.width > crElapsed) {
13 return true;
14 }
15 // (rtl mode) the right end of `cr` move out of left side of stage
16 var crLeftTime = that._.duration + cr.time - ct;
17 var cmtTotalWidth = that._.width + cmt.width;
18 var cmtTime = that.media ? cmt.time : cmt._utc;
19 var cmtElapsed = cmtTotalWidth * (ct - cmtTime) * pbr / that._.duration;
20 var cmtArrival = that._.width - cmtElapsed;
21 // (rtl mode) the left end of `cmt` reach the left side of stage
22 var cmtArrivalTime = that._.duration * cmtArrival / (that._.width + cmt.width);
23 return crLeftTime > cmtArrivalTime;
24 }
25 var crs = this._.space[cmt.mode];
26 var last = 0;
27 var curr = 0;
28 for (var i = 1; i < crs.length; i++) {
29 var cr = crs[i];
30 var requiredRange = cmt.height;
31 if (cmt.mode === 'top' || cmt.mode === 'bottom') {
32 requiredRange += cr.height;
33 }
34 if (cr.range - cr.height - crs[last].range >= requiredRange) {
35 curr = i;
36 break;
37 }
38 if (willCollide(cr, cmt)) {
39 last = i;
40 }
41 }
42 var channel = crs[last].range;
43 var crObj = {
44 range: channel + cmt.height,
45 time: this.media ? cmt.time : cmt._utc,
46 width: cmt.width,
47 height: cmt.height
48 };
49 crs.splice(last + 1, curr - last - 1, crObj);
50
51 if (cmt.mode === 'bottom') {
52 return this._.height - cmt.height - channel % this._.height;
53 }
54 return channel % (this._.height - cmt.height);
55}