@@ -9,6 +9,7 @@ import * as constants from '../core/constants';
9
9
import { Image } from '../image/p5.Image' ;
10
10
import { Vector } from '../math/p5.Vector' ;
11
11
import { Shape } from '../shape/custom_shapes' ;
12
+ import { States } from './States' ;
12
13
13
14
class ClonableObject {
14
15
constructor ( obj = { } ) {
@@ -70,15 +71,7 @@ class Renderer {
70
71
}
71
72
72
73
// Renderer state machine
73
- this . states = Object . assign ( { } , Renderer . states ) ;
74
- // Clone properties that support it
75
- for ( const key in this . states ) {
76
- if ( this . states [ key ] instanceof Array ) {
77
- this . states [ key ] = this . states [ key ] . slice ( ) ;
78
- } else if ( this . states [ key ] && this . states [ key ] . clone instanceof Function ) {
79
- this . states [ key ] = this . states [ key ] . clone ( ) ;
80
- }
81
- }
74
+ this . states = new States ( Renderer . states ) ;
82
75
83
76
this . states . strokeColor = new Color ( [ 0 , 0 , 0 ] ) ;
84
77
this . states . fillColor = new Color ( [ 255 , 255 , 255 ] ) ;
@@ -122,33 +115,25 @@ class Renderer {
122
115
// and push it into the push pop stack
123
116
push ( ) {
124
117
this . _pushPopDepth ++ ;
125
- const currentStates = Object . assign ( { } , this . states ) ;
126
- // Clone properties that support it
127
- for ( const key in currentStates ) {
128
- if ( currentStates [ key ] instanceof Array ) {
129
- currentStates [ key ] = currentStates [ key ] . slice ( ) ;
130
- } else if ( currentStates [ key ] && currentStates [ key ] . clone instanceof Function ) {
131
- currentStates [ key ] = currentStates [ key ] . clone ( ) ;
132
- }
133
- }
134
- this . _pushPopStack . push ( currentStates ) ;
135
- return currentStates ;
118
+ this . _pushPopStack . push ( this . states . getDiff ( ) ) ;
136
119
}
137
120
138
121
// Pop the previous states out of the push pop stack and
139
122
// assign it back to the current state
140
123
pop ( ) {
141
124
this . _pushPopDepth -- ;
142
- Object . assign ( this . states , this . _pushPopStack . pop ( ) ) ;
143
- this . updateShapeVertexProperties ( ) ;
144
- this . updateShapeProperties ( ) ;
125
+ const diff = this . _pushPopStack . pop ( ) || { } ;
126
+ const modified = this . states . getModified ( ) ;
127
+ this . states . applyDiff ( diff ) ;
128
+ this . updateShapeVertexProperties ( modified ) ;
129
+ this . updateShapeProperties ( modified ) ;
145
130
}
146
131
147
132
bezierOrder ( order ) {
148
133
if ( order === undefined ) {
149
134
return this . states . bezierOrder ;
150
135
} else {
151
- this . states . bezierOrder = order ;
136
+ this . states . setValue ( ' bezierOrder' , order ) ;
152
137
this . updateShapeProperties ( ) ;
153
138
}
154
139
}
@@ -165,6 +150,7 @@ class Renderer {
165
150
if ( value === undefined ) {
166
151
return this . states . splineProperties [ key ] ;
167
152
} else {
153
+ this . states . setValue ( 'splineProperties' , this . states . splineProperties . clone ( ) ) ;
168
154
this . states . splineProperties [ key ] = value ;
169
155
}
170
156
this . updateShapeProperties ( ) ;
@@ -192,7 +178,7 @@ class Renderer {
192
178
if ( d === undefined ) {
193
179
return this . states . curveDetail ;
194
180
} else {
195
- this . states . curveDetail = d ;
181
+ this . states . setValue ( ' curveDetail' , d ) ;
196
182
}
197
183
}
198
184
@@ -287,31 +273,31 @@ class Renderer {
287
273
}
288
274
289
275
fill ( ...args ) {
290
- this . states . fillSet = true ;
291
- this . states . fillColor = this . _pInst . color ( ...args ) ;
276
+ this . states . setValue ( ' fillSet' , true ) ;
277
+ this . states . setValue ( ' fillColor' , this . _pInst . color ( ...args ) ) ;
292
278
this . updateShapeVertexProperties ( ) ;
293
279
}
294
280
295
281
noFill ( ) {
296
- this . states . fillColor = null ;
282
+ this . states . setValue ( ' fillColor' , null ) ;
297
283
}
298
284
299
285
strokeWeight ( w ) {
300
286
if ( w === undefined ) {
301
287
return this . states . strokeWeight ;
302
288
} else {
303
- this . states . strokeWeight = w ;
289
+ this . states . setValue ( ' strokeWeight' , w ) ;
304
290
}
305
291
}
306
292
307
293
stroke ( ...args ) {
308
- this . states . strokeSet = true ;
309
- this . states . strokeColor = this . _pInst . color ( ...args ) ;
294
+ this . states . setValue ( ' strokeSet' , true ) ;
295
+ this . states . setValue ( ' strokeColor' , this . _pInst . color ( ...args ) ) ;
310
296
this . updateShapeVertexProperties ( ) ;
311
297
}
312
298
313
299
noStroke ( ) {
314
- this . states . strokeColor = null ;
300
+ this . states . setValue ( ' strokeColor' , null ) ;
315
301
}
316
302
317
303
getCommonVertexProperties ( ) {
@@ -324,25 +310,31 @@ class Renderer {
324
310
}
325
311
}
326
312
327
- updateShapeProperties ( ) {
328
- this . currentShape . bezierOrder ( this . states . bezierOrder ) ;
329
- this . currentShape . splineProperty ( 'ends' , this . states . splineProperties . ends ) ;
330
- this . currentShape . splineProperty ( 'tightness' , this . states . splineProperties . tightness ) ;
313
+ updateShapeProperties ( modified ) {
314
+ if ( ! modified || modified . bezierOrder || modified . splineProperties ) {
315
+ const shape = this . currentShape ;
316
+ shape . bezierOrder ( this . states . bezierOrder ) ;
317
+ shape . splineProperty ( 'ends' , this . states . splineProperties . ends ) ;
318
+ shape . splineProperty ( 'tightness' , this . states . splineProperties . tightness ) ;
319
+ }
331
320
}
332
321
333
- updateShapeVertexProperties ( ) {
322
+ updateShapeVertexProperties ( modified ) {
334
323
const props = this . getCommonVertexProperties ( ) ;
335
- for ( const key in props ) {
336
- this . currentShape [ key ] ( props [ key ] ) ;
324
+ if ( ! modified || Object . keys ( modified ) . some ( ( k ) => k in props ) ) {
325
+ const shape = this . currentShape ;
326
+ for ( const key in props ) {
327
+ shape [ key ] ( props [ key ] ) ;
328
+ }
337
329
}
338
330
}
339
331
340
332
textSize ( s ) {
341
333
if ( typeof s === 'number' ) {
342
- this . states . textSize = s ;
334
+ this . states . setValue ( ' textSize' , s ) ;
343
335
if ( ! this . states . leadingSet ) {
344
336
// only use a default value if not previously set (#5181)
345
- this . states . textLeading = s * constants . _DEFAULT_LEADMULT ;
337
+ this . states . setValue ( ' textLeading' , s * constants . _DEFAULT_LEADMULT ) ;
346
338
}
347
339
return this . _applyTextProperties ( ) ;
348
340
}
@@ -352,8 +344,8 @@ class Renderer {
352
344
353
345
textLeading ( l ) {
354
346
if ( typeof l === 'number' ) {
355
- this . states . leadingSet = true ;
356
- this . states . textLeading = l ;
347
+ this . states . setValue ( ' leadingSet' , true ) ;
348
+ this . states . setValue ( ' textLeading' , l ) ;
357
349
return this . _pInst ;
358
350
}
359
351
@@ -368,7 +360,7 @@ class Renderer {
368
360
s === constants . BOLD ||
369
361
s === constants . BOLDITALIC
370
362
) {
371
- this . states . fontStyle = s ;
363
+ this . states . setValue ( ' fontStyle' , s ) ;
372
364
}
373
365
374
366
return this . _applyTextProperties ( ) ;
@@ -393,10 +385,10 @@ class Renderer {
393
385
394
386
textAlign ( h , v ) {
395
387
if ( typeof h !== 'undefined' ) {
396
- this . states . textAlign = h ;
388
+ this . states . setValue ( ' textAlign' , h ) ;
397
389
398
390
if ( typeof v !== 'undefined' ) {
399
- this . states . textBaseline = v ;
391
+ this . states . setValue ( ' textBaseline' , v ) ;
400
392
}
401
393
402
394
return this . _applyTextProperties ( ) ;
@@ -409,7 +401,7 @@ class Renderer {
409
401
}
410
402
411
403
textWrap ( wrapStyle ) {
412
- this . states . textWrap = wrapStyle ;
404
+ this . states . setValue ( ' textWrap' , wrapStyle ) ;
413
405
return this . states . textWrap ;
414
406
}
415
407
@@ -657,8 +649,8 @@ class Renderer {
657
649
658
650
_updateTextMetrics ( ) {
659
651
if ( this . _isOpenType ( ) ) {
660
- this . states . textAscent = this . states . textFont . _textAscent ( ) ;
661
- this . states . textDescent = this . states . textFont . _textDescent ( ) ;
652
+ this . states . setValue ( ' textAscent' , this . states . textFont . _textAscent ( ) ) ;
653
+ this . states . setValue ( ' textDescent' , this . states . textFont . _textDescent ( ) ) ;
662
654
return this ;
663
655
}
664
656
@@ -694,8 +686,8 @@ class Renderer {
694
686
695
687
document . body . removeChild ( container ) ;
696
688
697
- this . states . textAscent = ascent ;
698
- this . states . textDescent = descent ;
689
+ this . states . setValue ( ' textAscent' , ascent ) ;
690
+ this . states . setValue ( ' textDescent' , descent ) ;
699
691
700
692
return this ;
701
693
}
0 commit comments