基础用法
const canvas = document. getElementById ( 'myCanvas' ) ;
const ctx = canvas. getContext ( '2d' ) ;
ctx. font = '20px Arial' ;
ctx. fillStyle = 'blue' ;
ctx. textAlign = 'center' ;
ctx. textBaseline = 'middle' ;
ctx. fillText ( 'Hello, Vue Canvas!' , canvas. width / 2 , canvas. height / 2 ) ;
封装调用
drawText ( text, x, y ) {
const canvas = document. getElementById ( 'myCanvas' ) ;
const ctx = canvas. getContext ( '2d' ) ;
ctx. font = '20px Arial' ;
ctx. fillStyle = 'blue' ;
ctx. textAlign = 'center' ;
ctx. textBaseline = 'middle' ;
ctx. fillText ( text, x, y) ;
}
this . drawText ( 'abc' , 50 , 50 )
循环调用
let serverData = [
{ text : '111111111' } ,
{ text : '222222222' } ,
{ text : '333333333' } ,
{ text : '444444444' }
]
for ( let i = 0 ; i < serverData. length; i++ ) {
this . drawText ( serverData[ i] . text, 300 * i + 50 , 50 )
}