-
-
Notifications
You must be signed in to change notification settings - Fork 574
/
Polygon.ts
238 lines (220 loc) · 5.86 KB
/
Polygon.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import {
BBox,
SerializedVector2,
SignalValue,
SimpleSignal,
Vector2,
} from '@motion-canvas/core';
import {CurveProfile, getPolylineProfile} from '../curves';
import {computed, initial, signal} from '../decorators';
import {DesiredLength} from '../partials';
import {drawPolygon} from '../utils';
import {Curve, CurveProps} from './Curve';
export interface PolygonProps extends CurveProps {
/**
* {@inheritDoc Polygon.sides}
*/
sides?: SignalValue<number>;
/**
* {@inheritDoc Polygon.radius}
*/
radius?: SignalValue<number>;
}
/**
* A node for drawing regular polygons.
*
* @remarks
* This node can be used to render shapes such as: triangle, pentagon,
* hexagon and more.
*
* Note that the polygon is inscribed in a circle defined by the height
* and width. If height and width are unequal, the polygon is inscribed
* in the resulting ellipse.
*
* Since the polygon is inscribed in the circle, the actual displayed
* height and width may differ somewhat from the bounding rectangle. This
* will be particularly noticeable if the number of sides is low, e.g. for a
* triangle.
*
* @preview
* ```tsx editor
* // snippet Polygon
* import {makeScene2D, Polygon} from '@motion-canvas/2d';
* import {createRef} from '@motion-canvas/core';
*
* export default makeScene2D(function* (view) {
* const ref = createRef<Polygon>();
* view.add(
* <Polygon
* ref={ref}
* sides={6}
* size={160}
* fill={'lightseagreen'}
* />
* );
*
* yield* ref().sides(3, 2).to(6, 2);
* });
*
* // snippet Pentagon outline
* import {makeScene2D, Polygon} from '@motion-canvas/2d';
*
* export default makeScene2D(function* (view) {
* view.add(
* <Polygon
* sides={5}
* size={160}
* radius={30}
* stroke={'lightblue'}
* lineWidth={8}
* />
* );
* });
*
* // snippet Accessing vertex data
* import {Circle, Polygon, makeScene2D} from '@motion-canvas/2d';
* import {createRef, range} from '@motion-canvas/core';
*
* export default makeScene2D(function* (view) {
* const polygon = createRef<Polygon>();
* view.add(
* <Polygon ref={polygon} sides={3} lineWidth={4} stroke={'white'} size={160}>
* {range(6).map(index => (
* <Circle
* fill={'white'}
* size={20}
* position={() => polygon().vertex(index)}
* opacity={() => polygon().vertexCompletion(index)}
* />
* ))}
* </Polygon>,
* );
*
* yield* polygon().sides(6, 2).wait(0.5).back(2);
* });
* ```
*/
export class Polygon extends Curve {
/**
* The number of sides of the polygon.
*
* @remarks
* For example, a value of 6 creates a hexagon.
*
* @example
* ```tsx
* <Polygon
* size={320}
* sides={7}
* stroke={'#fff'}
* lineWidth={8}
* fill={'lightseagreen'}
* />
* ```
*/
@initial(6)
@signal()
public declare readonly sides: SimpleSignal<number, this>;
/**
* The radius of the polygon's corners.
*
* @example
* ```tsx
* <Polygon
* radius={30}
* size={320}
* sides={3}
* stroke={'#fff'}
* lineWidth={8}
* />
* ```
*/
@initial(0)
@signal()
public declare readonly radius: SimpleSignal<number, this>;
public constructor(props: PolygonProps) {
super(props);
}
/**
* Get the position of the nth vertex in the local space of this polygon.
*
* @param index - The index of the vertex.
*/
public vertex(index: number): Vector2 {
const size = this.computedSize().scale(0.5);
const theta = (index * 2 * Math.PI) / this.sides();
const direction = Vector2.fromRadians(theta).perpendicular;
return direction.mul(size);
}
/**
* Get the completion of the nth vertex.
*
* @remarks
* The completion is a value between `0` and `1` that describes how the given
* vertex partakes in the polygon.
*
* For integer values of {@link sides}, the completion is simply `1` for
* each index making up the polygon and `0` for any other index. If `sides`
* includes a fraction, the last index of the polygon will have a completion
* equal to said fraction.
*
* Check out the {@link Polygon | Accessing vertex data} example for a
* demonstration.
*
* @param index - The index of the vertex.
*/
public vertexCompletion(index: number): number {
const sides = this.sides();
if (index < 0 || index > sides) {
return 0;
}
if (index < sides - 1) {
return 1;
}
return sides - index;
}
@computed()
public override profile(): CurveProfile {
const sides = this.sides();
const radius = this.radius();
const points = [];
const size = this.computedSize().scale(0.5);
for (let i = 0; i < sides; i++) {
const theta = (i * 2 * Math.PI) / sides;
const direction = Vector2.fromRadians(theta).perpendicular;
points.push(direction.mul(size));
}
return getPolylineProfile(points, radius, true);
}
protected override desiredSize(): SerializedVector2<DesiredLength> {
return {
x: this.width.context.getter(),
y: this.height.context.getter(),
};
}
protected override offsetComputedLayout(box: BBox): BBox {
return box;
}
protected override childrenBBox(): BBox {
return BBox.fromSizeCentered(this.computedSize());
}
protected override requiresProfile(): boolean {
return super.requiresProfile() || this.radius() > 0;
}
protected override getPath(): Path2D {
if (this.requiresProfile()) {
return this.curveDrawingInfo().path;
}
return this.createPath();
}
protected override getRipplePath(): Path2D {
return this.createPath(this.rippleSize());
}
protected createPath(expand = 0) {
const path = new Path2D();
const sides = this.sides();
const box = BBox.fromSizeCentered(this.size()).expand(expand);
drawPolygon(path, box, sides);
return path;
}
}