1 """
2 * Copyright 2008 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License") you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http:#www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 """
16
17
18
19
20 from pyjamas import DOM
21 from pyjamas import log
22 from pyjamas.ui.Widget import Widget
23
24 from pyjamas.Canvas.Color import Color
25
28
29 """*
30 * Deferred binding implementation of GWTCanvas.
31 """
33
35 self.canvasContext = None
36
37 - def arc(self, x, y, radius, startAngle, endAngle, antiClockwise):
38 self.canvasContext.arc(x,y,radius,startAngle,endAngle,antiClockwise)
39
40
43
44
45 - def clear(self, width, height):
47
48
51
52
57
58
61
62
63 - def fillText(self, text, sourceX, sourceY, maxWidth=None):
64
65
66
67 try:
68 if maxWidth is None:
69 self.canvasContext.fillText(text, sourceX, sourceY)
70 else:
71 self.canvasContext.fillText(text, sourceX, sourceY, maxWidth)
72 except:
73 self.saveContext()
74 self.translate(sourceX, sourceY)
75 try:
76 text = unicode(text)
77 self.canvasContext.mozDrawText(text)
78 except:
79 self.canvasContext.drawText(text)
80 self.restoreContext()
81
82 - def drawImage(self, img, sourceX, sourceY, sourceWidth=None, sourceHeight=None, destX=None, destY=None, destWidth=None, destHeight=None):
83
84 if isinstance(img, Widget):
85 img = img.getElement()
86 if sourceWidth is None:
87 self.canvasContext.drawImage(img,sourceX,sourceY)
88 else:
89 self.canvasContext.drawImage(img,sourceX,sourceY,sourceWidth,sourceHeight,destX,destY,destWidth,destHeight)
90
91
92
94 self.canvasContext.fill()
95
96
97 - def fillRect(self, startX, startY, width, height):
98 self.canvasContext.fillRect(startX,startY,width,height)
99
100
102 return self.canvasContext.globalAlpha
103
104
106 return self.canvasContext.globalCompositeOperation
107
108
110 return DOM.getElementPropertyInt(elem, "height")
111
112
114 return self.canvasContext.lineCap
115
116
118 return self.canvasContext.lineJoin
119
120
122 return self.canvasContext.lineWidth
123
124
126 return self.canvasContext.miterLimit
127
128
130 return DOM.getElementPropertyInt(elem, "width")
131
132
134 self.canvasContext.lineTo(x,y)
135
136
138 self.canvasContext.moveTo(x,y)
139
140
143
144
145 - def rect(self, x, y, width, height):
146 self.canvasContext.rect(x,y,width,height)
147
148
149 - def restoreContext(self):
150 self.canvasContext.restore()
151
152
154 self.canvasContext.rotate(angle)
155
156
157 - def saveContext(self):
158 self.canvasContext.save()
159
160
162 self.canvasContext.scale(x,y)
163
164
167
168
171
172
175
176
178 if isinstance(gradient, Color):
179 gradient = str(gradient)
180 elif not isinstance(gradient, str):
181 gradient = gradient.getObject()
182 self.canvasContext.strokeStyle = cvt(gradient)
183
184
186 if isinstance(gradient, Color):
187 gradient = str(gradient)
188 elif not isinstance(gradient, str):
189 gradient = gradient.getObject()
190 self.canvasContext.fillStyle = cvt(gradient)
191
193 self.canvasContext.globalAlpha = alpha
194
195
197 self.canvasContext.globalCompositeOperation = cvt(globalCompositeOperation)
198
199
201 self.canvasContext.lineCap = cvt(lineCap)
202
203
205 self.canvasContext.lineJoin = cvt(lineJoin)
206
207
209 self.canvasContext.lineWidth = width
210
211
213 self.canvasContext.miterLimit = miterLimit
214
215
218
219
222
223
225 self.canvasContext.stroke()
226
227
228 - def strokeRect(self, startX, startY, width, height):
229 self.canvasContext.strokeRect(startX,startY,width,height)
230
233
234
237
238
239 - def clearRect(self, startX, startY, width, height):
240 self.canvasContext.clearRect(startX,startY,width,height)
241
242
243 - def setCanvasContext(self, ctx):
244 self.canvasContext = ctx
245