Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 192 lines (142 sloc) 6.11 kB
a730bc1 add GWTCanvas demo
lkcl authored
1 """
2 * Copyright 2008 Google Inc.
57980f4 copyright update
lkcl authored
3 * Copyright (C) 2009 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
a730bc1 add GWTCanvas demo
lkcl authored
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6 * use this file except in compliance with the License. You may obtain a copy of
7 * the License at
8 *
9 * http:#www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 """
17
18 import math
19
20 from pyjamas.ui.Composite import Composite
21 from pyjamas.ui.VerticalPanel import VerticalPanel
22
e74e4a6 add GWTCanvas demo
lkcl authored
23 from pyjamas.Canvas import Color
a730bc1 add GWTCanvas demo
lkcl authored
24
25 from SimpleCanvasDemo import SimpleCanvasDemo
26
27
28 # All SimpleCanvasDemos need to set some controls
29 # Even if they don't have any :).
30 class StaticDemoControls(Composite):
31 def __init__(self):
e74e4a6 add GWTCanvas demo
lkcl authored
32 Composite.__init__(self, VerticalPanel())
a730bc1 add GWTCanvas demo
lkcl authored
33
34 """*
35 * Demo that showcases most of the GWTCanvas drawing Api,
36 * as well as transformations, and style settings.
37 """
38 class StaticDemo (SimpleCanvasDemo):
39
40 def __init__(self, theCanvas):
41 SimpleCanvasDemo.__init__(self, theCanvas)
42 self.width = 400
43 self.height = 400
44 self.demoName = "Static Scene"
45
46
47 def createControls(self):
48 self.controls = StaticDemoControls()
49
50
51 """
52 * Test cases derived from javascript test cases by Emil Eklund
53 * and from Mozilla foundation Canvas tutorial. Please note that
54 * his test cases are NOT bound by license since they were
55 * originally derived from mozilla foundation test cases
56 """
57 def drawDemo(self):
58
59 self.canvas.resize(self.width, self.height)
60
61 # Changing the coordinate size will implicitly clear the self.canvas
62 # self.canvas.clear()
63 self.canvas.saveContext()
64
65 self.canvas.setLineWidth(1)
66 self.canvas.setFillStyle(Color.GREEN)
67 self.canvas.fillRect(5, 5, 25, 25)
68 self.canvas.setStrokeStyle(Color.RED)
69 self.canvas.strokeRect(20, 20, 25, 25)
70
71 self.canvas.beginPath()
72
73 self.canvas.setLineWidth(1)
74
75 self.canvas.moveTo(1,1)
76 self.canvas.lineTo(80,80)
77 self.canvas.lineTo(100,20)
78 self.canvas.closePath()
79 self.canvas.stroke()
80
81 self.canvas.setStrokeStyle(Color.BLUE)
82 self.canvas.setFillStyle(Color.BLACK)
83
84 self.canvas.beginPath()
85 self.canvas.moveTo(120,50)
86 self.canvas.lineTo(150,70)
87 self.canvas.lineTo(150,50)
88
89 self.canvas.quadraticCurveTo(150, 150, 80, 80)
90 self.canvas.cubicCurveTo(85,25,75,37,75,40)
91 self.canvas.closePath()
92 self.canvas.fill()
93
d476822 Added SVGCanvas as alternative to GWTCanvas.
Rich Newpol authored
94 self.canvas.setFont("bold 16px Verdana")
95 self.canvas.fillText("PYJS",20,120)
96
a730bc1 add GWTCanvas demo
lkcl authored
97 self.canvas.beginPath()
98 self.canvas.rect(180,180,80,80)
99 self.canvas.rect(200,200,80,80)
100 self.canvas.stroke()
101
102 self.canvas.beginPath()
103 self.canvas.arc(200, 100, 20, 0, math.pi, False)
104 self.canvas.stroke()
105
106 self.canvas.saveContext()
107 self.canvas.translate(150, 0)
108
109 self.canvas.fillRect(0,0,150,150)
00cd1e0 add GWTCanvas demo
lkcl authored
110 self.canvas.setFillStyle(Color.Color("#09F"))
a730bc1 add GWTCanvas demo
lkcl authored
111 self.canvas.fillRect(15,15,120,120)
00cd1e0 add GWTCanvas demo
lkcl authored
112 self.canvas.setFillStyle(Color.Color("#FFF"))
a730bc1 add GWTCanvas demo
lkcl authored
113 self.canvas.setGlobalAlpha(0.5)
114 self.canvas.fillRect(30,30,90,90)
115 self.canvas.fillRect(45,45,60,60)
116 self.canvas.fillRect(60,60,30,30)
117
118 self.canvas.restoreContext()
119 self.canvas.saveContext()
120 self.canvas.translate(10, 140)
121
00cd1e0 add GWTCanvas demo
lkcl authored
122 self.canvas.setFillStyle(Color.Color("#FD0"))
a730bc1 add GWTCanvas demo
lkcl authored
123 self.canvas.fillRect(0,0,75,75)
00cd1e0 add GWTCanvas demo
lkcl authored
124 self.canvas.setFillStyle(Color.Color("#6C0"))
a730bc1 add GWTCanvas demo
lkcl authored
125 self.canvas.fillRect(75,0,75,75)
00cd1e0 add GWTCanvas demo
lkcl authored
126 self.canvas.setFillStyle(Color.Color("#09F"))
a730bc1 add GWTCanvas demo
lkcl authored
127 self.canvas.fillRect(0,75,75,75)
00cd1e0 add GWTCanvas demo
lkcl authored
128 self.canvas.setFillStyle(Color.Color("#F30"))
a730bc1 add GWTCanvas demo
lkcl authored
129 self.canvas.fillRect(75,75,75,75)
00cd1e0 add GWTCanvas demo
lkcl authored
130 self.canvas.setFillStyle(Color.Color("#FFF"))
a730bc1 add GWTCanvas demo
lkcl authored
131
132 self.canvas.setGlobalAlpha(0.2)
133
134 for i in range(7):
135 self.canvas.beginPath()
136 self.canvas.arc(75, 75, 10 + (10 * i), 0, math.pi * 2, False)
137 self.canvas.fill()
138
139
140 self.canvas.saveContext()
141 self.canvas.setGlobalAlpha(0.8)
142 self.canvas.beginPath()
143 self.canvas.arc(75,75,50,0, math.pi * 2,True); # Outer circle
144 self.canvas.moveTo(110,75)
145 self.canvas.arc(75,75,35,0, math.pi,False); # Mouth
146 self.canvas.moveTo(65,65)
147 self.canvas.arc(60,65,5,0, math.pi * 2,True); # Left eye
148 self.canvas.moveTo(95,65)
149 self.canvas.arc(90,65,5,0, math.pi * 2,True); # Right eye
150 self.canvas.stroke()
151 self.canvas.restoreContext()
152
153 self.canvas.restoreContext()
154
155 self.canvas.beginPath()
156 self.canvas.arc(200, 200, 20, 0, math.pi * 2, False)
157 self.canvas.stroke()
158
159 self.canvas.saveContext()
160 self.canvas.setGlobalAlpha(1.0)
161
162 for i in range(6):
163 # Loop through rings (from inside to out)
164 self.canvas.saveContext()
00cd1e0 add GWTCanvas demo
lkcl authored
165 self.canvas.setFillStyle(Color.Color((51 * i),(255 - 51 * i),255))
a730bc1 add GWTCanvas demo
lkcl authored
166
167 for j in range(i*6):
168 # draw individual dots
169 self.canvas.rotate(math.pi * 2 / ( i * 6.))
170 self.canvas.beginPath()
171
172 self.canvas.rect(0, i * 12.5,5,5)
173 self.canvas.fill()
174
175
176 self.canvas.restoreContext()
177
178
179 self.canvas.restoreContext()
180
181 self.canvas.restoreContext()
182 # self.canvas.clear()
183
184
185 def stopDemo(self):
186 # This demo is not animated so this is a no-op
187 pass
188
189
190
191
Something went wrong with that request. Please try again.