55} ( this , ( function ( ) { 'use strict' ;
66
77/* eslint no-invalid-this: 0 */
8- var allocate = function ( cmt ) {
8+ function allocate ( cmt ) {
99 var that = this ;
1010 var ct = this . _hasMedia ? this . media . currentTime : Date . now ( ) / 1000 ;
1111 var pbr = this . _hasMedia ? this . media . playbackRate : 1 ;
@@ -52,23 +52,30 @@ var allocate = function(cmt) {
5252 return this . height - cmt . height - channel % this . height ;
5353 }
5454 return channel % ( this . height - cmt . height ) ;
55- } ;
55+ }
5656
57- var createCommentNode = function ( cmt ) {
57+ function createCommentNode ( cmt ) {
5858 var node = document . createElement ( 'div' ) ;
59+ node . style . cssText = 'position:absolute;' ;
60+ if ( typeof cmt . render === 'function' ) {
61+ var $el = cmt . render ( ) ;
62+ if ( $el instanceof HTMLElement ) {
63+ node . appendChild ( $el ) ;
64+ return node ;
65+ }
66+ }
5967 if ( cmt . html === true ) {
6068 node . innerHTML = cmt . text ;
6169 } else {
6270 node . textContent = cmt . text ;
6371 }
64- node . style . cssText = 'position:absolute;' ;
6572 if ( cmt . style ) {
6673 for ( var key in cmt . style ) {
6774 node . style [ key ] = cmt . style [ key ] ;
6875 }
6976 }
7077 return node ;
71- } ;
78+ }
7279
7380var transform = ( function ( ) {
7481 var properties = [
@@ -90,7 +97,7 @@ var transform = (function() {
9097} ( ) ) ;
9198
9299/* eslint no-invalid-this: 0 */
93- var domEngine = function ( ) {
100+ function domEngine ( ) {
94101 var dn = Date . now ( ) / 1000 ;
95102 var ct = this . _hasMedia ? this . media . currentTime : dn ;
96103 var pbr = this . _hasMedia ? this . media . playbackRate : 1 ;
@@ -152,11 +159,11 @@ var domEngine = function() {
152159 if ( cmt . mode === 'rtl' ) cmt . x = this . width - elapsed ;
153160 cmt . node . style [ transform ] = 'translate(' + cmt . x + 'px,' + cmt . y + 'px)' ;
154161 }
155- } ;
162+ }
156163
157164var canvasHeightCache = Object . create ( null ) ;
158165
159- var canvasHeight = function ( font , fontSize ) {
166+ function canvasHeight ( font , fontSize ) {
160167 if ( canvasHeightCache [ font ] ) {
161168 return canvasHeightCache [ font ] ;
162169 }
@@ -180,9 +187,17 @@ var canvasHeight = function(font, fontSize) {
180187 }
181188 canvasHeightCache [ font ] = height ;
182189 return height ;
183- } ;
190+ }
184191
185- var createCommentCanvas = function ( cmt , fontSize ) {
192+ function createCommentCanvas ( cmt , fontSize ) {
193+ if ( typeof cmt . render === 'function' ) {
194+ var cvs = cmt . render ( ) ;
195+ if ( cvs instanceof HTMLCanvasElement ) {
196+ cmt . width = cvs . width ;
197+ cmt . height = cvs . height ;
198+ return cvs ;
199+ }
200+ }
186201 var canvas = document . createElement ( 'canvas' ) ;
187202 var ctx = canvas . getContext ( '2d' ) ;
188203 var style = cmt . canvasStyle || { } ;
@@ -219,10 +234,10 @@ var createCommentCanvas = function(cmt, fontSize) {
219234 }
220235 ctx . fillText ( cmt . text , strokeWidth , baseline ) ;
221236 return canvas ;
222- } ;
237+ }
223238
224239/* eslint no-invalid-this: 0 */
225- var canvasEngine = function ( ) {
240+ function canvasEngine ( ) {
226241 this . stage . context . clearRect ( 0 , 0 , this . width , this . height ) ;
227242 var dn = Date . now ( ) / 1000 ;
228243 var ct = this . _hasMedia ? this . media . currentTime : dn ;
@@ -262,10 +277,9 @@ var canvasEngine = function() {
262277 if ( cmt . mode === 'rtl' ) cmt . x = ( this . width - elapsed + .5 ) | 0 ;
263278 this . stage . context . drawImage ( cmt . canvas , cmt . x , cmt . y ) ;
264279 }
265- } ;
280+ }
266281
267282var raf =
268- /* istanbul ignore next */
269283 window . requestAnimationFrame ||
270284 window . mozRequestAnimationFrame ||
271285 window . webkitRequestAnimationFrame ||
@@ -274,14 +288,13 @@ var raf =
274288 } ;
275289
276290var caf =
277- /* istanbul ignore next */
278291 window . cancelAnimationFrame ||
279292 window . mozCancelAnimationFrame ||
280293 window . webkitCancelAnimationFrame ||
281294 clearTimeout ;
282295
283296/* eslint no-invalid-this: 0 */
284- var play = function ( ) {
297+ function play ( ) {
285298 if ( ! this . visible || ! this . paused ) {
286299 return this ;
287300 }
@@ -300,20 +313,20 @@ var play = function() {
300313 }
301314 this . _requestID = raf ( frame ) ;
302315 return this ;
303- } ;
316+ }
304317
305318/* eslint no-invalid-this: 0 */
306- var pause = function ( ) {
319+ function pause ( ) {
307320 if ( ! this . visible || this . paused ) {
308321 return this ;
309322 }
310323 this . paused = true ;
311324 caf ( this . _requestID ) ;
312325 this . _requestID = 0 ;
313326 return this ;
314- } ;
327+ }
315328
316- var binsearch = function ( arr , prop , key ) {
329+ function binsearch ( arr , prop , key ) {
317330 var mid = 0 ;
318331 var left = 0 ;
319332 var right = arr . length ;
@@ -329,7 +342,7 @@ var binsearch = function(arr, prop, key) {
329342 return left ;
330343 }
331344 return right ;
332- } ;
345+ }
333346
334347function collidableRange ( ) {
335348 var max = 9007199254740991 ;
@@ -354,7 +367,7 @@ function resetSpace(space) {
354367}
355368
356369/* eslint no-invalid-this: 0 */
357- var seek = function ( ) {
370+ function seek ( ) {
358371 if ( ! this . _hasMedia ) {
359372 return this ;
360373 }
@@ -363,7 +376,7 @@ var seek = function() {
363376 var position = binsearch ( this . comments , 'time' , this . media . currentTime ) ;
364377 this . position = Math . max ( 0 , position - 1 ) ;
365378 return this ;
366- } ;
379+ }
367380
368381/* eslint no-invalid-this: 0 */
369382function bindEvents ( _ ) {
@@ -397,14 +410,14 @@ function computeFontSize(el, fontSize) {
397410 }
398411}
399412
400- var formatMode = function ( mode ) {
413+ function formatMode ( mode ) {
401414 if ( ! / ^ ( l t r | t o p | b o t t o m ) $ / i. test ( mode ) ) {
402415 return 'rtl' ;
403416 }
404417 return mode . toLowerCase ( ) ;
405- } ;
418+ }
406419
407- var initMixin = function ( Danmaku ) {
420+ function initMixin ( Danmaku ) {
408421 Danmaku . prototype . init = function ( opt ) {
409422 if ( this . _isInited ) {
410423 return this ;
@@ -489,14 +502,30 @@ var initMixin = function(Danmaku) {
489502 this . _isInited = true ;
490503 return this ;
491504 } ;
492- } ;
505+ }
493506
494- var emitMixin = function ( Danmaku ) {
507+ var properties = [
508+ // meta
509+ 'mode' , 'time' ,
510+ // both engine
511+ 'text' , 'render' ,
512+ // DOM engine
513+ 'html' , 'style' ,
514+ // canvas engine
515+ 'canvasStyle'
516+ ] ;
517+
518+ function emitMixin ( Danmaku ) {
495519 Danmaku . prototype . emit = function ( obj ) {
496520 if ( ! obj || Object . prototype . toString . call ( obj ) !== '[object Object]' ) {
497521 return this ;
498522 }
499- var cmt = JSON . parse ( JSON . stringify ( obj ) ) ;
523+ var cmt = { } ;
524+ for ( var i = 0 ; i < properties . length ; i ++ ) {
525+ if ( obj [ properties [ i ] ] !== undefined ) {
526+ cmt [ properties [ i ] ] = obj [ properties [ i ] ] ;
527+ }
528+ }
500529 cmt . text = ( cmt . text || '' ) . toString ( ) ;
501530 cmt . mode = formatMode ( cmt . mode ) ;
502531 cmt . _utc = Date . now ( ) / 1000 ;
@@ -514,9 +543,9 @@ var emitMixin = function(Danmaku) {
514543 }
515544 return this ;
516545 } ;
517- } ;
546+ }
518547
519- var clearMixin = function ( Danmaku ) {
548+ function clearMixin ( Danmaku ) {
520549 Danmaku . prototype . clear = function ( ) {
521550 if ( this . _useCanvas ) {
522551 this . stage . context . clearRect ( 0 , 0 , this . width , this . height ) ;
@@ -534,9 +563,9 @@ var clearMixin = function(Danmaku) {
534563 this . runningList = [ ] ;
535564 return this ;
536565 } ;
537- } ;
566+ }
538567
539- var destroyMixin = function ( Danmaku ) {
568+ function destroyMixin ( Danmaku ) {
540569 Danmaku . prototype . destroy = function ( ) {
541570 if ( ! this . _isInited ) {
542571 return this ;
@@ -565,9 +594,9 @@ var destroyMixin = function(Danmaku) {
565594 }
566595 return this ;
567596 } ;
568- } ;
597+ }
569598
570- var showMixin = function ( Danmaku ) {
599+ function showMixin ( Danmaku ) {
571600 Danmaku . prototype . show = function ( ) {
572601 if ( this . visible ) {
573602 return this ;
@@ -580,9 +609,9 @@ var showMixin = function(Danmaku) {
580609 play . call ( this ) ;
581610 return this ;
582611 } ;
583- } ;
612+ }
584613
585- var hideMixin = function ( Danmaku ) {
614+ function hideMixin ( Danmaku ) {
586615 Danmaku . prototype . hide = function ( ) {
587616 if ( ! this . visible ) {
588617 return this ;
@@ -592,9 +621,9 @@ var hideMixin = function(Danmaku) {
592621 this . visible = false ;
593622 return this ;
594623 } ;
595- } ;
624+ }
596625
597- var resizeMixin = function ( Danmaku ) {
626+ function resizeMixin ( Danmaku ) {
598627 Danmaku . prototype . resize = function ( ) {
599628 if ( this . _hasInitContainer ) {
600629 this . width = this . container . offsetWidth ;
@@ -615,9 +644,9 @@ var resizeMixin = function(Danmaku) {
615644 this . duration = this . width / this . _speed ;
616645 return this ;
617646 } ;
618- } ;
647+ }
619648
620- var speedMixin = function ( Danmaku ) {
649+ function speedMixin ( Danmaku ) {
621650 Object . defineProperty ( Danmaku . prototype , 'speed' , {
622651 get : function ( ) {
623652 return this . _speed ;
@@ -636,7 +665,7 @@ var speedMixin = function(Danmaku) {
636665 return s ;
637666 }
638667 } ) ;
639- } ;
668+ }
640669
641670function Danmaku ( opt ) {
642671 this . _isInited = false ;
0 commit comments