Jim Van Verth | 7027691 | 2018-06-01 13:46:46 -0400 | [diff] [blame] | 1 | /* |
| 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 Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 7 | #include "Sample.h" |
Jim Van Verth | 7027691 | 2018-06-01 13:46:46 -0400 | [diff] [blame] | 8 | #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 | |
Ben Wagner | 14884cd | 2018-08-15 18:43:02 -0400 | [diff] [blame^] | 17 | #include <cmath> |
| 18 | |
Jim Van Verth | 7027691 | 2018-06-01 13:46:46 -0400 | [diff] [blame] | 19 | // Implementation in C++ of Animated Emoji |
| 20 | // See https://t.d3fc.io/status/705212795936247808 |
Ben Wagner | 14884cd | 2018-08-15 18:43:02 -0400 | [diff] [blame^] | 21 | // See https://crbug.com/848616 |
Jim Van Verth | 7027691 | 2018-06-01 13:46:46 -0400 | [diff] [blame] | 22 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 23 | class GlyphTransformView : public Sample { |
Jim Van Verth | 7027691 | 2018-06-01 13:46:46 -0400 | [diff] [blame] | 24 | public: |
| 25 | GlyphTransformView() {} |
| 26 | |
| 27 | protected: |
| 28 | void onOnceBeforeDraw() override { |
| 29 | fEmojiFont.fTypeface = sk_tool_utils::emoji_typeface(); |
| 30 | fEmojiFont.fText = sk_tool_utils::emoji_sample_text(); |
| 31 | } |
| 32 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 33 | bool onQuery(Sample::Event* evt) override { |
| 34 | if (Sample::TitleQ(*evt)) { |
| 35 | Sample::TitleR(evt, "Glyph Transform"); |
Jim Van Verth | 7027691 | 2018-06-01 13:46:46 -0400 | [diff] [blame] | 36 | return true; |
| 37 | } |
| 38 | return this->INHERITED::onQuery(evt); |
| 39 | } |
| 40 | |
| 41 | void onDrawContent(SkCanvas* canvas) override { |
| 42 | SkPaint paint; |
| 43 | paint.setTypeface(fEmojiFont.fTypeface); |
| 44 | const char* text = fEmojiFont.fText; |
| 45 | |
Ben Wagner | 14884cd | 2018-08-15 18:43:02 -0400 | [diff] [blame^] | 46 | double baseline = this->height() / 2; |
| 47 | canvas->drawLine(0, baseline, this->width(), baseline, paint); |
Jim Van Verth | 7027691 | 2018-06-01 13:46:46 -0400 | [diff] [blame] | 48 | |
Jim Van Verth | 7027691 | 2018-06-01 13:46:46 -0400 | [diff] [blame] | 49 | SkMatrix ctm; |
Ben Wagner | 14884cd | 2018-08-15 18:43:02 -0400 | [diff] [blame^] | 50 | ctm.setRotate(fRotate); // d3 rotate takes degrees |
| 51 | ctm.postScale(fScale * 4, fScale * 4); |
| 52 | ctm.postTranslate(fTranslate.fX + this->width() * 0.8, fTranslate.fY + baseline); |
Jim Van Verth | 7027691 | 2018-06-01 13:46:46 -0400 | [diff] [blame] | 53 | canvas->concat(ctm); |
Ben Wagner | 14884cd | 2018-08-15 18:43:02 -0400 | [diff] [blame^] | 54 | |
| 55 | // d3 by default anchors text around the middle |
| 56 | SkRect bounds; |
| 57 | paint.measureText(text, strlen(text), &bounds); |
| 58 | canvas->drawString(text, -bounds.centerX(), -bounds.centerY(), paint); |
Jim Van Verth | 7027691 | 2018-06-01 13:46:46 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | bool onAnimate(const SkAnimTimer& timer) override { |
Ben Wagner | 14884cd | 2018-08-15 18:43:02 -0400 | [diff] [blame^] | 62 | constexpr SkScalar maxt = 100000; |
| 63 | double t = timer.pingPong(20, 0, 0, maxt); // d3 t is in milliseconds |
Jim Van Verth | 7027691 | 2018-06-01 13:46:46 -0400 | [diff] [blame] | 64 | |
Ben Wagner | 14884cd | 2018-08-15 18:43:02 -0400 | [diff] [blame^] | 65 | fTranslate.set(sin(t / 3000) - t * this->width() * 0.7 / maxt, sin(t / 999) / t); |
| 66 | fScale = 4.5 - std::sqrt(t) / 99; |
Jim Van Verth | 7027691 | 2018-06-01 13:46:46 -0400 | [diff] [blame] | 67 | fRotate = sin(t / 734); |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | struct EmojiFont { |
| 74 | sk_sp<SkTypeface> fTypeface; |
| 75 | const char* fText; |
| 76 | } fEmojiFont; |
| 77 | |
| 78 | SkVector fTranslate; |
| 79 | SkScalar fScale; |
| 80 | SkScalar fRotate; |
| 81 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 82 | typedef Sample INHERITED; |
Jim Van Verth | 7027691 | 2018-06-01 13:46:46 -0400 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | ////////////////////////////////////////////////////////////////////////////// |
| 86 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 87 | DEF_SAMPLE( return new GlyphTransformView(); ) |