Flutter Engine
The Flutter Engine
SkPath_quadTo_example_parametric_animated.cpp
Go to the documentation of this file.
1// Copyright 2020 Google LLC.
2// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4REG_FIDDLE_ANIMATED(SkPath_quadTo_example_parametric_animated, 512, 512, false, 0, 4) {
5// SkPath_quadTo_example_parametric_animated
6
7SkPoint quad(SkPoint p0, SkPoint p1, SkPoint p2, float t) {
8 float s = 1 - t;
9 return {(s * s * p0.x()) + (2 * s * t * p1.x()) + (t * t * p2.x()),
10 (s * s * p0.y()) + (2 * s * t * p1.y()) + (t * t * p2.y())};
11}
12
13/*
14
15 If the starting point is (x0, y0), then this curve is defined as the
16 paramentric curve as `t` goes from 0 to 1:
17
18 s := 1 - t
19 x := (s * s * x0) + (2 * s * t * x1) + (t * t * x2)
20 y := (s * s * y0) + (2 * s * t * y1) + (t * t * y2)
21
22*/
23
24static SkPoint interpolate(SkPoint a, SkPoint b, float t) {
25 return {SkScalarInterp(a.x(), b.x(), t),
26 SkScalarInterp(a.y(), b.y(), t)};
27}
28
29static void draw_line(SkCanvas* canvas, SkPoint p0, SkPoint p1, const SkPaint& paint) {
30 canvas->drawLine(p0.x(), p0.y(), p1.x(), p1.y(), paint);
31}
32
33static void draw_point(SkCanvas* canvas, SkPoint p, const SkPaint& paint) {
34 canvas->drawPoint(p.x(), p.y(), paint);
35}
36
37void draw(SkCanvas* canvas) {
38 canvas->clear(SkColorSetARGB(255,255,255,255));
39
41 paint.setAntiAlias(true);
43 paint.setStrokeWidth(5);
44
45 SkPoint a{10, 100};
46 SkPoint b{200, 400};
47 SkPoint c{390, 100};
48
50 SkPoint bc = interpolate(b, c, frame);
51
52 draw_line(canvas, a, b, paint);
53 draw_line(canvas, b, c, paint);
54
55 paint.setStrokeWidth(1);
56 draw_line(canvas, ab, bc, paint);
57 paint.setStrokeWidth(5);
58
59 paint.setColor(SkColorSetARGB(255,0,0,255));
60 SkPath quadraticCurve;
61 quadraticCurve.moveTo(a);
62 quadraticCurve.quadTo(b, c);
63 canvas->drawPath(quadraticCurve, paint);
64
65 SkPaint textPaint;
66 SkFont font(fontMgr->matchFamilyStyle(nullptr, {}), 32);
67 textPaint.setAntiAlias(true);
68 canvas->drawString("a", a.x(), a.y(), font, textPaint);
69 canvas->drawString("b", b.x()+20, b.y()+20, font, textPaint);
70 canvas->drawString("c", c.x(), c.y(), font, textPaint);
71
72 SkPaint pointPaint;
73 pointPaint.setAntiAlias(true);
74 pointPaint.setStrokeWidth(8);
76 pointPaint.setColor(SkColorSetARGB(255, 0, 255, 0));
77 draw_point(canvas, quad(a, b, c, frame), pointPaint);
78}
79} // END FIDDLE
static double interpolate(double A, double B, double t)
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColor.h:49
REG_FIDDLE_ANIMATED(SkPath_quadTo_example_parametric_animated, 512, 512, false, 0, 4)
static SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t)
Definition: SkScalar.h:131
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition: aaclip.cpp:27
void drawPoint(SkScalar x, SkScalar y, const SkPaint &paint)
Definition: SkCanvas.cpp:2695
void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint &paint)
Definition: SkCanvas.cpp:2700
void clear(SkColor color)
Definition: SkCanvas.h:1199
void drawPath(const SkPath &path, const SkPaint &paint)
Definition: SkCanvas.cpp:1747
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.h:1803
sk_sp< SkTypeface > matchFamilyStyle(const char familyName[], const SkFontStyle &) const
Definition: SkFontMgr.cpp:109
Definition: SkFont.h:35
@ kRound_Cap
adds circle
Definition: SkPaint.h:335
void setColor(SkColor color)
Definition: SkPaint.cpp:119
void setAntiAlias(bool aa)
Definition: SkPaint.h:170
void setStrokeCap(Cap cap)
Definition: SkPaint.cpp:179
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
void setStrokeWidth(SkScalar width)
Definition: SkPaint.cpp:159
Definition: SkPath.h:59
SkPath & moveTo(SkScalar x, SkScalar y)
Definition: SkPath.cpp:688
SkPath & quadTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2)
Definition: SkPath.cpp:746
const Paint & paint
Definition: color_source.cc:38
sk_sp< SkFontMgr > fontMgr
Definition: examples.cpp:32
double frame
Definition: examples.cpp:31
static bool b
struct MyStruct s
struct MyStruct a[10]
static void draw_line(SkCanvas *canvas, SkImage *, const SkRect &r, sk_sp< SkImageFilter > imf)
Definition: ab.py:1
font
Font Metadata and Metrics.
constexpr float y() const
Definition: SkPoint_impl.h:187
constexpr float x() const
Definition: SkPoint_impl.h:181