1
1
const QR = require ( './qrcode.js' ) ;
2
2
const GD = require ( './gradient.js' ) ;
3
3
4
+ export const penCache = {
5
+ // 用于存储带 id 的 view 的 rect 信息
6
+ viewRect : { } ,
7
+ textLines : { } ,
8
+ } ;
9
+ export const clearPenCache = id => {
10
+ if ( id ) {
11
+ penCache . viewRect [ id ] = null ;
12
+ penCache . textLines [ id ] = null ;
13
+ } else {
14
+ penCache . viewRect = { } ;
15
+ penCache . textLines = { } ;
16
+ }
17
+ } ;
4
18
export default class Painter {
5
19
constructor ( ctx , data ) {
6
20
this . ctx = ctx ;
7
21
this . data = data ;
8
22
}
9
23
10
- isMoving = false ;
11
- // 动态模板时的缓存,加速渲染
12
- movingCache = { } ;
13
- callbackInfo = { } ;
14
- // 用于存储带 id 的 view 的 rect 信息
15
- viewRect = { } ;
16
- paint ( callback , isMoving , movingCache ) {
24
+ paint ( callback ) {
17
25
this . style = {
18
26
width : this . data . width . toPx ( ) ,
19
27
height : this . data . height . toPx ( ) ,
20
28
} ;
21
- if ( isMoving ) {
22
- this . isMoving = true ;
23
- this . movingCache = movingCache ;
24
- }
29
+
25
30
this . _background ( ) ;
26
31
for ( const view of this . data . views ) {
27
32
this . _drawAbsolute ( view ) ;
28
33
}
29
34
this . ctx . draw ( false , ( ) => {
30
- callback && callback ( this . callbackInfo ) ;
35
+ callback && callback ( ) ;
31
36
} ) ;
32
37
}
33
38
@@ -262,14 +267,14 @@ export default class Painter {
262
267
width = Math . round ( view . sWidth / ratio ) ;
263
268
height = Math . round ( view . sHeight / ratio ) ;
264
269
} else if ( view . css . width === 'auto' ) {
265
- height = view . css . height . toPx ( false , this . style . height , this . viewRect ) ;
270
+ height = view . css . height . toPx ( false , this . style . height ) ;
266
271
width = ( view . sWidth / view . sHeight ) * height ;
267
272
} else if ( view . css . height === 'auto' ) {
268
- width = view . css . width . toPx ( false , this . style . width , this . viewRect ) ;
273
+ width = view . css . width . toPx ( false , this . style . width ) ;
269
274
height = ( view . sHeight / view . sWidth ) * width ;
270
275
} else {
271
- width = view . css . width . toPx ( false , this . style . width , this . viewRect ) ;
272
- height = view . css . height . toPx ( false , this . style . height , this . viewRect ) ;
276
+ width = view . css . width . toPx ( false , this . style . width ) ;
277
+ height = view . css . height . toPx ( false , this . style . height ) ;
273
278
}
274
279
break ;
275
280
}
@@ -278,41 +283,44 @@ export default class Painter {
278
283
console . error ( 'You should set width and height' ) ;
279
284
return ;
280
285
}
281
- width = view . css . width . toPx ( false , this . style . width , this . viewRect ) ;
282
- height = view . css . height . toPx ( false , this . style . height , this . viewRect ) ;
286
+ width = view . css . width . toPx ( false , this . style . width ) ;
287
+ height = view . css . height . toPx ( false , this . style . height ) ;
283
288
break ;
284
289
}
285
290
let x ;
286
291
if ( view . css && view . css . right ) {
287
292
if ( typeof view . css . right === 'string' ) {
288
- x = this . style . width - view . css . right . toPx ( true , this . style . width , this . viewRect ) ;
293
+ x = this . style . width - view . css . right . toPx ( true , this . style . width ) ;
289
294
} else {
290
295
// 可以用数组方式,把文字长度计算进去
291
296
// [right, 文字id, 乘数(默认 1)]
292
297
const rights = view . css . right ;
293
- x = this . style . width - rights [ 0 ] . toPx ( true , this . style . width ) - this . viewRect [ rights [ 1 ] ] . width * ( rights [ 2 ] || 1 ) ;
298
+ x =
299
+ this . style . width -
300
+ rights [ 0 ] . toPx ( true , this . style . width ) -
301
+ penCache . viewRect [ rights [ 1 ] ] . width * ( rights [ 2 ] || 1 ) ;
294
302
}
295
303
} else if ( view . css && view . css . left ) {
296
304
if ( typeof view . css . left === 'string' ) {
297
- x = view . css . left . toPx ( true , this . style . width , this . viewRect ) ;
305
+ x = view . css . left . toPx ( true , this . style . width ) ;
298
306
} else {
299
307
const lefts = view . css . left ;
300
- x = lefts [ 0 ] . toPx ( true , this . style . width ) + this . viewRect [ lefts [ 1 ] ] . width * ( lefts [ 2 ] || 1 ) ;
308
+ x = lefts [ 0 ] . toPx ( true , this . style . width ) + penCache . viewRect [ lefts [ 1 ] ] . width * ( lefts [ 2 ] || 1 ) ;
301
309
}
302
310
} else {
303
311
x = 0 ;
304
312
}
305
313
//const y = view.css && view.css.bottom ? this.style.height - height - view.css.bottom.toPx(true) : (view.css && view.css.top ? view.css.top.toPx(true) : 0);
306
314
let y ;
307
315
if ( view . css && view . css . bottom ) {
308
- y = this . style . height - height - view . css . bottom . toPx ( true , this . style . height , this . viewRect ) ;
316
+ y = this . style . height - height - view . css . bottom . toPx ( true , this . style . height ) ;
309
317
} else {
310
318
if ( view . css && view . css . top ) {
311
319
if ( typeof view . css . top === 'string' ) {
312
- y = view . css . top . toPx ( true , this . style . height , this . viewRect ) ;
320
+ y = view . css . top . toPx ( true , this . style . height ) ;
313
321
} else {
314
322
const tops = view . css . top ;
315
- y = tops [ 0 ] . toPx ( true , this . style . height ) + this . viewRect [ tops [ 1 ] ] . height * ( tops [ 2 ] || 1 ) ;
323
+ y = tops [ 0 ] . toPx ( true , this . style . height ) + penCache . viewRect [ tops [ 1 ] ] . height * ( tops [ 2 ] || 1 ) ;
316
324
}
317
325
} else {
318
326
y = 0 ;
@@ -396,7 +404,7 @@ export default class Painter {
396
404
}
397
405
this . _doShadow ( view ) ;
398
406
if ( view . id ) {
399
- this . viewRect [ view . id ] = {
407
+ penCache . viewRect [ view . id ] = {
400
408
width,
401
409
height,
402
410
left : x ,
@@ -520,10 +528,9 @@ export default class Painter {
520
528
this . ctx . save ( ) ;
521
529
const { width, height, extra } = this . _preProcess ( view , view . css . background && view . css . borderRadius ) ;
522
530
this . ctx . fillStyle = view . css . color || 'black' ;
523
- if ( this . isMoving && JSON . stringify ( this . movingCache ) !== JSON . stringify ( { } ) ) {
524
- this . viewRect [ view . id ] = this . movingCache . viewRect ;
531
+ if ( view . id && penCache . textLines [ view . id ] ) {
525
532
this . ctx . textAlign = view . css . textAlign ? view . css . textAlign : 'left' ;
526
- for ( const i of this . movingCache . lineArray ) {
533
+ for ( const i of penCache . textLines [ view . id ] ) {
527
534
const { measuredWith, text, x, y, textDecoration } = i ;
528
535
if ( view . css . textStyle === 'stroke' ) {
529
536
this . ctx . strokeText ( text , x , y , measuredWith ) ;
@@ -550,12 +557,7 @@ export default class Painter {
550
557
const _w = this . ctx . measureText ( textArray [ i ] ) . width ;
551
558
textWidth = _w > textWidth ? _w : textWidth ;
552
559
}
553
- this . viewRect [ view . id ] . width = width ? ( textWidth < width ? textWidth : width ) : textWidth ;
554
- if ( ! this . isMoving ) {
555
- Object . assign ( this . callbackInfo , {
556
- viewRect : this . viewRect [ view . id ] ,
557
- } ) ;
558
- }
560
+ penCache . viewRect [ view . id ] . width = width ? ( textWidth < width ? textWidth : width ) : textWidth ;
559
561
}
560
562
let lineIndex = 0 ;
561
563
for ( let j = 0 ; j < textArray . length ; ++ j ) {
@@ -619,6 +621,7 @@ export default class Painter {
619
621
lineX = x ;
620
622
break ;
621
623
}
624
+
622
625
const y =
623
626
- ( height / 2 ) +
624
627
( lineIndex === 0 ? view . css . fontSize . toPx ( ) : view . css . fontSize . toPx ( ) + lineIndex * lineHeight ) ;
@@ -661,16 +664,16 @@ export default class Painter {
661
664
this . ctx . strokeStyle = view . css . color ;
662
665
this . ctx . stroke ( ) ;
663
666
}
664
- if ( ! this . isMoving ) {
665
- this . callbackInfo . lineArray
666
- ? this . callbackInfo . lineArray . push ( {
667
+ if ( view . id ) {
668
+ penCache . textLines [ view . id ]
669
+ ? penCache . textLines [ view . id ] . push ( {
667
670
text,
668
671
x,
669
672
y,
670
673
measuredWith,
671
674
textDecoration,
672
675
} )
673
- : ( this . callbackInfo . lineArray = [
676
+ : ( penCache . textLines [ view . id ] = [
674
677
{
675
678
text,
676
679
x,
0 commit comments