You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.4 KiB
84 lines
2.4 KiB
4 months ago
|
/*
|
||
|
* Copyright 2018 Google Inc.
|
||
|
*
|
||
|
* Use of this source code is governed by a BSD-style license that can be
|
||
|
* found in the LICENSE file.
|
||
|
*/
|
||
|
#include "samplecode/Sample.h"
|
||
|
#include "tools/ToolUtils.h"
|
||
|
|
||
|
#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/TimeUtils.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:
|
||
|
GlyphTransformView() {}
|
||
|
|
||
|
protected:
|
||
|
void onOnceBeforeDraw() override {
|
||
|
fEmojiFont.fTypeface = ToolUtils::emoji_typeface();
|
||
|
fEmojiFont.fText = ToolUtils::emoji_sample_text();
|
||
|
}
|
||
|
|
||
|
SkString name() override { return SkString("Glyph Transform"); }
|
||
|
|
||
|
void onDrawContent(SkCanvas* canvas) override {
|
||
|
SkPaint paint;
|
||
|
|
||
|
SkFont font(fEmojiFont.fTypeface);
|
||
|
const char* text = fEmojiFont.fText;
|
||
|
|
||
|
double baseline = this->height() / 2;
|
||
|
canvas->drawLine(0, baseline, this->width(), baseline, paint);
|
||
|
|
||
|
SkMatrix ctm;
|
||
|
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);
|
||
|
|
||
|
// d3 by default anchors text around the middle
|
||
|
SkRect bounds;
|
||
|
font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
|
||
|
canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, -bounds.centerX(), -bounds.centerY(),
|
||
|
font, paint);
|
||
|
}
|
||
|
|
||
|
bool onAnimate(double nanos) override {
|
||
|
constexpr SkScalar maxt = 100000;
|
||
|
double t = TimeUtils::PingPong(1e-9 * nanos, 20, 0, 0, maxt); // d3 t is in milliseconds
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
struct EmojiFont {
|
||
|
sk_sp<SkTypeface> fTypeface;
|
||
|
const char* fText;
|
||
|
} fEmojiFont;
|
||
|
|
||
|
SkVector fTranslate;
|
||
|
SkScalar fScale;
|
||
|
SkScalar fRotate;
|
||
|
|
||
|
using INHERITED = Sample;
|
||
|
};
|
||
|
|
||
|
//////////////////////////////////////////////////////////////////////////////
|
||
|
|
||
|
DEF_SAMPLE( return new GlyphTransformView(); )
|