blob: 30ea43442d480e7e73c038e1ab62ac2bb763e0a4 [file] [log] [blame]
Jim Van Verth70276912018-06-01 13:46:46 -04001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04007#include "Sample.h"
Jim Van Verth70276912018-06-01 13:46:46 -04008#include "sk_tool_utils.h"
9
10#include "SkAnimTimer.h"
11#include "SkCanvas.h"
12#include "SkPath.h"
13#include "SkRandom.h"
14#include "SkRRect.h"
15#include "SkTypeface.h"
16
17// Implementation in C++ of Animated Emoji
18// See https://t.d3fc.io/status/705212795936247808
19
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040020class GlyphTransformView : public Sample {
Jim Van Verth70276912018-06-01 13:46:46 -040021public:
22 GlyphTransformView() {}
23
24protected:
25 void onOnceBeforeDraw() override {
26 fEmojiFont.fTypeface = sk_tool_utils::emoji_typeface();
27 fEmojiFont.fText = sk_tool_utils::emoji_sample_text();
28 }
29
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040030 bool onQuery(Sample::Event* evt) override {
31 if (Sample::TitleQ(*evt)) {
32 Sample::TitleR(evt, "Glyph Transform");
Jim Van Verth70276912018-06-01 13:46:46 -040033 return true;
34 }
35 return this->INHERITED::onQuery(evt);
36 }
37
38 void onDrawContent(SkCanvas* canvas) override {
39 SkPaint paint;
40 paint.setTypeface(fEmojiFont.fTypeface);
41 const char* text = fEmojiFont.fText;
42
43 canvas->scale(4, 4);
44
45 canvas->drawLine(0, 200, 600, 200, paint);
46 SkMatrix ctm;
47 ctm.setRotate(SkRadiansToDegrees(fRotate));
48 ctm.postScale(fScale, fScale);
49 ctm.postTranslate(fTranslate.fX, fTranslate.fY);
50 canvas->concat(ctm);
51 canvas->drawString(text, 0, 0, paint);
52 }
53
54 bool onAnimate(const SkAnimTimer& timer) override {
55 double t = timer.secs();
56
57 fTranslate.set(99 + sin(t / 3.0e3) - t / 1024, 200 + sin(t / 999) / t);
58 fScale = 4.5 - t*t / 99;
59 fRotate = sin(t / 734);
60
61 return true;
62 }
63
64private:
65 struct EmojiFont {
66 sk_sp<SkTypeface> fTypeface;
67 const char* fText;
68 } fEmojiFont;
69
70 SkVector fTranslate;
71 SkScalar fScale;
72 SkScalar fRotate;
73
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040074 typedef Sample INHERITED;
Jim Van Verth70276912018-06-01 13:46:46 -040075};
76
77//////////////////////////////////////////////////////////////////////////////
78
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040079DEF_SAMPLE( return new GlyphTransformView(); )