1+ let _canvas ;
2+ /** @returns {CanvasRenderingContext2D } */
3+ function _getTmpContext ( width , height ) {
4+ if ( ! _canvas ) {
5+ _canvas = document . createElement ( 'canvas' ) ;
6+ }
7+ _canvas . width = width ;
8+ _canvas . height = height ;
9+
10+ const context = _canvas . getContext ( '2d' , { willReadFrequently : true } ) ;
11+ context . clearRect ( 0 , 0 , width , height ) ;
12+ return context ;
13+ }
14+
15+ export function fixPatterns ( ) {
16+ ig . ImagePattern . inject ( {
17+ // Only the draw calls and the assignment to this.image1 and this.image2 are changed. (usePatternDraw was removed because it is unused)
18+ initBuffer ( ) {
19+ const scale = ig . system . scale ;
20+ this . sourceX = this . sourceX ? this . sourceX * scale : 0 ;
21+ this . sourceY = this . sourceY ? this . sourceY * scale : 0 ;
22+ this . width = ( this . width ? this . width : this . sourceImage . width ) * scale ;
23+ this . height = ( this . height ? this . height : this . sourceImage . height ) * scale ;
24+ const opt = ig . ImagePattern . OPT ;
25+ const widthFactor = Math . ceil ( ( this . optMode == opt . NONE || this . optMode == opt . REPEAT_Y ? 1 : 256 ) / this . width ) ;
26+ const heightFactor = Math . ceil ( ( this . optMode == opt . NONE || this . optMode == opt . REPEAT_X ? 1 : 256 ) / this . height ) ;
27+ const scaledWidth = widthFactor * this . width ;
28+ const scaledHeight = heightFactor * this . height ;
29+ const context = _getTmpContext ( scaledWidth , this . optMode == opt . REPEAT_X_OR_Y ? this . height : scaledHeight ) ;
30+ for ( let y = 0 ; y < ( this . optMode == opt . REPEAT_X_OR_Y ? 1 : heightFactor ) ; ++ y ) {
31+ for ( let x = 0 ; x < widthFactor ; x ++ ) {
32+ context . drawImage ( this . sourceImage . data , this . sourceX , this . sourceY , this . width , this . height , x * this . width , y * this . height , this . width , this . height ) ;
33+ ig . Image . drawCount ++ ;
34+ }
35+ }
36+ this . image1 = document . createElement ( 'img' ) ;
37+ this . image1 . src = context . canvas . toDataURL ( ) ;
38+
39+ if ( this . optMode == opt . REPEAT_X_OR_Y ) {
40+ const context2 = _getTmpContext ( this . width , scaledHeight ) ;
41+ for ( let y = 0 ; y < heightFactor ; ++ y ) {
42+ context . drawImage ( this . sourceImage . data , this . sourceX , this . sourceY , this . width , this . height , 0 , y * this . height , this . width , this . height ) ;
43+ ig . Image . drawCount ++ ;
44+ }
45+ this . image2 = document . createElement ( 'img' ) ;
46+ this . image2 . src = context2 . canvas . toDataURL ( ) ;
47+ }
48+ this . totalWidth = scaledWidth ;
49+ this . totalHeight = scaledHeight ;
50+ } ,
51+ clearCached ( ) {
52+ if ( this . image1 ) {
53+ this . image1 = null ;
54+ }
55+ if ( this . image2 ) {
56+ this . image2 = null ;
57+ }
58+ } ,
59+ } ) ;
60+ ig . ImagePattern . OPT = {
61+ NONE : 0 ,
62+ REPEAT_X : 1 ,
63+ REPEAT_Y : 2 ,
64+ REPEAT_X_OR_Y : 3 ,
65+ REPEAT_X_AND_Y : 4
66+ } ;
67+ }
0 commit comments