2018-06-01 17:46:46 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2018 Google Inc.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
|
* found in the LICENSE file.
|
|
|
|
|
*/
|
2019-04-23 17:05:21 +00:00
|
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
|
#include "tools/ToolUtils.h"
|
2018-06-01 17:46:46 +00:00
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
|
#include "include/core/SkRRect.h"
|
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
|
#include "tools/timer/AnimTimer.h"
|
2018-06-01 17:46:46 +00:00
|
|
|
|
|
2018-08-15 22:43:02 +00:00
|
|
|
|
#include <cmath>
|
|
|
|
|
|
2018-06-01 17:46:46 +00:00
|
|
|
|
// Implementation in C++ of Animated Emoji
|
|
|
|
|
// See https://t.d3fc.io/status/705212795936247808
|
2018-08-15 22:43:02 +00:00
|
|
|
|
// See https://crbug.com/848616
|
2018-06-01 17:46:46 +00:00
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
|
class GlyphTransformView : public Sample {
|
2018-06-01 17:46:46 +00:00
|
|
|
|
public:
|
|
|
|
|
GlyphTransformView() {}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void onOnceBeforeDraw() override {
|
2019-03-20 16:12:10 +00:00
|
|
|
|
fEmojiFont.fTypeface = ToolUtils::emoji_typeface();
|
|
|
|
|
fEmojiFont.fText = ToolUtils::emoji_sample_text();
|
2018-06-01 17:46:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 14:55:44 +00:00
|
|
|
|
SkString name() override { return SkString("Glyph Transform"); }
|
2018-06-01 17:46:46 +00:00
|
|
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
|
|
|
|
SkPaint paint;
|
2019-01-02 17:21:01 +00:00
|
|
|
|
|
|
|
|
|
SkFont font(fEmojiFont.fTypeface);
|
2018-06-01 17:46:46 +00:00
|
|
|
|
const char* text = fEmojiFont.fText;
|
|
|
|
|
|
2018-08-15 22:43:02 +00:00
|
|
|
|
double baseline = this->height() / 2;
|
|
|
|
|
canvas->drawLine(0, baseline, this->width(), baseline, paint);
|
2018-06-01 17:46:46 +00:00
|
|
|
|
|
|
|
|
|
SkMatrix ctm;
|
2018-08-15 22:43:02 +00:00
|
|
|
|
ctm.setRotate(fRotate); // d3 rotate takes degrees
|
|
|
|
|
ctm.postScale(fScale * 4, fScale * 4);
|
|
|
|
|
ctm.postTranslate(fTranslate.fX + this->width() * 0.8, fTranslate.fY + baseline);
|
2018-06-01 17:46:46 +00:00
|
|
|
|
canvas->concat(ctm);
|
2018-08-15 22:43:02 +00:00
|
|
|
|
|
|
|
|
|
// d3 by default anchors text around the middle
|
|
|
|
|
SkRect bounds;
|
2019-05-07 19:38:46 +00:00
|
|
|
|
font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
|
|
|
|
|
canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, -bounds.centerX(), -bounds.centerY(),
|
2019-01-02 17:21:01 +00:00
|
|
|
|
font, paint);
|
2018-06-01 17:46:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-20 16:55:08 +00:00
|
|
|
|
bool onAnimate(const AnimTimer& timer) override {
|
2018-08-15 22:43:02 +00:00
|
|
|
|
constexpr SkScalar maxt = 100000;
|
|
|
|
|
double t = timer.pingPong(20, 0, 0, maxt); // d3 t is in milliseconds
|
2018-06-01 17:46:46 +00:00
|
|
|
|
|
2018-08-15 22:43:02 +00:00
|
|
|
|
fTranslate.set(sin(t / 3000) - t * this->width() * 0.7 / maxt, sin(t / 999) / t);
|
|
|
|
|
fScale = 4.5 - std::sqrt(t) / 99;
|
2018-06-01 17:46:46 +00:00
|
|
|
|
fRotate = sin(t / 734);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
struct EmojiFont {
|
|
|
|
|
sk_sp<SkTypeface> fTypeface;
|
|
|
|
|
const char* fText;
|
|
|
|
|
} fEmojiFont;
|
|
|
|
|
|
|
|
|
|
SkVector fTranslate;
|
|
|
|
|
SkScalar fScale;
|
|
|
|
|
SkScalar fRotate;
|
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
|
typedef Sample INHERITED;
|
2018-06-01 17:46:46 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
|
DEF_SAMPLE( return new GlyphTransformView(); )
|