aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar robertphillips <robertphillips@google.com>2016-04-08 13:35:14 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-04-08 13:35:14 -0700
commit651bb5fafe1edd723425dffd478dfa67b113da73 (patch)
tree4699dc4326f07711b94016f39110b4bbc1d74b04
parent7cbffda6c85e0634399d74607db2a82943abe06a (diff)
Make some GMs animate
-rw-r--r--gm/dashcircle.cpp128
-rw-r--r--gm/lighting.cpp38
-rw-r--r--gm/stringart.cpp25
3 files changed, 129 insertions, 62 deletions
diff --git a/gm/dashcircle.cpp b/gm/dashcircle.cpp
index a02c7d0f7b..f631c40476 100644
--- a/gm/dashcircle.cpp
+++ b/gm/dashcircle.cpp
@@ -6,6 +6,7 @@
*/
#include "gm.h"
+#include "SkAnimTimer.h"
#include "SkPath.h"
#include "SkDashPathEffect.h"
@@ -24,55 +25,86 @@ struct DashExample {
{ dash4, SK_ARRAY_COUNT(dash4) }
};
-DEF_SIMPLE_GM(dashcircle, canvas, 900, 1200) {
- SkPaint refPaint;
- refPaint.setAntiAlias(true);
- refPaint.setColor(0xFFbf3f7f);
- refPaint.setStyle(SkPaint::kStroke_Style);
- refPaint.setStrokeWidth(1);
- const SkScalar radius = 125;
- SkRect oval = SkRect::MakeLTRB(-radius - 20, -radius - 20, radius + 20, radius + 20);
- SkPath circle;
- circle.addCircle(0, 0, radius);
- SkScalar circumference = radius * SK_ScalarPI * 2;
- int wedges[] = { 6, 12, 36 };
- canvas->translate(radius + 20, radius + 20);
- for (int wedge : wedges) {
- SkScalar arcLength = 360.f / wedge;
- canvas->save();
- for (const DashExample& dashExample : dashExamples) {
- SkPath refPath;
- int dashUnits = 0;
- for (int index = 0; index < dashExample.length; ++index) {
- dashUnits += dashExample.pattern[index];
- }
- SkScalar unitLength = arcLength / dashUnits;
- SkScalar angle = 0;
- for (int index = 0; index < wedge; ++index) {
- for (int i2 = 0; i2 < dashExample.length; i2 += 2) {
- SkScalar span = dashExample.pattern[i2] * unitLength;
- refPath.moveTo(0, 0);
- refPath.arcTo(oval, angle, span, false);
- refPath.close();
- angle += span + (dashExample.pattern[i2 + 1]) * unitLength;
+
+class DashCircleGM : public skiagm::GM {
+public:
+ DashCircleGM() : fRotation(0) { }
+
+protected:
+ SkString onShortName() override { return SkString("dashcircle"); }
+
+ SkISize onISize() override { return SkISize::Make(900, 1200); }
+
+ void onDraw(SkCanvas* canvas) override {
+ SkPaint refPaint;
+ refPaint.setAntiAlias(true);
+ refPaint.setColor(0xFFbf3f7f);
+ refPaint.setStyle(SkPaint::kStroke_Style);
+ refPaint.setStrokeWidth(1);
+ const SkScalar radius = 125;
+ SkRect oval = SkRect::MakeLTRB(-radius - 20, -radius - 20, radius + 20, radius + 20);
+ SkPath circle;
+ circle.addCircle(0, 0, radius);
+ SkScalar circumference = radius * SK_ScalarPI * 2;
+ int wedges[] = { 6, 12, 36 };
+ canvas->translate(radius+20, radius+20);
+ for (int wedge : wedges) {
+ SkScalar arcLength = 360.f / wedge;
+ canvas->save();
+ for (const DashExample& dashExample : dashExamples) {
+ SkPath refPath;
+ int dashUnits = 0;
+ for (int index = 0; index < dashExample.length; ++index) {
+ dashUnits += dashExample.pattern[index];
}
+ SkScalar unitLength = arcLength / dashUnits;
+ SkScalar angle = 0;
+ for (int index = 0; index < wedge; ++index) {
+ for (int i2 = 0; i2 < dashExample.length; i2 += 2) {
+ SkScalar span = dashExample.pattern[i2] * unitLength;
+ refPath.moveTo(0, 0);
+ refPath.arcTo(oval, angle, span, false);
+ refPath.close();
+ angle += span + (dashExample.pattern[i2 + 1]) * unitLength;
+ }
+ }
+ canvas->save();
+ canvas->rotate(fRotation);
+ canvas->drawPath(refPath, refPaint);
+ canvas->restore();
+ SkPaint p;
+ p.setAntiAlias(true);
+ p.setStyle(SkPaint::kStroke_Style);
+ p.setStrokeWidth(10);
+ SkScalar intervals[4];
+ int intervalCount = dashExample.length;
+ SkScalar dashLength = circumference / wedge / dashUnits;
+ for (int index = 0; index < dashExample.length; ++index) {
+ intervals[index] = dashExample.pattern[index] * dashLength;
+ }
+ p.setPathEffect(SkDashPathEffect::Make(intervals, intervalCount, 0));
+ canvas->save();
+ canvas->rotate(fRotation);
+ canvas->drawPath(circle, p);
+ canvas->restore();
+ canvas->translate(0, radius * 2 + 50);
}
- canvas->drawPath(refPath, refPaint);
- SkPaint p;
- p.setAntiAlias(true);
- p.setStyle(SkPaint::kStroke_Style);
- p.setStrokeWidth(10);
- SkScalar intervals[4];
- int intervalCount = dashExample.length;
- SkScalar dashLength = circumference / wedge / dashUnits;
- for (int index = 0; index < dashExample.length; ++index) {
- intervals[index] = dashExample.pattern[index] * dashLength;
- }
- p.setPathEffect(SkDashPathEffect::Make(intervals, intervalCount, 0));
- canvas->drawPath(circle, p);
- canvas->translate(0, radius * 2 + 50);
+ canvas->restore();
+ canvas->translate(radius * 2 + 50, 0);
}
- canvas->restore();
- canvas->translate(radius * 2 + 50, 0);
}
-}
+
+ bool onAnimate(const SkAnimTimer& timer) override {
+ static const SkScalar kDesiredDurationSecs = 100.0f;
+
+ fRotation = timer.scaled(360.0f/kDesiredDurationSecs, 360.0f);
+ return true;
+ }
+
+private:
+ SkScalar fRotation;
+
+ typedef GM INHERITED;
+};
+
+DEF_GM(return new DashCircleGM; )
diff --git a/gm/lighting.cpp b/gm/lighting.cpp
index e0a317d328..08d09c2ecb 100644
--- a/gm/lighting.cpp
+++ b/gm/lighting.cpp
@@ -6,6 +6,7 @@
*/
#include "gm.h"
+#include "SkAnimTimer.h"
#include "SkLightingImageFilter.h"
#include "SkOffsetImageFilter.h"
#include "SkPoint3.h"
@@ -17,7 +18,8 @@ namespace skiagm {
class ImageLightingGM : public GM {
public:
- ImageLightingGM() {
+ ImageLightingGM()
+ : fAzimuth(SkIntToScalar(kStartAzimuth)) {
this->setBGColor(0xFF000000);
}
@@ -57,19 +59,25 @@ protected:
canvas->restore();
}
}
- SkPoint3 pointLocation = SkPoint3::Make(0, 0, SkIntToScalar(10));
- SkScalar azimuthRad = SkDegreesToRadians(SkIntToScalar(225));
+ SkScalar cosAzimuth;
+ SkScalar sinAzimuth = SkScalarSinCos(SkDegreesToRadians(fAzimuth), &cosAzimuth);
+
+ SkPoint3 spotTarget = SkPoint3::Make(SkIntToScalar(40), SkIntToScalar(40), 0);
+ SkPoint3 spotLocation = SkPoint3::Make(spotTarget.fX + 70.7214f * cosAzimuth,
+ spotTarget.fY + 70.7214f * sinAzimuth,
+ spotTarget.fZ + SkIntToScalar(20));
+ SkScalar spotExponent = SK_Scalar1;
+
+ SkPoint3 pointLocation = SkPoint3::Make(spotTarget.fX + 50 * cosAzimuth,
+ spotTarget.fY + 50 * sinAzimuth,
+ SkIntToScalar(10));
SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5));
- SkPoint3 distantDirection = SkPoint3::Make(SkScalarMul(SkScalarCos(azimuthRad),
+
+ SkPoint3 distantDirection = SkPoint3::Make(SkScalarMul(cosAzimuth,
SkScalarCos(elevationRad)),
- SkScalarMul(SkScalarSin(azimuthRad),
+ SkScalarMul(sinAzimuth,
SkScalarCos(elevationRad)),
SkScalarSin(elevationRad));
- SkPoint3 spotLocation = SkPoint3::Make(SkIntToScalar(-10),
- SkIntToScalar(-10),
- SkIntToScalar(20));
- SkPoint3 spotTarget = SkPoint3::Make(SkIntToScalar(40), SkIntToScalar(40), 0);
- SkScalar spotExponent = SK_Scalar1;
SkScalar cutoffAngle = SkIntToScalar(15);
SkScalar kd = SkIntToScalar(2);
SkScalar ks = SkIntToScalar(1);
@@ -149,8 +157,18 @@ protected:
}
}
+ bool onAnimate(const SkAnimTimer& timer) override {
+ static const SkScalar kDesiredDurationSecs = 15.0f;
+
+ fAzimuth = kStartAzimuth + timer.scaled(360.0f/kDesiredDurationSecs, 360.0f);
+ return true;
+ }
+
private:
+ static const int kStartAzimuth = 225;
+
SkBitmap fBitmap;
+ SkScalar fAzimuth;
typedef GM INHERITED;
};
diff --git a/gm/stringart.cpp b/gm/stringart.cpp
index d381350e18..7eaac85ebc 100644
--- a/gm/stringart.cpp
+++ b/gm/stringart.cpp
@@ -6,6 +6,7 @@
*/
#include "gm.h"
+#include "SkAnimTimer.h"
#include "SkCanvas.h"
#include "SkPath.h"
@@ -14,13 +15,14 @@
static const int kWidth = 640;
static const int kHeight = 480;
static const SkScalar kAngle = 0.305f;
+static const int kMaxNumSteps = 140;
// Renders a string art shape.
// The particular shape rendered can be controlled by adjusting kAngle, from 0 to 1
class StringArtGM : public skiagm::GM {
public:
- StringArtGM() {}
+ StringArtGM() : fNumSteps(kMaxNumSteps) {}
protected:
@@ -42,15 +44,13 @@ protected:
SkPath path;
path.moveTo(center);
- while (length < (SkScalarHalf(size) - 10.f))
- {
+ for (int i = 0; i < fNumSteps && length < (SkScalarHalf(size) - 10.f); ++i) {
SkPoint rp = SkPoint::Make(length*SkScalarCos(step) + center.fX,
length*SkScalarSin(step) + center.fY);
path.lineTo(rp);
length += angle / SkScalarHalf(SK_ScalarPI);
step += angle;
}
- path.close();
SkPaint paint;
paint.setAntiAlias(true);
@@ -60,7 +60,24 @@ protected:
canvas->drawPath(path, paint);
}
+ bool onAnimate(const SkAnimTimer& timer) override {
+ static const SkScalar kDesiredDurationSecs = 3.0f;
+
+ // Make the animation ping-pong back and forth but start in the fully drawn state
+ SkScalar fraction = 1.0f - timer.scaled(2.0f/kDesiredDurationSecs, 2.0f);
+ if (fraction <= 0.0f) {
+ fraction = -fraction;
+ }
+
+ SkASSERT(fraction >= 0.0f && fraction <= 1.0f);
+
+ fNumSteps = (int) (fraction * kMaxNumSteps);
+ return true;
+ }
+
private:
+ int fNumSteps;
+
typedef GM INHERITED;
};