OLD | NEW |
---|---|
(Empty) | |
1 | |
tfarina
2013/09/10 21:59:02
rm this blank line.
| |
2 /* | |
3 * Copyright 2011 Google Inc. | |
tfarina
2013/09/10 21:59:02
2013?
| |
4 * | |
5 * Use of this source code is governed by a BSD-style license that can be | |
6 * found in the LICENSE file. | |
7 */ | |
8 #include "SampleCode.h" | |
9 | |
10 #include "SkCanvas.h" | |
11 #include "SkRandom.h" | |
12 #include "SkRRect.h" | |
13 #include "SkTime.h" | |
14 | |
15 // Implementation in C++ of Mozilla Canvas2D benchmark Canvas Clock Test | |
16 | |
17 #define USE_PATH 1 | |
18 | |
19 class ClockView : public SampleView { | |
20 public: | |
21 ClockView() {} | |
22 | |
23 protected: | |
24 // overrides from SkEventSink | |
25 virtual bool onQuery(SkEvent* evt) { | |
26 if (SampleCode::TitleQ(*evt)) { | |
27 SampleCode::TitleR(evt, "Clock"); | |
28 return true; | |
29 } | |
30 return this->INHERITED::onQuery(evt); | |
31 } | |
32 | |
33 static void fill_pts(SkPoint pts[], size_t n, SkMWCRandom* rand) { | |
34 for (size_t i = 0; i < n; i++) | |
35 pts[i].set(rand->nextUScalar1() * 640, rand->nextUScalar1() * 480); | |
36 } | |
37 | |
38 virtual void onDrawContent(SkCanvas* canvas) { | |
tfarina
2013/09/10 21:59:02
SK_OVERRIDE?
| |
39 SkPaint paintFill; | |
40 SkPaint paintStroke; | |
41 SkPath path; | |
42 | |
43 canvas->save(); | |
44 canvas->translate(150, 150); | |
45 canvas->scale(0.4f, 0.4f); | |
46 canvas->rotate(-180.f/2.f); | |
47 | |
48 paintFill.setAntiAlias(true); | |
49 paintFill.setColor(SK_ColorBLACK); | |
50 paintStroke.setAntiAlias(true); | |
51 paintStroke.setStyle(SkPaint::kStroke_Style); | |
52 paintStroke.setColor(SK_ColorBLACK); | |
53 paintStroke.setStrokeWidth(8); | |
54 paintStroke.setStrokeCap(SkPaint::kRound_Cap); | |
55 | |
56 // Hour marks | |
57 SkRect rect; | |
58 #ifndef USE_PATH | |
59 rect = SkRect::MakeLTRB(200-4, -4, 240+4, 4); | |
60 SkRRect rrect; | |
61 SkVector radii[4] = {{4,4}, {4,4}, {4,4}, {4,4}}; | |
62 rrect.setRectRadii(rect, radii); | |
63 #endif | |
64 canvas->save(); | |
65 for (int i=0;i<12;i++){ | |
66 canvas->rotate(180.f/6.f); | |
67 #ifdef USE_PATH | |
68 path.reset(); | |
69 path.moveTo(200,0); | |
70 path.lineTo(240,0); | |
71 canvas->drawPath(path, paintStroke); | |
72 #else | |
73 canvas->drawRRect(rrect, paintFill); | |
74 #endif | |
75 } | |
76 canvas->restore(); | |
77 | |
78 // Minute marks | |
79 canvas->save(); | |
80 #ifdef USE_PATH | |
81 paintStroke.setStrokeWidth(5); | |
82 #else | |
83 rect = SkRect::MakeLTRB(231.5f, -2.5f, 242.5, 2.5f); | |
84 radii[0] = SkPoint::Make(2.5f,2.5f); | |
85 radii[1] = SkPoint::Make(2.5f,2.5f); | |
86 radii[2] = SkPoint::Make(2.5f,2.5f); | |
87 radii[3] = SkPoint::Make(2.5f,2.5f); | |
88 rrect.setRectRadii(rect, radii); | |
89 #endif | |
90 for (int i=0;i<60;i++){ | |
91 if (i%5 == 0) { | |
92 canvas->rotate(180.f/30.f); | |
93 continue; | |
94 } | |
95 #ifdef USE_PATH | |
96 path.reset(); | |
97 path.moveTo(234,0); | |
98 path.lineTo(240,0); | |
99 canvas->drawPath(path, paintStroke); | |
100 #else | |
101 canvas->drawRRect(rrect, paintFill); | |
102 #endif | |
103 canvas->rotate(180.f/30.f); | |
104 } | |
105 canvas->restore(); | |
106 | |
107 SkTime::DateTime time; | |
108 SkTime::GetDateTime(&time); | |
109 time.fHour = time.fHour >= 12 ? time.fHour-12 : time.fHour; | |
110 paintFill.setColor(SK_ColorBLACK); | |
111 | |
112 // Write hours | |
113 canvas->save(); | |
114 canvas->rotate(time.fHour*(180.f/6.f) + time.fMinute*(180.f/360.f) | |
115 + time.fSecond*(180.f/21600.f) ); | |
116 #ifdef USE_PATH | |
117 paintStroke.setStrokeWidth(14); | |
118 path.reset(); | |
119 path.moveTo(-20,0); | |
120 path.lineTo(80,0); | |
121 canvas->drawPath(path, paintStroke); | |
122 #else | |
123 rect = SkRect::MakeLTRB(-20-7, -7, 80+7, 7); | |
124 radii[0] = SkPoint::Make(7,7); | |
125 radii[1] = SkPoint::Make(7,7); | |
126 radii[2] = SkPoint::Make(7,7); | |
127 radii[3] = SkPoint::Make(7,7); | |
128 rrect.setRectRadii(rect, radii); | |
129 canvas->drawRRect(rrect, paintFill); | |
130 #endif | |
131 canvas->restore(); | |
132 | |
133 // Write minutes | |
134 canvas->save(); | |
135 canvas->rotate(time.fMinute*(180.f/30.f) | |
136 + time.fSecond*(180.f/1800.f) ); | |
137 #ifdef USE_PATH | |
138 paintStroke.setStrokeWidth(10); | |
139 path.reset(); | |
140 path.moveTo(-56,0); | |
141 path.lineTo(224,0); | |
142 canvas->drawPath(path, paintStroke); | |
143 #else | |
144 rect = SkRect::MakeLTRB(-56-5, -5, 224+5, 5); | |
145 radii[0] = SkPoint::Make(5,5); | |
146 radii[1] = SkPoint::Make(5,5); | |
147 radii[2] = SkPoint::Make(5,5); | |
148 radii[3] = SkPoint::Make(5,5); | |
149 rrect.setRectRadii(rect, radii); | |
150 canvas->drawRRect(rrect, paintFill); | |
151 #endif | |
152 canvas->restore(); | |
153 | |
154 // Write seconds | |
155 canvas->save(); | |
156 canvas->rotate(time.fSecond*(180.f/30.f)); | |
157 paintFill.setColor(0xffd40000); | |
158 paintStroke.setColor(0xffd40000); | |
159 paintStroke.setStrokeWidth(6); | |
160 #ifdef USE_PATH | |
161 path.reset(); | |
162 path.moveTo(-60,0); | |
163 path.lineTo(166,0); | |
164 canvas->drawPath(path, paintStroke); | |
165 #else | |
166 rect = SkRect::MakeLTRB(-60-3, -3, 166+3, 3); | |
167 radii[0] = SkPoint::Make(3,3); | |
168 radii[1] = SkPoint::Make(3,3); | |
169 radii[2] = SkPoint::Make(3,3); | |
170 radii[3] = SkPoint::Make(3,3); | |
171 rrect.setRectRadii(rect, radii); | |
172 canvas->drawRRect(rrect, paintFill); | |
173 #endif | |
174 rect = SkRect::MakeLTRB(-20, -20, 20, 20); | |
175 #ifdef USE_PATH | |
176 path.reset(); | |
177 path.arcTo(rect, 0, 0, false); | |
178 path.addOval(rect, SkPath::kCCW_Direction); | |
179 path.arcTo(rect, 360, 0, true); | |
180 canvas->drawPath(path, paintFill); | |
181 #else | |
182 canvas->drawOval(rect, paintFill); | |
183 #endif | |
184 rect = SkRect::MakeLTRB(-20+190, -20, 20+190, 20); | |
185 #ifdef USE_PATH | |
186 path.reset(); | |
187 path.arcTo(rect, 0, 0, false); | |
188 path.addOval(rect, SkPath::kCCW_Direction); | |
189 path.arcTo(rect, 360, 0, true); | |
190 canvas->drawPath(path, paintStroke); | |
191 #else | |
192 canvas->drawOval(rect, paintStroke); | |
193 #endif | |
194 paintFill.setColor(0xff505050); | |
195 #ifdef USE_PATH | |
196 rect = SkRect::MakeLTRB(-6, -6, 6, 6); | |
197 path.arcTo(rect, 0, 0, false); | |
198 path.addOval(rect, SkPath::kCCW_Direction); | |
199 path.arcTo(rect, 360, 0, true); | |
200 canvas->drawPath(path, paintFill); | |
201 #else | |
202 canvas->drawOval(rect, paintFill); | |
203 rect = SkRect::MakeLTRB(-6, -6, 6, 6); | |
204 canvas->drawOval(rect, paintFill); | |
205 #endif | |
206 canvas->restore(); | |
207 | |
208 paintStroke.setStrokeWidth(18); | |
209 paintStroke.setColor(0xff325FA2); | |
210 rect = SkRect::MakeLTRB(-284, -284, 284, 284); | |
211 #ifdef USE_PATH | |
212 path.reset(); | |
213 path.arcTo(rect, 0, 0, false); | |
214 path.addOval(rect, SkPath::kCCW_Direction); | |
215 path.arcTo(rect, 360, 0, true); | |
216 canvas->drawPath(path, paintStroke); | |
217 #else | |
218 canvas->drawOval(rect, paintStroke); | |
219 #endif | |
220 | |
221 canvas->restore(); | |
222 | |
223 this->inval(NULL); | |
224 } | |
225 | |
226 private: | |
227 | |
228 typedef SampleView INHERITED; | |
229 }; | |
230 | |
231 ////////////////////////////////////////////////////////////////////////////// | |
232 | |
233 static SkView* MyFactory() { return new ClockView; } | |
234 static SkViewRegister reg(MyFactory); | |
235 | |
236 ////////////////////////////////////////////////////////////////////////////// | |
237 | |
238 // Original canvas source, for reference | |
239 // | |
240 //ctx.save(); | |
241 //ctx.clearRect(0,0,300,300); | |
242 //ctx.translate(150,150); | |
243 //ctx.scale(0.4,0.4); | |
244 //ctx.rotate(-Math.PI/2); | |
245 //ctx.strokeStyle = "black"; | |
246 //ctx.fillStyle = "white"; | |
247 //ctx.lineWidth = 8; | |
248 //ctx.lineCap = "round"; | |
249 // | |
250 //// Hour marks | |
251 //ctx.save(); | |
252 //for (var i=0;i<12;i++){ | |
253 // ctx.beginPath(); | |
254 // ctx.rotate(Math.PI/6); | |
255 // ctx.moveTo(200,0); | |
256 // ctx.lineTo(240,0); | |
257 // ctx.stroke(); | |
258 //} | |
259 //ctx.restore(); | |
260 // | |
261 //// Minute marks | |
262 //ctx.save(); | |
263 //ctx.lineWidth = 5; | |
264 //for (i=0;i<60;i++){ | |
265 // if (i%5!=0) { | |
266 // ctx.beginPath(); | |
267 // ctx.moveTo(234,0); | |
268 // ctx.lineTo(240,0); | |
269 // ctx.stroke(); | |
270 // } | |
271 // ctx.rotate(Math.PI/30); | |
272 //} | |
273 //ctx.restore(); | |
274 // | |
275 //var sec = now.getSeconds() + (now.getMilliseconds()/1000.0); | |
276 //var min = now.getMinutes(); | |
277 //var hr = now.getHours(); | |
278 //hr = hr>=12 ? hr-12 : hr; | |
279 // | |
280 //ctx.fillStyle = "black"; | |
281 // | |
282 //// write Hours | |
283 //ctx.save(); | |
284 //ctx.rotate( hr*(Math.PI/6) + (Math.PI/360)*min + (Math.PI/21600)*sec ) | |
285 //ctx.lineWidth = 14; | |
286 //ctx.beginPath(); | |
287 //ctx.moveTo(-20,0); | |
288 //ctx.lineTo(80,0); | |
289 //ctx.stroke(); | |
290 //ctx.restore(); | |
291 // | |
292 //// write Minutes | |
293 //ctx.save(); | |
294 //ctx.rotate( (Math.PI/30)*min + (Math.PI/1800)*sec ) | |
295 //ctx.lineWidth = 10; | |
296 //ctx.beginPath(); | |
297 //ctx.moveTo(-56,0); | |
298 //ctx.lineTo(224,0); | |
299 //ctx.stroke(); | |
300 //ctx.restore(); | |
301 // | |
302 //// Write seconds | |
303 //ctx.save(); | |
304 //ctx.rotate(sec * Math.PI/30); | |
305 //ctx.strokeStyle = "#D40000"; | |
306 //ctx.fillStyle = "#D40000"; | |
307 //ctx.lineWidth = 6; | |
308 //ctx.beginPath(); | |
309 //ctx.moveTo(-60,0); | |
310 //ctx.lineTo(166,0); | |
311 //ctx.stroke(); | |
312 //ctx.beginPath(); | |
313 //ctx.arc(0,0,20,0,Math.PI*2,true); | |
314 //ctx.fill(); | |
315 //ctx.beginPath(); | |
316 //ctx.arc(190,0,20,0,Math.PI*2,true); | |
317 //ctx.stroke(); | |
318 //ctx.fillStyle = "#555"; | |
319 //ctx.arc(0,0,6,0,Math.PI*2,true); | |
320 //ctx.fill(); | |
321 //ctx.restore(); | |
322 // | |
323 //ctx.beginPath(); | |
324 //ctx.lineWidth = 18; | |
325 //ctx.strokeStyle = '#325FA2'; | |
326 //ctx.arc(0,0,284,0,Math.PI*2,true); | |
327 //ctx.stroke(); | |
328 // | |
329 //ctx.restore(); | |
OLD | NEW |