vexflow 5.0.0-alpha.2 → 5.0.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (157) hide show
  1. package/build/cjs/vexflow-bravura.js +2 -2
  2. package/build/cjs/vexflow-bravura.js.map +1 -1
  3. package/build/cjs/vexflow-core.js +2 -2
  4. package/build/cjs/vexflow-core.js.map +1 -1
  5. package/build/cjs/vexflow-debug-with-tests.js +528 -492
  6. package/build/cjs/vexflow-debug-with-tests.js.map +1 -1
  7. package/build/cjs/vexflow-debug.js +460 -452
  8. package/build/cjs/vexflow-debug.js.map +1 -1
  9. package/build/cjs/vexflow.js +2 -2
  10. package/build/cjs/vexflow.js.map +1 -1
  11. package/build/esm/src/accidental.js +22 -28
  12. package/build/esm/src/beam.js +24 -42
  13. package/build/esm/src/clefnote.js +6 -1
  14. package/build/esm/src/element.js +83 -101
  15. package/build/esm/src/fonts/gonville.js +1 -1
  16. package/build/esm/src/formatter.js +9 -17
  17. package/build/esm/src/fraction.js +15 -22
  18. package/build/esm/src/glyphnote.js +3 -2
  19. package/build/esm/src/metrics.js +3 -0
  20. package/build/esm/src/modifier.js +6 -21
  21. package/build/esm/src/multimeasurerest.js +16 -25
  22. package/build/esm/src/note.js +7 -0
  23. package/build/esm/src/registry.js +5 -19
  24. package/build/esm/src/stave.js +4 -6
  25. package/build/esm/src/stavenote.js +26 -40
  26. package/build/esm/src/stavetempo.js +4 -11
  27. package/build/esm/src/stavetie.js +82 -36
  28. package/build/esm/src/svgcontext.js +5 -12
  29. package/build/esm/src/tickable.js +13 -26
  30. package/build/esm/src/tickcontext.js +20 -2
  31. package/build/esm/src/version.js +3 -3
  32. package/build/esm/src/vibrato.js +6 -1
  33. package/build/esm/tests/clef_tests.js +5 -1
  34. package/build/esm/tests/glyphnote_tests.js +6 -0
  35. package/build/esm/tests/stavetie_tests.js +29 -0
  36. package/build/esm/tests/tabstave_tests.js +1 -0
  37. package/build/esm/tests/vexflow_test_helpers.js +25 -39
  38. package/build/types/src/accidental.d.ts +19 -1
  39. package/build/types/src/annotation.d.ts +1 -1
  40. package/build/types/src/beam.d.ts +12 -1
  41. package/build/types/src/clefnote.d.ts +2 -0
  42. package/build/types/src/element.d.ts +13 -3
  43. package/build/types/src/fonts/gonville.d.ts +1 -1
  44. package/build/types/src/formatter.d.ts +5 -3
  45. package/build/types/src/fraction.d.ts +12 -7
  46. package/build/types/src/modifier.d.ts +2 -1
  47. package/build/types/src/multimeasurerest.d.ts +4 -1
  48. package/build/types/src/note.d.ts +2 -0
  49. package/build/types/src/registry.d.ts +1 -1
  50. package/build/types/src/stavenote.d.ts +2 -1
  51. package/build/types/src/stavetempo.d.ts +3 -3
  52. package/build/types/src/stavetie.d.ts +39 -2
  53. package/build/types/src/svgcontext.d.ts +1 -1
  54. package/build/types/src/tickable.d.ts +2 -1
  55. package/build/types/src/tickcontext.d.ts +30 -0
  56. package/package.json +2 -4
  57. package/tests/flow.html +2 -2
  58. package/entry/vexflow-bravura.ts +0 -25
  59. package/entry/vexflow-core.ts +0 -16
  60. package/entry/vexflow-debug-with-tests.ts +0 -64
  61. package/entry/vexflow-debug.ts +0 -54
  62. package/entry/vexflow.ts +0 -47
  63. package/src/accidental.ts +0 -570
  64. package/src/annotation.ts +0 -294
  65. package/src/articulation.ts +0 -383
  66. package/src/barnote.ts +0 -95
  67. package/src/beam.ts +0 -999
  68. package/src/bend.ts +0 -268
  69. package/src/boundingbox.ts +0 -108
  70. package/src/canvascontext.ts +0 -348
  71. package/src/chordsymbol.ts +0 -473
  72. package/src/clef.ts +0 -164
  73. package/src/clefnote.ts +0 -50
  74. package/src/crescendo.ts +0 -132
  75. package/src/curve.ts +0 -195
  76. package/src/dot.ts +0 -171
  77. package/src/easyscore.ts +0 -538
  78. package/src/element.ts +0 -692
  79. package/src/factory.ts +0 -694
  80. package/src/font.ts +0 -343
  81. package/src/fonts/academico.ts +0 -2
  82. package/src/fonts/academicobold.ts +0 -2
  83. package/src/fonts/bravura.ts +0 -2
  84. package/src/fonts/gonville.ts +0 -2
  85. package/src/fonts/petaluma.ts +0 -2
  86. package/src/fonts/petalumascript.ts +0 -2
  87. package/src/formatter.ts +0 -1107
  88. package/src/fraction.ts +0 -245
  89. package/src/frethandfinger.ts +0 -180
  90. package/src/ghostnote.ts +0 -75
  91. package/src/glyphnote.ts +0 -68
  92. package/src/glyphs.ts +0 -5880
  93. package/src/gracenote.ts +0 -146
  94. package/src/gracenotegroup.ts +0 -194
  95. package/src/gracetabnote.ts +0 -30
  96. package/src/index.ts +0 -90
  97. package/src/keymanager.ts +0 -153
  98. package/src/keysignature.ts +0 -272
  99. package/src/keysignote.ts +0 -46
  100. package/src/metrics.ts +0 -222
  101. package/src/modifier.ts +0 -216
  102. package/src/modifiercontext.ts +0 -177
  103. package/src/multimeasurerest.ts +0 -229
  104. package/src/music.ts +0 -443
  105. package/src/note.ts +0 -690
  106. package/src/notehead.ts +0 -158
  107. package/src/notesubgroup.ts +0 -88
  108. package/src/ornament.ts +0 -301
  109. package/src/parenthesis.ts +0 -89
  110. package/src/parser.ts +0 -263
  111. package/src/pedalmarking.ts +0 -273
  112. package/src/registry.ts +0 -146
  113. package/src/rendercontext.ts +0 -98
  114. package/src/renderer.ts +0 -166
  115. package/src/repeatnote.ts +0 -25
  116. package/src/stave.ts +0 -885
  117. package/src/stavebarline.ts +0 -231
  118. package/src/staveconnector.ts +0 -281
  119. package/src/stavehairpin.ts +0 -209
  120. package/src/staveline.ts +0 -343
  121. package/src/stavemodifier.ts +0 -93
  122. package/src/stavenote.ts +0 -1230
  123. package/src/staverepetition.ts +0 -148
  124. package/src/stavesection.ts +0 -54
  125. package/src/stavetempo.ts +0 -162
  126. package/src/stavetext.ts +0 -69
  127. package/src/stavetie.ts +0 -235
  128. package/src/stavevolta.ts +0 -66
  129. package/src/stem.ts +0 -225
  130. package/src/stemmablenote.ts +0 -248
  131. package/src/stringnumber.ts +0 -286
  132. package/src/strokes.ts +0 -214
  133. package/src/svgcontext.ts +0 -639
  134. package/src/system.ts +0 -327
  135. package/src/tables.ts +0 -796
  136. package/src/tabnote.ts +0 -449
  137. package/src/tabslide.ts +0 -115
  138. package/src/tabstave.ts +0 -31
  139. package/src/tabtie.ts +0 -44
  140. package/src/textbracket.ts +0 -231
  141. package/src/textdynamics.ts +0 -101
  142. package/src/textnote.ts +0 -175
  143. package/src/tickable.ts +0 -374
  144. package/src/tickcontext.ts +0 -280
  145. package/src/timesignature.ts +0 -229
  146. package/src/timesignote.ts +0 -45
  147. package/src/tremolo.ts +0 -51
  148. package/src/tuning.ts +0 -90
  149. package/src/tuplet.ts +0 -349
  150. package/src/typeguard.ts +0 -136
  151. package/src/util.ts +0 -169
  152. package/src/version.ts +0 -4
  153. package/src/vexflow.ts +0 -378
  154. package/src/vibrato.ts +0 -99
  155. package/src/vibratobracket.ts +0 -81
  156. package/src/voice.ts +0 -302
  157. package/src/web.ts +0 -35
