import { IDanmaMessage , Point , Rect } from '../interface/IDanmaMessage' ; import { IDanmaTrackInfo } from '../interface/IDanmaTrack' ; import { BaseMessage } from './BaseMessage' ; interface TextMessageConfig { mMsg : string ; color : string ; strokeColor : string ; fontSize : number ; fontFamily : string ; } export default class TextMessage extends BaseMessage { private textMessageConfig : TextMessageConfig = { mMsg : '' , color : '#FFF' , strokeColor : '#000' , fontSize : 30 , fontFamily : '黑体' } ; constructor ( msg ) { super ( ) ; this . textMessageConfig = Object . assign ( { } , this . textMessageConfig , msg ) ; } onCreate ( callback ) { callback ( true ) ; } onMeasure ( ctx , trackInfo ) { ctx . textAlign = 'left' ; ctx . textBaseline = 'middle' ; ctx . font = `${ this . textMessageConfig . fontSize }px ${ this . textMessageConfig . fontFamily }` ; let res = ctx . measureText ( this . textMessageConfig . mMsg ) ; return { width : res . width , height : trackInfo . height } ; } onLayout ( ctx , trackInfo ) { if ( ! this . isLayout ) { this . isLayout = true ; return { top : trackInfo . top + trackInfo . height / 2 , left : ctx . canvas . width } ; } this . position . left -= 3 ; return this . position ; } onDraw ( ctx , trackInfo ) { ctx . fillStyle = this . textMessageConfig . strokeColor ; ctx . strokeText ( this . textMessageConfig . mMsg , this . position . left , this . position . top ) ; ctx . fillStyle = this . textMessageConfig . color ; ctx . fillText ( this . textMessageConfig . mMsg , this . position . left , this . position . top ) ; } onDestroyed ( ) { return this . position . left < - this . size . width ; } }