diff options
author | Ben Wagner <bungeman@google.com> | 2018-08-15 18:43:02 -0400 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2018-08-16 14:17:46 +0000 |
commit | 14884cd86c459888c9f216acb403dfac4b97e4c7 (patch) | |
tree | 4cb66313dfbdae5adedb6a4ef6aad7e678f1d07b /samplecode | |
parent | cbd83bbd77d1c8a56ed3547548dcf11ef8e60d2e (diff) | |
download | skqp-14884cd86c459888c9f216acb403dfac4b97e4c7.tar.gz |
Beautify GlyphTransform sample.
The glyph transform sample often looks empty when seen in Viewer since
it doesn't loop and the drawing is often out of range by the time the
sample is seen. Make the sample loop, correct 't' to be in milliseconds,
correct 'sq' to sqrt, move the origin to the draw, make the text draw
centered at the origin, and update the animation based on the size of
the sample.
Change-Id: Ia87817db6981ee73a50793b39696ef393ff26df8
Reviewed-on: https://skia-review.googlesource.com/147283
Reviewed-by: Jim Van Verth <jvanverth@google.com>
Commit-Queue: Ben Wagner <bungeman@google.com>
Diffstat (limited to 'samplecode')
-rw-r--r-- | samplecode/SampleGlyphTransform.cpp | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/samplecode/SampleGlyphTransform.cpp b/samplecode/SampleGlyphTransform.cpp index 30ea43442d..e8567d1ec9 100644 --- a/samplecode/SampleGlyphTransform.cpp +++ b/samplecode/SampleGlyphTransform.cpp @@ -14,8 +14,11 @@ #include "SkRRect.h" #include "SkTypeface.h" +#include <cmath> + // Implementation in C++ of Animated Emoji // See https://t.d3fc.io/status/705212795936247808 +// See https://crbug.com/848616 class GlyphTransformView : public Sample { public: @@ -40,22 +43,27 @@ protected: paint.setTypeface(fEmojiFont.fTypeface); const char* text = fEmojiFont.fText; - canvas->scale(4, 4); + double baseline = this->height() / 2; + canvas->drawLine(0, baseline, this->width(), baseline, paint); - canvas->drawLine(0, 200, 600, 200, paint); SkMatrix ctm; - ctm.setRotate(SkRadiansToDegrees(fRotate)); - ctm.postScale(fScale, fScale); - ctm.postTranslate(fTranslate.fX, fTranslate.fY); + ctm.setRotate(fRotate); // d3 rotate takes degrees + ctm.postScale(fScale * 4, fScale * 4); + ctm.postTranslate(fTranslate.fX + this->width() * 0.8, fTranslate.fY + baseline); canvas->concat(ctm); - canvas->drawString(text, 0, 0, paint); + + // d3 by default anchors text around the middle + SkRect bounds; + paint.measureText(text, strlen(text), &bounds); + canvas->drawString(text, -bounds.centerX(), -bounds.centerY(), paint); } bool onAnimate(const SkAnimTimer& timer) override { - double t = timer.secs(); + constexpr SkScalar maxt = 100000; + double t = timer.pingPong(20, 0, 0, maxt); // d3 t is in milliseconds - fTranslate.set(99 + sin(t / 3.0e3) - t / 1024, 200 + sin(t / 999) / t); - fScale = 4.5 - t*t / 99; + fTranslate.set(sin(t / 3000) - t * this->width() * 0.7 / maxt, sin(t / 999) / t); + fScale = 4.5 - std::sqrt(t) / 99; fRotate = sin(t / 734); return true; |