Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 189 lines (140 sloc) 6.016 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
94 self.canvas.beginPath()
95 self.canvas.rect(180,180,80,80)
96 self.canvas.rect(200,200,80,80)
97 self.canvas.stroke()
98
99 self.canvas.beginPath()
100 self.canvas.arc(200, 100, 20, 0, math.pi, False)
101 self.canvas.stroke()
102
103 self.canvas.saveContext()
104 self.canvas.translate(150, 0)
105
106 self.canvas.fillRect(0,0,150,150)
00cd1e0 add GWTCanvas demo
lkcl authored
107 self.canvas.setFillStyle(Color.Color("#09F"))
a730bc1 add GWTCanvas demo
lkcl authored
108 self.canvas.fillRect(15,15,120,120)
00cd1e0 add GWTCanvas demo
lkcl authored
109 self.canvas.setFillStyle(Color.Color("#FFF"))
a730bc1 add GWTCanvas demo
lkcl authored
110 self.canvas.setGlobalAlpha(0.5)
111 self.canvas.fillRect(30,30,90,90)
112 self.canvas.fillRect(45,45,60,60)
113 self.canvas.fillRect(60,60,30,30)
114
115 self.canvas.restoreContext()
116 self.canvas.saveContext()
117 self.canvas.translate(10, 140)
118
00cd1e0 add GWTCanvas demo
lkcl authored
119 self.canvas.setFillStyle(Color.Color("#FD0"))
a730bc1 add GWTCanvas demo
lkcl authored
120 self.canvas.fillRect(0,0,75,75)
00cd1e0 add GWTCanvas demo
lkcl authored
121 self.canvas.setFillStyle(Color.Color("#6C0"))
a730bc1 add GWTCanvas demo
lkcl authored
122 self.canvas.fillRect(75,0,75,75)
00cd1e0 add GWTCanvas demo
lkcl authored
123 self.canvas.setFillStyle(Color.Color("#09F"))
a730bc1 add GWTCanvas demo
lkcl authored
124 self.canvas.fillRect(0,75,75,75)
00cd1e0 add GWTCanvas demo
lkcl authored
125 self.canvas.setFillStyle(Color.Color("#F30"))
a730bc1 add GWTCanvas demo
lkcl authored
126 self.canvas.fillRect(75,75,75,75)
00cd1e0 add GWTCanvas demo
lkcl authored
127 self.canvas.setFillStyle(Color.Color("#FFF"))
a730bc1 add GWTCanvas demo
lkcl authored
128
129 self.canvas.setGlobalAlpha(0.2)
130
131 for i in range(7):
132 self.canvas.beginPath()
133 self.canvas.arc(75, 75, 10 + (10 * i), 0, math.pi * 2, False)
134 self.canvas.fill()
135
136
137 self.canvas.saveContext()
138 self.canvas.setGlobalAlpha(0.8)
139 self.canvas.beginPath()
140 self.canvas.arc(75,75,50,0, math.pi * 2,True); # Outer circle
141 self.canvas.moveTo(110,75)
142 self.canvas.arc(75,75,35,0, math.pi,False); # Mouth
143 self.canvas.moveTo(65,65)
144 self.canvas.arc(60,65,5,0, math.pi * 2,True); # Left eye
145 self.canvas.moveTo(95,65)
146 self.canvas.arc(90,65,5,0, math.pi * 2,True); # Right eye
147 self.canvas.stroke()
148 self.canvas.restoreContext()
149
150 self.canvas.restoreContext()
151
152 self.canvas.beginPath()
153 self.canvas.arc(200, 200, 20, 0, math.pi * 2, False)
154 self.canvas.stroke()
155
156 self.canvas.saveContext()
157 self.canvas.setGlobalAlpha(1.0)
158
159 for i in range(6):
160 # Loop through rings (from inside to out)
161 self.canvas.saveContext()
00cd1e0 add GWTCanvas demo
lkcl authored
162 self.canvas.setFillStyle(Color.Color((51 * i),(255 - 51 * i),255))
a730bc1 add GWTCanvas demo
lkcl authored
163
164 for j in range(i*6):
165 # draw individual dots
166 self.canvas.rotate(math.pi * 2 / ( i * 6.))
167 self.canvas.beginPath()
168
169 self.canvas.rect(0, i * 12.5,5,5)
170 self.canvas.fill()
171
172
173 self.canvas.restoreContext()
174
175
176 self.canvas.restoreContext()
177
178 self.canvas.restoreContext()
179 # self.canvas.clear()
180
181
182 def stopDemo(self):
183 # This demo is not animated so this is a no-op
184 pass
185
186
187
188
Something went wrong with that request. Please try again.