Chromium Code Reviews| Index: samplecode/SampleClock.cpp |
| diff --git a/samplecode/SampleClock.cpp b/samplecode/SampleClock.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a060307d1216e0455c12239df966c3ba775820f6 |
| --- /dev/null |
| +++ b/samplecode/SampleClock.cpp |
| @@ -0,0 +1,329 @@ |
| + |
|
tfarina
2013/09/10 21:59:02
rm this blank line.
|
| +/* |
| + * Copyright 2011 Google Inc. |
|
tfarina
2013/09/10 21:59:02
2013?
|
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| +#include "SampleCode.h" |
| + |
| +#include "SkCanvas.h" |
| +#include "SkRandom.h" |
| +#include "SkRRect.h" |
| +#include "SkTime.h" |
| + |
| +// Implementation in C++ of Mozilla Canvas2D benchmark Canvas Clock Test |
| + |
| +#define USE_PATH 1 |
| + |
| +class ClockView : public SampleView { |
| +public: |
| + ClockView() {} |
| + |
| +protected: |
| + // overrides from SkEventSink |
| + virtual bool onQuery(SkEvent* evt) { |
| + if (SampleCode::TitleQ(*evt)) { |
| + SampleCode::TitleR(evt, "Clock"); |
| + return true; |
| + } |
| + return this->INHERITED::onQuery(evt); |
| + } |
| + |
| + static void fill_pts(SkPoint pts[], size_t n, SkMWCRandom* rand) { |
| + for (size_t i = 0; i < n; i++) |
| + pts[i].set(rand->nextUScalar1() * 640, rand->nextUScalar1() * 480); |
| + } |
| + |
| + virtual void onDrawContent(SkCanvas* canvas) { |
|
tfarina
2013/09/10 21:59:02
SK_OVERRIDE?
|
| + SkPaint paintFill; |
| + SkPaint paintStroke; |
| + SkPath path; |
| + |
| + canvas->save(); |
| + canvas->translate(150, 150); |
| + canvas->scale(0.4f, 0.4f); |
| + canvas->rotate(-180.f/2.f); |
| + |
| + paintFill.setAntiAlias(true); |
| + paintFill.setColor(SK_ColorBLACK); |
| + paintStroke.setAntiAlias(true); |
| + paintStroke.setStyle(SkPaint::kStroke_Style); |
| + paintStroke.setColor(SK_ColorBLACK); |
| + paintStroke.setStrokeWidth(8); |
| + paintStroke.setStrokeCap(SkPaint::kRound_Cap); |
| + |
| + // Hour marks |
| + SkRect rect; |
| +#ifndef USE_PATH |
| + rect = SkRect::MakeLTRB(200-4, -4, 240+4, 4); |
| + SkRRect rrect; |
| + SkVector radii[4] = {{4,4}, {4,4}, {4,4}, {4,4}}; |
| + rrect.setRectRadii(rect, radii); |
| +#endif |
| + canvas->save(); |
| + for (int i=0;i<12;i++){ |
| + canvas->rotate(180.f/6.f); |
| +#ifdef USE_PATH |
| + path.reset(); |
| + path.moveTo(200,0); |
| + path.lineTo(240,0); |
| + canvas->drawPath(path, paintStroke); |
| +#else |
| + canvas->drawRRect(rrect, paintFill); |
| +#endif |
| + } |
| + canvas->restore(); |
| + |
| + // Minute marks |
| + canvas->save(); |
| +#ifdef USE_PATH |
| + paintStroke.setStrokeWidth(5); |
| +#else |
| + rect = SkRect::MakeLTRB(231.5f, -2.5f, 242.5, 2.5f); |
| + radii[0] = SkPoint::Make(2.5f,2.5f); |
| + radii[1] = SkPoint::Make(2.5f,2.5f); |
| + radii[2] = SkPoint::Make(2.5f,2.5f); |
| + radii[3] = SkPoint::Make(2.5f,2.5f); |
| + rrect.setRectRadii(rect, radii); |
| +#endif |
| + for (int i=0;i<60;i++){ |
| + if (i%5 == 0) { |
| + canvas->rotate(180.f/30.f); |
| + continue; |
| + } |
| +#ifdef USE_PATH |
| + path.reset(); |
| + path.moveTo(234,0); |
| + path.lineTo(240,0); |
| + canvas->drawPath(path, paintStroke); |
| +#else |
| + canvas->drawRRect(rrect, paintFill); |
| +#endif |
| + canvas->rotate(180.f/30.f); |
| + } |
| + canvas->restore(); |
| + |
| + SkTime::DateTime time; |
| + SkTime::GetDateTime(&time); |
| + time.fHour = time.fHour >= 12 ? time.fHour-12 : time.fHour; |
| + paintFill.setColor(SK_ColorBLACK); |
| + |
| + // Write hours |
| + canvas->save(); |
| + canvas->rotate(time.fHour*(180.f/6.f) + time.fMinute*(180.f/360.f) |
| + + time.fSecond*(180.f/21600.f) ); |
| +#ifdef USE_PATH |
| + paintStroke.setStrokeWidth(14); |
| + path.reset(); |
| + path.moveTo(-20,0); |
| + path.lineTo(80,0); |
| + canvas->drawPath(path, paintStroke); |
| +#else |
| + rect = SkRect::MakeLTRB(-20-7, -7, 80+7, 7); |
| + radii[0] = SkPoint::Make(7,7); |
| + radii[1] = SkPoint::Make(7,7); |
| + radii[2] = SkPoint::Make(7,7); |
| + radii[3] = SkPoint::Make(7,7); |
| + rrect.setRectRadii(rect, radii); |
| + canvas->drawRRect(rrect, paintFill); |
| +#endif |
| + canvas->restore(); |
| + |
| + // Write minutes |
| + canvas->save(); |
| + canvas->rotate(time.fMinute*(180.f/30.f) |
| + + time.fSecond*(180.f/1800.f) ); |
| +#ifdef USE_PATH |
| + paintStroke.setStrokeWidth(10); |
| + path.reset(); |
| + path.moveTo(-56,0); |
| + path.lineTo(224,0); |
| + canvas->drawPath(path, paintStroke); |
| +#else |
| + rect = SkRect::MakeLTRB(-56-5, -5, 224+5, 5); |
| + radii[0] = SkPoint::Make(5,5); |
| + radii[1] = SkPoint::Make(5,5); |
| + radii[2] = SkPoint::Make(5,5); |
| + radii[3] = SkPoint::Make(5,5); |
| + rrect.setRectRadii(rect, radii); |
| + canvas->drawRRect(rrect, paintFill); |
| +#endif |
| + canvas->restore(); |
| + |
| + // Write seconds |
| + canvas->save(); |
| + canvas->rotate(time.fSecond*(180.f/30.f)); |
| + paintFill.setColor(0xffd40000); |
| + paintStroke.setColor(0xffd40000); |
| + paintStroke.setStrokeWidth(6); |
| +#ifdef USE_PATH |
| + path.reset(); |
| + path.moveTo(-60,0); |
| + path.lineTo(166,0); |
| + canvas->drawPath(path, paintStroke); |
| +#else |
| + rect = SkRect::MakeLTRB(-60-3, -3, 166+3, 3); |
| + radii[0] = SkPoint::Make(3,3); |
| + radii[1] = SkPoint::Make(3,3); |
| + radii[2] = SkPoint::Make(3,3); |
| + radii[3] = SkPoint::Make(3,3); |
| + rrect.setRectRadii(rect, radii); |
| + canvas->drawRRect(rrect, paintFill); |
| +#endif |
| + rect = SkRect::MakeLTRB(-20, -20, 20, 20); |
| +#ifdef USE_PATH |
| + path.reset(); |
| + path.arcTo(rect, 0, 0, false); |
| + path.addOval(rect, SkPath::kCCW_Direction); |
| + path.arcTo(rect, 360, 0, true); |
| + canvas->drawPath(path, paintFill); |
| +#else |
| + canvas->drawOval(rect, paintFill); |
| +#endif |
| + rect = SkRect::MakeLTRB(-20+190, -20, 20+190, 20); |
| +#ifdef USE_PATH |
| + path.reset(); |
| + path.arcTo(rect, 0, 0, false); |
| + path.addOval(rect, SkPath::kCCW_Direction); |
| + path.arcTo(rect, 360, 0, true); |
| + canvas->drawPath(path, paintStroke); |
| +#else |
| + canvas->drawOval(rect, paintStroke); |
| +#endif |
| + paintFill.setColor(0xff505050); |
| +#ifdef USE_PATH |
| + rect = SkRect::MakeLTRB(-6, -6, 6, 6); |
| + path.arcTo(rect, 0, 0, false); |
| + path.addOval(rect, SkPath::kCCW_Direction); |
| + path.arcTo(rect, 360, 0, true); |
| + canvas->drawPath(path, paintFill); |
| +#else |
| + canvas->drawOval(rect, paintFill); |
| + rect = SkRect::MakeLTRB(-6, -6, 6, 6); |
| + canvas->drawOval(rect, paintFill); |
| +#endif |
| + canvas->restore(); |
| + |
| + paintStroke.setStrokeWidth(18); |
| + paintStroke.setColor(0xff325FA2); |
| + rect = SkRect::MakeLTRB(-284, -284, 284, 284); |
| +#ifdef USE_PATH |
| + path.reset(); |
| + path.arcTo(rect, 0, 0, false); |
| + path.addOval(rect, SkPath::kCCW_Direction); |
| + path.arcTo(rect, 360, 0, true); |
| + canvas->drawPath(path, paintStroke); |
| +#else |
| + canvas->drawOval(rect, paintStroke); |
| +#endif |
| + |
| + canvas->restore(); |
| + |
| + this->inval(NULL); |
| + } |
| + |
| +private: |
| + |
| + typedef SampleView INHERITED; |
| +}; |
| + |
| +////////////////////////////////////////////////////////////////////////////// |
| + |
| +static SkView* MyFactory() { return new ClockView; } |
| +static SkViewRegister reg(MyFactory); |
| + |
| +////////////////////////////////////////////////////////////////////////////// |
| + |
| +// Original canvas source, for reference |
| +// |
| +//ctx.save(); |
| +//ctx.clearRect(0,0,300,300); |
| +//ctx.translate(150,150); |
| +//ctx.scale(0.4,0.4); |
| +//ctx.rotate(-Math.PI/2); |
| +//ctx.strokeStyle = "black"; |
| +//ctx.fillStyle = "white"; |
| +//ctx.lineWidth = 8; |
| +//ctx.lineCap = "round"; |
| +// |
| +//// Hour marks |
| +//ctx.save(); |
| +//for (var i=0;i<12;i++){ |
| +// ctx.beginPath(); |
| +// ctx.rotate(Math.PI/6); |
| +// ctx.moveTo(200,0); |
| +// ctx.lineTo(240,0); |
| +// ctx.stroke(); |
| +//} |
| +//ctx.restore(); |
| +// |
| +//// Minute marks |
| +//ctx.save(); |
| +//ctx.lineWidth = 5; |
| +//for (i=0;i<60;i++){ |
| +// if (i%5!=0) { |
| +// ctx.beginPath(); |
| +// ctx.moveTo(234,0); |
| +// ctx.lineTo(240,0); |
| +// ctx.stroke(); |
| +// } |
| +// ctx.rotate(Math.PI/30); |
| +//} |
| +//ctx.restore(); |
| +// |
| +//var sec = now.getSeconds() + (now.getMilliseconds()/1000.0); |
| +//var min = now.getMinutes(); |
| +//var hr = now.getHours(); |
| +//hr = hr>=12 ? hr-12 : hr; |
| +// |
| +//ctx.fillStyle = "black"; |
| +// |
| +//// write Hours |
| +//ctx.save(); |
| +//ctx.rotate( hr*(Math.PI/6) + (Math.PI/360)*min + (Math.PI/21600)*sec ) |
| +//ctx.lineWidth = 14; |
| +//ctx.beginPath(); |
| +//ctx.moveTo(-20,0); |
| +//ctx.lineTo(80,0); |
| +//ctx.stroke(); |
| +//ctx.restore(); |
| +// |
| +//// write Minutes |
| +//ctx.save(); |
| +//ctx.rotate( (Math.PI/30)*min + (Math.PI/1800)*sec ) |
| +//ctx.lineWidth = 10; |
| +//ctx.beginPath(); |
| +//ctx.moveTo(-56,0); |
| +//ctx.lineTo(224,0); |
| +//ctx.stroke(); |
| +//ctx.restore(); |
| +// |
| +//// Write seconds |
| +//ctx.save(); |
| +//ctx.rotate(sec * Math.PI/30); |
| +//ctx.strokeStyle = "#D40000"; |
| +//ctx.fillStyle = "#D40000"; |
| +//ctx.lineWidth = 6; |
| +//ctx.beginPath(); |
| +//ctx.moveTo(-60,0); |
| +//ctx.lineTo(166,0); |
| +//ctx.stroke(); |
| +//ctx.beginPath(); |
| +//ctx.arc(0,0,20,0,Math.PI*2,true); |
| +//ctx.fill(); |
| +//ctx.beginPath(); |
| +//ctx.arc(190,0,20,0,Math.PI*2,true); |
| +//ctx.stroke(); |
| +//ctx.fillStyle = "#555"; |
| +//ctx.arc(0,0,6,0,Math.PI*2,true); |
| +//ctx.fill(); |
| +//ctx.restore(); |
| +// |
| +//ctx.beginPath(); |
| +//ctx.lineWidth = 18; |
| +//ctx.strokeStyle = '#325FA2'; |
| +//ctx.arc(0,0,284,0,Math.PI*2,true); |
| +//ctx.stroke(); |
| +// |
| +//ctx.restore(); |