@@ -47,37 +47,73 @@ define(function (require) {
47
47
} ;
48
48
49
49
/**
50
- * The pushStyle() function saves the current style settings and popStyle()
51
- * restores the prior settings. Note that these functions are always used
52
- * together. They allow you to change the style settings and later return to
53
- * what you had. When a new style is started with pushStyle(), it builds on
54
- * the current style information. The pushStyle() and popStyle() functions
55
- * can be embedded to provide more control. (See the second example above
56
- * for a demonstration.)
57
- *
58
- * The style information controlled by the following functions are included
59
- * in the style: fill(), stroke(), tint(), strokeWeight(), strokeCap(),
60
- * strokeJoin(), imageMode(), rectMode(), ellipseMode(), shapeMode(),
61
- * colorMode(), textAlign(), textFont(), textMode(), textSize(),
62
- * textLeading(), emissive(), specular(), shininess(), ambient()
63
- *
64
- * @method pushStyle
50
+ * The push() function saves the current drawing style settings and
51
+ * transformations, while pop() restores these settings. Note that these
52
+ * functions are always used together. They allow you to change the style
53
+ * and transformation settings and later return to what you had. When a new
54
+ * state is started with push(), it builds on the current style and transform
55
+ * information. The push() and pop() functions can be embedded to provide
56
+ * more control. (See the second example for a demonstration.)
57
+ * <br><br>
58
+ * push() stores information related to the current transformation state
59
+ * and style settings controlled by the following functions: fill(),
60
+ * stroke(), tint(), strokeWeight(), strokeCap(), strokeJoin(),
61
+ * imageMode(), rectMode(), ellipseMode(), colorMode(), textAlign(),
62
+ * textFont(), textMode(), textSize(), textLeading().
63
+ *
64
+ * @method push
65
+ * @example
66
+ * <div>
67
+ * <code>
68
+ * ellipse(0, 50, 33, 33); // Left circle
69
+ *
70
+ * push(); // Start a new drawing state
71
+ * strokeWeight(10);
72
+ * fill(204, 153, 0);
73
+ * translate(50, 0);
74
+ * ellipse(0, 50, 33, 33); // Middle circle
75
+ * pop(); // Restore original state
76
+ *
77
+ * ellipse(100, 50, 33, 33); // Right circle
78
+ * </code>
79
+ * </div>
80
+ * <div>
81
+ * <code>
82
+ * ellipse(0, 50, 33, 33); // Left circle
83
+ *
84
+ * push(); // Start a new drawing state
85
+ * strokeWeight(10);
86
+ * fill(204, 153, 0);
87
+ * ellipse(33, 50, 33, 33); // Left-middle circle
88
+ *
89
+ * push(); // Start another new drawing state
90
+ * stroke(0, 102, 153);
91
+ * ellipse(66, 50, 33, 33); // Right-middle circle
92
+ * pop(); // Restore previous state
93
+ *
94
+ * pop(); // Restore original state
95
+ *
96
+ * ellipse(100, 50, 33, 33); // Right circle
97
+ * </code>
98
+ * </div>
65
99
*/
66
- p5 . prototype . pushStyle = function ( ) {
100
+ p5 . prototype . push = function ( ) {
101
+ var ctx = this . canvas . getContext ( '2d' ) ;
102
+ ctx . save ( ) ;
67
103
68
104
this . styles . push ( {
69
- fillStyle : this . canvas . getContext ( '2d' ) . fillStyle , // fill
70
- strokeStyle : this . canvas . getContext ( '2d' ) . strokeStyle , // stroke
71
- lineWidth : this . canvas . getContext ( '2d' ) . lineWidth , // strokeWeight
72
- lineCap : this . canvas . getContext ( '2d' ) . lineCap , // strokeCap
73
- lineJoin : this . canvas . getContext ( '2d' ) . lineJoin , // strokeJoin
105
+ fillStyle : ctx . fillStyle , // fill
106
+ strokeStyle : ctx . strokeStyle , // stroke
107
+ lineWidth : ctx . lineWidth , // strokeWeight
108
+ lineCap : ctx . lineCap , // strokeCap
109
+ lineJoin : ctx . lineJoin , // strokeJoin
74
110
tint : this . _tint , // tint
75
111
imageMode : this . _imageMode , // imageMode
76
112
rectMode : this . _rectMode , // rectMode
77
113
ellipseMode : this . _ellipseMode , // ellipseMode
78
114
// @todo shapeMode
79
115
colorMode : this . _colorMode , // colorMode
80
- textAlign : this . canvas . getContext ( '2d' ) . textAlign , // textAlign
116
+ textAlign : ctx . textAlign , // textAlign
81
117
textFont : this . textFont ,
82
118
textLeading : this . textLeading , // textLeading
83
119
textSize : this . textSize , // textSize
@@ -86,38 +122,88 @@ define(function (require) {
86
122
} ;
87
123
88
124
/**
89
- * The pushStyle() function saves the current style settings and popStyle()
90
- * restores the prior settings; these functions are always used together.
91
- * They allow you to change the style settings and later return to what you
92
- * had. When a new style is started with pushStyle(), it builds on the
93
- * current style information. The pushStyle() and popStyle() functions can
94
- * be embedded to provide more control (see the second example above for
95
- * a demonstration.)
125
+ * The push() function saves the current drawing style settings and
126
+ * transformations, while pop() restores these settings. Note that these
127
+ * functions are always used together. They allow you to change the style
128
+ * and transformation settings and later return to what you had. When a new
129
+ * state is started with push(), it builds on the current style and transform
130
+ * information. The push() and pop() functions can be embedded to provide
131
+ * more control. (See the second example for a demonstration.)
132
+ * <br><br>
133
+ * push() stores information related to the current transformation state
134
+ * and style settings controlled by the following functions: fill(),
135
+ * stroke(), tint(), strokeWeight(), strokeCap(), strokeJoin(),
136
+ * imageMode(), rectMode(), ellipseMode(), colorMode(), textAlign(),
137
+ * textFont(), textMode(), textSize(), textLeading().
96
138
*
97
- * @method popStyle
139
+ * @method pop
140
+ * @example
141
+ * <div>
142
+ * <code>
143
+ * ellipse(0, 50, 33, 33); // Left circle
144
+ *
145
+ * push(); // Start a new drawing state
146
+ * translate(50, 0);
147
+ * strokeWeight(10);
148
+ * fill(204, 153, 0);
149
+ * ellipse(0, 50, 33, 33); // Middle circle
150
+ * pop(); // Restore original state
151
+ *
152
+ * ellipse(100, 50, 33, 33); // Right circle
153
+ * </code>
154
+ * </div>
155
+ * <div>
156
+ * <code>
157
+ * ellipse(0, 50, 33, 33); // Left circle
158
+ *
159
+ * push(); // Start a new drawing state
160
+ * strokeWeight(10);
161
+ * fill(204, 153, 0);
162
+ * ellipse(33, 50, 33, 33); // Left-middle circle
163
+ *
164
+ * push(); // Start another new drawing state
165
+ * stroke(0, 102, 153);
166
+ * ellipse(66, 50, 33, 33); // Right-middle circle
167
+ * pop(); // Restore previous state
168
+ *
169
+ * pop(); // Restore original state
170
+ *
171
+ * ellipse(100, 50, 33, 33); // Right circle
172
+ * </code>
173
+ * </div>
98
174
*/
99
- p5 . prototype . popStyle = function ( ) {
175
+ p5 . prototype . pop = function ( ) {
176
+ var ctx = this . canvas . getContext ( '2d' ) ;
177
+ ctx . restore ( ) ;
100
178
101
179
var lastS = this . styles . pop ( ) ;
102
180
103
- this . canvas . getContext ( '2d' ) . fillStyle = lastS . fillStyle ; // fill
104
- this . canvas . getContext ( '2d' ) . strokeStyle = lastS . strokeStyle ; // stroke
105
- this . canvas . getContext ( '2d' ) . lineWidth = lastS . lineWidth ; // strokeWeight
106
- this . canvas . getContext ( '2d' ) . lineCap = lastS . lineCap ; // strokeCap
107
- this . canvas . getContext ( '2d' ) . lineJoin = lastS . lineJoin ; // strokeJoin
181
+ ctx . fillStyle = lastS . fillStyle ; // fill
182
+ ctx . strokeStyle = lastS . strokeStyle ; // stroke
183
+ ctx . lineWidth = lastS . lineWidth ; // strokeWeight
184
+ ctx . lineCap = lastS . lineCap ; // strokeCap
185
+ ctx . lineJoin = lastS . lineJoin ; // strokeJoin
108
186
this . _tint = lastS . tint ; // tint
109
187
this . _imageMode = lastS . imageMode ; // imageMode
110
188
this . _rectMode = lastS . _rectMode ; // rectMode
111
189
this . _ellipseMode = lastS . ellipseMode ; // elllipseMode
112
190
// @todo shapeMode
113
191
this . _colorMode = lastS . _colorMode ; // colorMode
114
- this . canvas . getContext ( '2d' ) . textAlign = lastS . textAlign ; // textAlign
192
+ ctx . textAlign = lastS . textAlign ; // textAlign
115
193
this . textFont = lastS . textFont ;
116
194
this . textLeading = lastS . textLeading ; // textLeading
117
195
this . textSize = lastS . textSize ; // textSize
118
196
this . textStyle = lastS . textStyle ; // textStyle
119
197
} ;
120
198
199
+ p5 . prototype . pushStyle = function ( ) {
200
+ throw new Error ( 'pushStyle() not used, see push()' ) ;
201
+ } ;
202
+
203
+ p5 . prototype . popStyle = function ( ) {
204
+ throw new Error ( 'popStyle() not used, see pop()' ) ;
205
+ } ;
206
+
121
207
/**
122
208
*
123
209
* Executes the code within draw() one time. This functions allows the
0 commit comments