-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapes.d.ts
211 lines (184 loc) · 5.62 KB
/
shapes.d.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
/**
* - A DOMString parsed as CSS <color> value.
* - A CanvasGradient object (a linear or radial gradient).
* - A CanvasPattern object (a repetitive image).
*/
export type ShapeStyle = string | CanvasGradient | CanvasPattern;
/** The supported baselines for text rendering. */
export type TextBaseline =
| 'top'
| 'bottom'
| 'middle'
| 'alphabetic'
| 'hanging';
/** Defines the fill of a shape */
export interface Filled {
/** The style to fill the shape with */
fill: ShapeStyle;
}
/** Defines that a shape is stroked. */
export interface Stroked {
/** The style of the stroke */
stroke: ShapeStyle;
/** The thickness of the stroke, in pixels */
thickness: number;
}
/** Defines that the shape has an opacity. */
export interface Translucent {
/** The opacity of the shape, 0.0 to 1.0, where 1.0 means 100% opaque. */
opacity: number;
}
/** Defines that the shape is visible. Filled, stroked or both */
export type Visible = (Filled | Stroked) | (Filled & Stroked) | Translucent;
/** Defines the offset of a shape. */
export interface Offset {
/** Number of pixels offset from the left */
x: number;
/** Number of pixels offset from the top */
y: number;
}
/** The specifications for how a rectangle should be rendered. */
export interface RectSpec extends Offset {
/** The width of the rectangle in pixels */
width: number;
/** The height of the rectangle in pixels */
height: number;
}
/** The specifications for how a rounded rectangle should be rendered. */
export interface RoundedRectSpec extends RectSpec {
/** The border radius */
radius: number;
}
/** The specifications for how a text string should be rendered. */
export interface TextSpec extends Offset {
/** The text to render */
value: string;
/**
* A DOMString parsed as CSS font value.
* The default font is 10px sans-serif.
*/
font: string;
/** The baseline of the text */
baseline: TextBaseline;
}
/** The specifications for how a polygon should be rendered. */
export interface PolySpec {
/** A nested array consisting of `[ x, y ]`-coordinates. */
points: [number, number][];
}
/** The specifications for how a SVG path should be rendered. */
export interface PathSpec {
/** The Path2D to draw to the context. */
path: Path2D;
}
/** The specifications for how a circle should be rendered. */
export interface CircleSpec extends Offset {
/** The radius of the circle. */
radius: number;
}
/** The specification for any shape. */
export type AnyShapeSpec =
| PathSpec
| PolySpec
| RectSpec
| RoundedRectSpec
| TextSpec
| CircleSpec;
/** Constitutes a set of named commands and specifications. */
export interface ShapeLayer {
[namedCommand: string]: AnyShapeSpec & Visible;
}
/** An easy-to-use shape drawing API for the HTML canvas. */
export interface ShapeApi {
/**
* Renders a SVG path using the provided specifications.
* @param spec The specification of the path to draw.
*/
path(spec: PathSpec & Visible): void;
/**
* Renders a polygon using the provided specifications.
* @param spec The specification of the polygon to render.
*/
poly(spec: PolySpec & Visible): void;
/**
* Renders a rectangle using the provided specifications.
* @param spec The specification of the rectangle to render.
*/
rect(spec: RectSpec & Visible): void;
/**
* Renders a rounded rectangle using the provided specifications.
* @param spec The specification of the rounded rectangle to render.
*/
roundedRect(spec: RoundedRectSpec & Visible): void;
/**
* Renders a circle using the provided specifications.
* @param spec The specification of the circle to render.
*/
circle(spec: CircleSpec & Visible): void;
/**
* Renders text using the provided specifications.
* @param spec The specification of the text to render.
*/
text(spec: TextSpec & Visible): void;
/**
* Measures the width of the text in whole pixels, rounding up.
* @param text The text to measure.
* @param font The font to use when measuring.
*/
textWidth(text: string, font: string): number;
/**
* Draws the specified layers to the context.
*
* **Usage:**
```
shape.layers([
{
'rect background': {
x: 0, y: 10,
width: 10px,
height: 20px
fill: '#222',
stroke: '#111',
thickness: 1
},
'text contents': {
x: 10, y: 15,
value: 'Hi!',
font: '10px Arial',
fill: '#fff'
}
}
]);
```
* @param layers A collection of layers to render.
*/
layers(layers: ShapeLayer[]): void;
/**
* Sets the drawing origin to the specified point.
* @param x The horizontal offset to set, in pixels.
* @param y The vertical offset to set, in pixels.
*/
setOrigin(x: number, y: number): void;
/**
* Offsets the drawing origin by the specified amount of pixels.
* @param x Number of pixels to offset from the left.
* @param y Number of pixels to offset from the top.
*/
offset(x: number, y: number): void;
/**
* Prepends an offset to the provided `pathString` as defined by `x` and `y`,
* and the global offset of the context, defined using `offset` and/or `setOrigin`.
*
* **Note:** In order for the path to be properly offset,
* **all commands in the path must be relative.**
* @param pathString An all-relative path string
* @param x Number of pixels offset from the left.
* @param y Number of pixels offset from the top.
*/
offsetPath(pathString: string, x?: number, y?: number): string;
}
/** Creates a new shape API. */
declare function shapes(
ctx: HTMLCanvasElement | CanvasRenderingContext2D
): ShapeApi;
export default shapes;