package/src/bend.ts DELETED
@@ -1,268 +0,0 @@
1
- // Copyright (c) 2023-present VexFlow contributors: https://github.com/vexflow/vexflow/graphs/contributors
2
- // MIT License
3
-
4
- import { Element } from './element';
5
- import { Modifier } from './modifier';
6
- import { ModifierContextState } from './modifiercontext';
7
- import { Category, isTabNote } from './typeguard';
8
- import { RuntimeError } from './util';
9
-
10
- export interface BendPhrase {
11
- x?: number;
12
- type: number;
13
- text: string;
14
- width?: number;
15
- drawWidth?: number;
16
- }
17
-
18
- /** Bend implements tablature bends. */
19
- export class Bend extends Modifier {
20
- static get CATEGORY(): string {
21
- return Category.Bend;
22
- }
23
-
24
- static get UP(): number {
25
- return 0;
26
- }
27
-
28
- static get DOWN(): number {
29
- return 1;
30
- }
31
-
32
- // Arrange bends in `ModifierContext`
33
- static format(bends: Bend[], state: ModifierContextState): boolean {
34
- if (!bends || bends.length === 0) return false;
35
-
36
- let lastWidth = 0;
37
- // Format Bends
38
- for (let i = 0; i < bends.length; ++i) {
39
- const bend = bends[i];
40
- const note = bend.checkAttachedNote();
41
-
42
- if (isTabNote(note)) {
43
- const stringPos = note.leastString() - 1;
44
- if (state.topTextLine < stringPos) {
45
- state.topTextLine = stringPos;
46
- }
47
- }
48
- bend.setXShift(lastWidth);
49
- lastWidth = bend.getWidth();
50
- bend.setTextLine(state.topTextLine);
51
- }
52
-
53
- state.rightShift += lastWidth;
54
- state.topTextLine += 1;
55
- return true;
56
- }
57
-
58
- protected tap: string;
59
- protected phrase: BendPhrase[];
60
-
61
- public renderOptions: {
62
- releaseWidth: number;
63
- bendWidth: number;
64
- };
65
-
66
- /**
67
- * Example of a phrase:
68
- * ```
69
- * [{
70
- * type: UP,
71
- * text: "whole"
72
- * width: 8;
73
- * },
74
- * {
75
- * type: DOWN,
76
- * text: "whole"
77
- * width: 8;
78
- * },
79
- * {
80
- * type: UP,
81
- * text: "half"
82
- * width: 8;
83
- * },
84
- * {
85
- * type: UP,
86
- * text: "whole"
87
- * width: 8;
88
- * },
89
- * {
90
- * type: DOWN,
91
- * text: "1 1/2"
92
- * width: 8;
93
- * }]
94
- * ```
95
- */
96
- constructor(phrase: BendPhrase[]) {
97
- super();
98
-
99
- this.xShift = 0;
100
- this.tap = '';
101
- this.renderOptions = {
102
- bendWidth: 8,
103
- releaseWidth: 8,
104
- };
105
- this.setStyle({ lineWidth: 1.0, strokeStyle: 'black', fillStyle: 'black' });
106
-
107
- this.phrase = phrase;
108
-
109
- this.updateWidth();
110
- }
111
-
112
- /** Set horizontal shift in pixels. */
113
- setXShift(value: number): this {
114
- this.xShift = value;
115
- this.updateWidth();
116
- return this;
117
- }
118
-
119
- setTap(value: string): this {
120
- this.tap = value;
121
- return this;
122
- }
123
-
124
- getTextHeight(): number {
125
- const element = new Element(Category.Bend);
126
- element.setText(this.phrase[0].text);
127
- return element.getHeight();
128
- }
129
-
130
- /** Recalculate width. */
131
- protected updateWidth(): this {
132
- const measureText = (text: string) => {
133
- const element = new Element(Category.Bend);
134
- element.setText(text);
135
- return element.getWidth();
136
- };
137
-
138
- let totalWidth = 0;
139
- for (let i = 0; i < this.phrase.length; ++i) {
140
- const bend = this.phrase[i];
141
- if (bend.width !== undefined) {
142
- totalWidth += bend.width;
143
- } else {
144
- const additionalWidth = bend.type === Bend.UP ? this.renderOptions.bendWidth : this.renderOptions.releaseWidth;
145
-
146
- bend.width = Math.max(additionalWidth, measureText(bend.text)) + 3;
147
- bend.drawWidth = bend.width / 2;
148
- totalWidth += bend.width;
149
- }
150
- }
151
-
152
- this.setWidth(totalWidth + this.xShift);
153
- return this;
154
- }
155
-
156
- /** Draw the bend on the rendering context. */
157
- draw(): void {
158
- const ctx = this.checkContext();
159
- const note = this.checkAttachedNote();
160
- this.setRendered();
161
- this.applyStyle();
162
-
163
- const start = note.getModifierStartXY(Modifier.Position.RIGHT, this.index);
164
- start.x += 3;
165
- start.y += 0.5;
166
- const xShift = this.xShift;
167
-
168
- const stave = note.checkStave();
169
- const spacing = stave.getSpacingBetweenLines();
170
- const lowestY = note.getYs().reduce((a, b) => (a < b ? a : b));
171
- // this.textLine is relative to top string in the group.
172
- const bendHeight = start.y - ((this.textLine + 1) * spacing + start.y - lowestY) + 3;
173
- const annotationY = start.y - ((this.textLine + 1) * spacing + start.y - lowestY) - 1;
174
-
175
- const renderBend = (x: number, y: number, width: number, height: number) => {
176
- const cpX = x + width;
177
- const cpY = y;
178
-
179
- ctx.beginPath();
180
- ctx.moveTo(x, y);
181
- ctx.quadraticCurveTo(cpX, cpY, x + width, height);
182
- ctx.stroke();
183
- };
184
-
185
- const renderRelease = (x: number, y: number, width: number, height: number) => {
186
- ctx.beginPath();
187
- ctx.moveTo(x, height);
188
- ctx.quadraticCurveTo(x + width, height, x + width, y);
189
- ctx.stroke();
190
- };
191
-
192
- const renderArrowHead = (x: number, y: number, direction: number) => {
193
- const width = 4;
194
- const yBase = y + width * direction;
195
-
196
- ctx.beginPath();
197
- ctx.moveTo(x, y); // tip of the arrow
198
- ctx.lineTo(x - width, yBase);
199
- ctx.lineTo(x + width, yBase);
200
- ctx.closePath();
201
- ctx.fill();
202
- };
203
-
204
- const renderText = (x: number, text: string) => {
205
- ctx.setFont(this.fontInfo);
206
- const renderX = x - ctx.measureText(text).width / 2;
207
- ctx.fillText(text, renderX, annotationY);
208
- };
209
-
210
- let lastBend = undefined;
211
- let lastBendDrawWidth = 0;
212
- let lastDrawnWidth = 0;
213
- if (this.tap?.length) {
214
- const tapStart = note.getModifierStartXY(Modifier.Position.CENTER, this.index);
215
- renderText(tapStart.x, this.tap);
216
- }
217
-
218
- for (let i = 0; i < this.phrase.length; ++i) {
219
- const bend = this.phrase[i];
220
- if (!bend.drawWidth) bend.drawWidth = 0;
221
- if (i === 0) bend.drawWidth += xShift;
222
-
223
- lastDrawnWidth = bend.drawWidth + lastBendDrawWidth - (i === 1 ? xShift : 0);
224
- if (bend.type === Bend.UP) {
225
- if (lastBend && lastBend.type === Bend.UP) {
226
- renderArrowHead(start.x, bendHeight, +1);
227
- }
228
-
229
- renderBend(start.x, start.y, lastDrawnWidth, bendHeight);
230
- }
231
-
232
- if (bend.type === Bend.DOWN) {
233
- if (lastBend && lastBend.type === Bend.UP) {
234
- renderRelease(start.x, start.y, lastDrawnWidth, bendHeight);
235
- }
236
-
237
- if (lastBend && lastBend.type === Bend.DOWN) {
238
- renderArrowHead(start.x, start.y, -1);
239
- renderRelease(start.x, start.y, lastDrawnWidth, bendHeight);
240
- }
241
-
242
- if (!lastBend) {
243
- lastDrawnWidth = bend.drawWidth;
244
- renderRelease(start.x, start.y, lastDrawnWidth, bendHeight);
245
- }
246
- }
247
-
248
- renderText(start.x + lastDrawnWidth, bend.text);
249
- lastBend = bend;
250
- lastBendDrawWidth = bend.drawWidth;
251
- lastBend.x = start.x;
252
-
253
- start.x += lastDrawnWidth;
254
- }
255
-
256
- if (!lastBend || lastBend.x === undefined) {
257
- throw new RuntimeError('NoLastBendForBend', 'Internal error.');
258
- }
259
-
260
- // Final arrowhead and text
261
- if (lastBend.type === Bend.UP) {
262
- renderArrowHead(lastBend.x + lastDrawnWidth, bendHeight, +1);
263
- } else if (lastBend.type === Bend.DOWN) {
264
- renderArrowHead(lastBend.x + lastDrawnWidth, start.y, -1);
265
- }
266
- this.restoreStyle();
267
- }
268
- }
@@ -1,108 +0,0 @@
1
- // Copyright (c) 2023-present VexFlow contributors: https://github.com/vexflow/vexflow/graphs/contributors
2
- // MIT License
3
-
4
- // Bounding boxes for interactive notation.
5
-
6
- export interface Bounds {
7
- x: number;
8
- y: number;
9
- w: number;
10
- h: number;
11
- }
12
-
13
- export class BoundingBox implements Bounds {
14
- x: number;
15
- y: number;
16
- w: number;
17
- h: number;
18
-
19
- /**
20
- * Create a new copy.
21
- */
22
- static copy(that: BoundingBox): BoundingBox {
23
- return new BoundingBox(that.x, that.y, that.w, that.h);
24
- }
25
-
26
- constructor(x: number, y: number, w: number, h: number) {
27
- this.x = x;
28
- this.y = y;
29
- this.w = w;
30
- this.h = h;
31
- }
32
-
33
- /** Get x position. */
34
- getX(): number {
35
- return this.x;
36
- }
37
-
38
- /** Get y position. */
39
- getY(): number {
40
- return this.y;
41
- }
42
-
43
- /** Get width. */
44
- getW(): number {
45
- return this.w;
46
- }
47
-
48
- /** Get height. */
49
- getH(): number {
50
- return this.h;
51
- }
52
-
53
- /** Set x position. */
54
- setX(x: number): this {
55
- this.x = x;
56
- return this;
57
- }
58
-
59
- /** Set y position. */
60
- setY(y: number): this {
61
- this.y = y;
62
- return this;
63
- }
64
-
65
- /** Set width. */
66
- setW(w: number): this {
67
- this.w = w;
68
- return this;
69
- }
70
-
71
- /** Set height. */
72
- setH(h: number): this {
73
- this.h = h;
74
- return this;
75
- }
76
-
77
- /** Move to position. */
78
- move(x: number, y: number): this {
79
- this.x += x;
80
- this.y += y;
81
- return this;
82
- }
83
-
84
- /** Clone. */
85
- clone(): BoundingBox {
86
- return BoundingBox.copy(this);
87
- }
88
-
89
- /**
90
- * Merge my box with given box. Creates a bigger bounding box unless
91
- * the given box is contained in this one.
92
- */
93
- mergeWith(boundingBox: BoundingBox): this {
94
- const that = boundingBox;
95
-
96
- const newX = this.x < that.x ? this.x : that.x;
97
- const newY = this.y < that.y ? this.y : that.y;
98
- const newW = Math.max(this.x + this.w, that.x + that.w) - newX;
99
- const newH = Math.max(this.y + this.h, that.y + that.h) - newY;
100
-
101
- this.x = newX;
102
- this.y = newY;
103
- this.w = newW;
104
- this.h = newH;
105
-
106
- return this;
107
- }
108
- }
@@ -1,348 +0,0 @@
1
- // Copyright (c) 2023-present VexFlow contributors: https://github.com/vexflow/vexflow/graphs/contributors
2
- // MIT License
3
-
4
- import { Font, FontInfo } from './font';
5
- import { RenderContext, TextMeasure } from './rendercontext';
6
- import { globalObject, warn } from './util';
7
- import { isHTMLCanvas } from './web';
8
-
9
- // https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/offscreencanvas
10
- // https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/master/types/offscreencanvas/index.d.ts
11
-
12
- interface OffscreenCanvas extends EventTarget {
13
- width: number;
14
- height: number;
15
- // ...more stuff that we removed.
16
- }
17
-
18
- // https://html.spec.whatwg.org/multipage/canvas.html#offscreencanvasrenderingcontext2d
19
- interface OffscreenCanvasRenderingContext2D
20
- extends CanvasState,
21
- CanvasTransform,
22
- CanvasCompositing,
23
- CanvasImageSmoothing,
24
- CanvasFillStrokeStyles,
25
- CanvasShadowStyles,
26
- CanvasFilters,
27
- CanvasRect,
28
- CanvasDrawPath,
29
- CanvasText,
30
- CanvasDrawImage,
31
- CanvasImageData,
32
- CanvasPathDrawingStyles,
33
- CanvasTextDrawingStyles,
34
- CanvasPath {
35
- readonly canvas: OffscreenCanvas;
36
- }
37
-
38
- /**
39
- * A rendering context for the Canvas backend. This class serves as a proxy for the
40
- * underlying CanvasRenderingContext2D object, part of the browser's API.
41
- */
42
- export class CanvasContext extends RenderContext {
43
- /** The 2D rendering context from the Canvas API. Forward method calls to this object. */
44
- context2D: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;
45
-
46
- curTransfrom: DOMMatrix;
47
- /**
48
- * The HTMLCanvasElement or OffscreenCanvas that is associated with the above context.
49
- * If there was no associated `<canvas>` element, just store the default WIDTH / HEIGHT.
50
- */
51
- canvas: HTMLCanvasElement | OffscreenCanvas | { width: number; height: number };
52
-
53
- /** Height of one line of text (in pixels). */
54
- textHeight: number = 0;
55
-
56
- static get WIDTH(): number {
57
- return 600;
58
- }
59
-
60
- static get HEIGHT(): number {
61
- return 400;
62
- }
63
-
64
- static get CANVAS_BROWSER_SIZE_LIMIT(): number {
65
- return 32767; // Chrome/Firefox. Could be determined more precisely by npm module canvas-size.
66
- }
67
-
68
- /**
69
- * Ensure that width and height do not exceed the browser limit.
70
- * @returns array of [width, height] clamped to the browser limit.
71
- */
72
- static sanitizeCanvasDims(width: number, height: number): [number, number] {
73
- const limit = this.CANVAS_BROWSER_SIZE_LIMIT;
74
- if (Math.max(width, height) > limit) {
75
- warn('Canvas dimensions exceed browser limit. Cropping to ' + limit);
76
- if (width > limit) {
77
- width = limit;
78
- }
79
- if (height > limit) {
80
- height = limit;
81
- }
82
- }
83
- return [width, height];
84
- }
85
-
86
- constructor(context: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D) {
87
- super();
88
- this.context2D = context;
89
- this.curTransfrom = context.getTransform();
90
- if (!context.canvas) {
91
- this.canvas = {
92
- width: CanvasContext.WIDTH,
93
- height: CanvasContext.HEIGHT,
94
- };
95
- } else {
96
- this.canvas = context.canvas;
97
- }
98
- }
99
-
100
- /**
101
- * Set all pixels to transparent black rgba(0,0,0,0).
102
- */
103
- clear(): void {
104
- this.context2D.clearRect(0, 0, this.canvas.width, this.canvas.height);
105
- }
106
-
107
- // eslint-disable-next-line
108
- openGroup(cls?: string, id?: string): any {
109
- // Containers not implemented.
110
- }
111
-
112
- closeGroup(): void {
113
- // Containers not implemented.
114
- }
115
-
116
- openRotation(angleDegrees: number, x: number, y: number) {
117
- this.curTransfrom = this.context2D.getTransform();
118
- this.context2D.translate(x, y);
119
- this.context2D.rotate((angleDegrees * Math.PI) / 180);
120
- this.context2D.translate(-x, -y);
121
- }
122
-
123
- closeRotation() {
124
- this.context2D.setTransform(this.curTransfrom);
125
- }
126
-
127
- // eslint-disable-next-line
128
- add(child: any): void {
129
- // Containers not implemented.
130
- }
131
-
132
- setFillStyle(style: string): this {
133
- this.context2D.fillStyle = style;
134
- return this;
135
- }
136
-
137
- /** CanvasContext ignores `setBackgroundFillStyle()`. */
138
- // eslint-disable-next-line
139
- setBackgroundFillStyle(style: string): this {
140
- // DO NOTHING
141
- return this;
142
- }
143
-
144
- setStrokeStyle(style: string): this {
145
- this.context2D.strokeStyle = style;
146
- return this;
147
- }
148
-
149
- setShadowColor(color: string): this {
150
- this.context2D.shadowColor = color;
151
- return this;
152
- }
153
-
154
- setShadowBlur(blur: number): this {
155
- // CanvasRenderingContext2D does not scale the shadow blur by the current
156
- // transform, so we have to do it manually. We assume uniform scaling
157
- // (though allow for rotation) because the blur can only be scaled
158
- // uniformly anyway.
159
- const t = this.context2D.getTransform();
160
- const scale = Math.sqrt(t.a * t.a + t.b * t.b + t.c * t.c + t.d * t.d);
161
- this.context2D.shadowBlur = scale * blur;
162
- return this;
163
- }
164
-
165
- setLineWidth(width: number): this {
166
- this.context2D.lineWidth = width;
167
- return this;
168
- }
169
-
170
- setLineCap(capType: CanvasLineCap): this {
171
- this.context2D.lineCap = capType;
172
- return this;
173
- }
174
-
175
- setLineDash(dash: number[]): this {
176
- this.context2D.setLineDash(dash);
177
- return this;
178
- }
179
-
180
- scale(x: number, y: number): this {
181
- this.context2D.scale(x, y);
182
- return this;
183
- }
184
-
185
- resize(width: number, height: number, devicePixelRatio?: number): this {
186
- const canvas = this.context2D.canvas;
187
- const dpr: number = devicePixelRatio ?? globalObject().devicePixelRatio ?? 1;
188
-
189
- // Scale the canvas size by the device pixel ratio clamping to the maximum supported size.
190
- [width, height] = CanvasContext.sanitizeCanvasDims(width * dpr, height * dpr);
191
-
192
- // Divide back down by the pixel ratio and convert to integers.
193
- width = (width / dpr) | 0;
194
- height = (height / dpr) | 0;
195
-
196
- canvas.width = width * dpr;
197
- canvas.height = height * dpr;
198
-
199
- // The canvas could be an instance of either HTMLCanvasElement or an OffscreenCanvas.
200
- // Only HTMLCanvasElement has a style attribute.
201
- if (isHTMLCanvas(canvas)) {
202
- canvas.style.width = width + 'px';
203
- canvas.style.height = height + 'px';
204
- }
205
-
206
- return this.scale(dpr, dpr);
207
- }
208
-
209
- rect(x: number, y: number, width: number, height: number): this {
210
- this.context2D.rect(x, y, width, height);
211
- return this;
212
- }
213
-
214
- fillRect(x: number, y: number, width: number, height: number): this {
215
- this.context2D.fillRect(x, y, width, height);
216
- return this;
217
- }
218
-
219
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
220
- pointerRect(x: number, y: number, width: number, height: number): this {
221
- return this;
222
- }
223
-
224
- /**
225
- * Set the pixels in a rectangular area to transparent black rgba(0,0,0,0).
226
- */
227
- clearRect(x: number, y: number, width: number, height: number): this {
228
- this.context2D.clearRect(x, y, width, height);
229
- return this;
230
- }
231
-
232
- beginPath(): this {
233
- this.context2D.beginPath();
234
- return this;
235
- }
236
-
237
- moveTo(x: number, y: number): this {
238
- this.context2D.moveTo(x, y);
239
- return this;
240
- }
241
-
242
- lineTo(x: number, y: number): this {
243
- this.context2D.lineTo(x, y);
244
- return this;
245
- }
246
-
247
- bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): this {
248
- this.context2D.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
249
- return this;
250
- }
251
-
252
- quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): this {
253
- this.context2D.quadraticCurveTo(cpx, cpy, x, y);
254
- return this;
255
- }
256
-
257
- arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, counterclockwise: boolean): this {
258
- this.context2D.arc(x, y, radius, startAngle, endAngle, counterclockwise);
259
- return this;
260
- }
261
-
262
- fill(): this {
263
- this.context2D.fill();
264
- return this;
265
- }
266
-
267
- stroke(): this {
268
- this.context2D.stroke();
269
- return this;
270
- }
271
-
272
- closePath(): this {
273
- this.context2D.closePath();
274
- return this;
275
- }
276
-
277
- measureText(text: string): TextMeasure {
278
- const metrics = this.context2D.measureText(text);
279
-
280
- let y = 0;
281
- let height = 0;
282
- if (metrics.fontBoundingBoxAscent) {
283
- y = -metrics.fontBoundingBoxAscent;
284
- height = metrics.fontBoundingBoxDescent + metrics.fontBoundingBoxAscent;
285
- } else {
286
- y = -metrics.actualBoundingBoxAscent;
287
- height = metrics.actualBoundingBoxDescent + metrics.actualBoundingBoxAscent;
288
- }
289
- // Return x, y, width & height in the same manner as svg getBBox
290
- return {
291
- x: 0,
292
- y: y,
293
- width: metrics.width,
294
- height: height,
295
- };
296
- }
297
-
298
- fillText(text: string, x: number, y: number): this {
299
- this.context2D.fillText(text, x, y);
300
- return this;
301
- }
302
-
303
- save(): this {
304
- this.context2D.save();
305
- return this;
306
- }
307
-
308
- restore(): this {
309
- this.context2D.restore();
310
- return this;
311
- }
312
-
313
- set fillStyle(style: string | CanvasGradient | CanvasPattern) {
314
- this.context2D.fillStyle = style;
315
- }
316
-
317
- get fillStyle(): string | CanvasGradient | CanvasPattern {
318
- return this.context2D.fillStyle;
319
- }
320
-
321
- set strokeStyle(style: string | CanvasGradient | CanvasPattern) {
322
- this.context2D.strokeStyle = style;
323
- }
324
-
325
- get strokeStyle(): string | CanvasGradient | CanvasPattern {
326
- return this.context2D.strokeStyle;
327
- }
328
-
329
- /**
330
- * @param f is 1) a `FontInfo` object or
331
- * 2) a string formatted as CSS font shorthand (e.g., 'bold 10pt Arial') or
332
- * 3) a string representing the font family (one of `size`, `weight`, or `style` must also be provided).
333
- * @param size a string specifying the font size and unit (e.g., '16pt'), or a number (the unit is assumed to be 'pt').
334
- * @param weight is a string (e.g., 'bold', 'normal') or a number (100, 200, ... 900).
335
- * @param style is a string (e.g., 'italic', 'normal').
336
- */
337
- setFont(f?: string | FontInfo, size?: string | number, weight?: string | number, style?: string): this {
338
- const fontInfo = Font.validate(f, size, weight, style);
339
- this.context2D.font = Font.toCSSString(fontInfo);
340
- this.textHeight = Font.convertSizeToPixelValue(fontInfo.size);
341
- return this;
342
- }
343
-
344
- /** Return a string of the form `'italic bold 15pt Arial'` */
345
- getFont(): string {
346
- return this.context2D.font;
347
- }
348
- }