sliver-engine 0.0.1-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.
- package/README.md +37 -0
- package/dist/Assets/Shapes/Arrow.d.ts +12 -0
- package/dist/Assets/Shapes/Arrow.d.ts.map +1 -0
- package/dist/Assets/Shapes/Arrow.js +32 -0
- package/dist/Assets/Shapes/Arrow.js.map +1 -0
- package/dist/Assets/Shapes/index.d.ts +4 -0
- package/dist/Assets/Shapes/index.d.ts.map +1 -0
- package/dist/Assets/Shapes/index.js +2 -0
- package/dist/Assets/Shapes/index.js.map +1 -0
- package/dist/CanvasController/SpriteLibrary.d.ts +58 -0
- package/dist/CanvasController/SpriteLibrary.d.ts.map +1 -0
- package/dist/CanvasController/SpriteLibrary.js +412 -0
- package/dist/CanvasController/SpriteLibrary.js.map +1 -0
- package/dist/CanvasController/Transformations.d.ts +5 -0
- package/dist/CanvasController/Transformations.d.ts.map +1 -0
- package/dist/CanvasController/Transformations.js +19 -0
- package/dist/CanvasController/Transformations.js.map +1 -0
- package/dist/CanvasController/index.d.ts +238 -0
- package/dist/CanvasController/index.d.ts.map +1 -0
- package/dist/CanvasController/index.js +418 -0
- package/dist/CanvasController/index.js.map +1 -0
- package/dist/Context/index.d.ts +53 -0
- package/dist/Context/index.d.ts.map +1 -0
- package/dist/Context/index.js +103 -0
- package/dist/Context/index.js.map +1 -0
- package/dist/Events/decorators.d.ts +74 -0
- package/dist/Events/decorators.d.ts.map +1 -0
- package/dist/Events/decorators.js +417 -0
- package/dist/Events/decorators.js.map +1 -0
- package/dist/Events/index.d.ts +50 -0
- package/dist/Events/index.d.ts.map +1 -0
- package/dist/Events/index.js +73 -0
- package/dist/Events/index.js.map +1 -0
- package/dist/Events/keyAccumulator.d.ts +9 -0
- package/dist/Events/keyAccumulator.d.ts.map +1 -0
- package/dist/Events/keyAccumulator.js +17 -0
- package/dist/Events/keyAccumulator.js.map +1 -0
- package/dist/Game/Saves/index.d.ts +39 -0
- package/dist/Game/Saves/index.d.ts.map +1 -0
- package/dist/Game/Saves/index.js +213 -0
- package/dist/Game/Saves/index.js.map +1 -0
- package/dist/Game/index.d.ts +45 -0
- package/dist/Game/index.d.ts.map +1 -0
- package/dist/Game/index.js +101 -0
- package/dist/Game/index.js.map +1 -0
- package/dist/GameObject/Decorators/index.d.ts +81 -0
- package/dist/GameObject/Decorators/index.d.ts.map +1 -0
- package/dist/GameObject/Decorators/index.js +183 -0
- package/dist/GameObject/Decorators/index.js.map +1 -0
- package/dist/GameObject/Hitboxes/ColisionHandler.d.ts +23 -0
- package/dist/GameObject/Hitboxes/ColisionHandler.d.ts.map +1 -0
- package/dist/GameObject/Hitboxes/ColisionHandler.js +458 -0
- package/dist/GameObject/Hitboxes/ColisionHandler.js.map +1 -0
- package/dist/GameObject/Hitboxes/index.d.ts +50 -0
- package/dist/GameObject/Hitboxes/index.d.ts.map +1 -0
- package/dist/GameObject/Hitboxes/index.js +306 -0
- package/dist/GameObject/Hitboxes/index.js.map +1 -0
- package/dist/GameObject/Library/Button.d.ts +15 -0
- package/dist/GameObject/Library/Button.d.ts.map +1 -0
- package/dist/GameObject/Library/Button.js +45 -0
- package/dist/GameObject/Library/Button.js.map +1 -0
- package/dist/GameObject/Library/Clickableshape.d.ts +14 -0
- package/dist/GameObject/Library/Clickableshape.d.ts.map +1 -0
- package/dist/GameObject/Library/Clickableshape.js +36 -0
- package/dist/GameObject/Library/Clickableshape.js.map +1 -0
- package/dist/GameObject/Library/FloatingView.d.ts +20 -0
- package/dist/GameObject/Library/FloatingView.d.ts.map +1 -0
- package/dist/GameObject/Library/FloatingView.js +39 -0
- package/dist/GameObject/Library/FloatingView.js.map +1 -0
- package/dist/GameObject/Library/Group.d.ts +8 -0
- package/dist/GameObject/Library/Group.d.ts.map +1 -0
- package/dist/GameObject/Library/Group.js +26 -0
- package/dist/GameObject/Library/Group.js.map +1 -0
- package/dist/GameObject/Library/HoverableShape.d.ts +15 -0
- package/dist/GameObject/Library/HoverableShape.d.ts.map +1 -0
- package/dist/GameObject/Library/HoverableShape.js +41 -0
- package/dist/GameObject/Library/HoverableShape.js.map +1 -0
- package/dist/GameObject/Library/SceneTranslator.d.ts +14 -0
- package/dist/GameObject/Library/SceneTranslator.d.ts.map +1 -0
- package/dist/GameObject/Library/SceneTranslator.js +104 -0
- package/dist/GameObject/Library/SceneTranslator.js.map +1 -0
- package/dist/GameObject/Library/ScrollView.d.ts +41 -0
- package/dist/GameObject/Library/ScrollView.d.ts.map +1 -0
- package/dist/GameObject/Library/ScrollView.js +112 -0
- package/dist/GameObject/Library/ScrollView.js.map +1 -0
- package/dist/GameObject/Library/ShowOnHover.d.ts +13 -0
- package/dist/GameObject/Library/ShowOnHover.d.ts.map +1 -0
- package/dist/GameObject/Library/ShowOnHover.js +33 -0
- package/dist/GameObject/Library/ShowOnHover.js.map +1 -0
- package/dist/GameObject/Library/Text.d.ts +25 -0
- package/dist/GameObject/Library/Text.d.ts.map +1 -0
- package/dist/GameObject/Library/Text.js +35 -0
- package/dist/GameObject/Library/Text.js.map +1 -0
- package/dist/GameObject/Library/TextBox.d.ts +45 -0
- package/dist/GameObject/Library/TextBox.d.ts.map +1 -0
- package/dist/GameObject/Library/TextBox.js +170 -0
- package/dist/GameObject/Library/TextBox.js.map +1 -0
- package/dist/GameObject/Library/index.d.ts +11 -0
- package/dist/GameObject/Library/index.d.ts.map +1 -0
- package/dist/GameObject/Library/index.js +11 -0
- package/dist/GameObject/Library/index.js.map +1 -0
- package/dist/GameObject/Walker.d.ts +91 -0
- package/dist/GameObject/Walker.d.ts.map +1 -0
- package/dist/GameObject/Walker.js +634 -0
- package/dist/GameObject/Walker.js.map +1 -0
- package/dist/GameObject/index.d.ts +97 -0
- package/dist/GameObject/index.d.ts.map +1 -0
- package/dist/GameObject/index.js +386 -0
- package/dist/GameObject/index.js.map +1 -0
- package/dist/Lib/Math.d.ts +2 -0
- package/dist/Lib/Math.d.ts.map +1 -0
- package/dist/Lib/Math.js +4 -0
- package/dist/Lib/Math.js.map +1 -0
- package/dist/Lib/MinHeap/index.d.ts +12 -0
- package/dist/Lib/MinHeap/index.d.ts.map +1 -0
- package/dist/Lib/MinHeap/index.js +64 -0
- package/dist/Lib/MinHeap/index.js.map +1 -0
- package/dist/Lib/Mixins/index.d.ts +8 -0
- package/dist/Lib/Mixins/index.d.ts.map +1 -0
- package/dist/Lib/Mixins/index.js +45 -0
- package/dist/Lib/Mixins/index.js.map +1 -0
- package/dist/Lib/PathfindingProxy/index.d.ts +16 -0
- package/dist/Lib/PathfindingProxy/index.d.ts.map +1 -0
- package/dist/Lib/PathfindingProxy/index.js +48 -0
- package/dist/Lib/PathfindingProxy/index.js.map +1 -0
- package/dist/Lib/Vector/index.d.ts +24 -0
- package/dist/Lib/Vector/index.d.ts.map +1 -0
- package/dist/Lib/Vector/index.js +94 -0
- package/dist/Lib/Vector/index.js.map +1 -0
- package/dist/Lib/index.d.ts +6 -0
- package/dist/Lib/index.d.ts.map +1 -0
- package/dist/Lib/index.js +6 -0
- package/dist/Lib/index.js.map +1 -0
- package/dist/Scenes/SceneManager/Transitions.d.ts +37 -0
- package/dist/Scenes/SceneManager/Transitions.d.ts.map +1 -0
- package/dist/Scenes/SceneManager/Transitions.js +165 -0
- package/dist/Scenes/SceneManager/Transitions.js.map +1 -0
- package/dist/Scenes/SceneManager/index.d.ts +24 -0
- package/dist/Scenes/SceneManager/index.d.ts.map +1 -0
- package/dist/Scenes/SceneManager/index.js +151 -0
- package/dist/Scenes/SceneManager/index.js.map +1 -0
- package/dist/Scenes/index.d.ts +44 -0
- package/dist/Scenes/index.d.ts.map +1 -0
- package/dist/Scenes/index.js +252 -0
- package/dist/Scenes/index.js.map +1 -0
- package/dist/ScriptedEvents/Combinators/index.d.ts +17 -0
- package/dist/ScriptedEvents/Combinators/index.d.ts.map +1 -0
- package/dist/ScriptedEvents/Combinators/index.js +127 -0
- package/dist/ScriptedEvents/Combinators/index.js.map +1 -0
- package/dist/ScriptedEvents/Library/TextBoxSequence.d.ts +7 -0
- package/dist/ScriptedEvents/Library/TextBoxSequence.d.ts.map +1 -0
- package/dist/ScriptedEvents/Library/TextBoxSequence.js +16 -0
- package/dist/ScriptedEvents/Library/TextBoxSequence.js.map +1 -0
- package/dist/ScriptedEvents/Library/WaitForKeyPress.d.ts +2 -0
- package/dist/ScriptedEvents/Library/WaitForKeyPress.d.ts.map +1 -0
- package/dist/ScriptedEvents/Library/WaitForKeyPress.js +1 -0
- package/dist/ScriptedEvents/Library/WaitForKeyPress.js.map +1 -0
- package/dist/ScriptedEvents/Library/WalkCharacter.d.ts +4 -0
- package/dist/ScriptedEvents/Library/WalkCharacter.d.ts.map +1 -0
- package/dist/ScriptedEvents/Library/WalkCharacter.js +14 -0
- package/dist/ScriptedEvents/Library/WalkCharacter.js.map +1 -0
- package/dist/ScriptedEvents/Library/index.d.ts +3 -0
- package/dist/ScriptedEvents/Library/index.d.ts.map +1 -0
- package/dist/ScriptedEvents/Library/index.js +3 -0
- package/dist/ScriptedEvents/Library/index.js.map +1 -0
- package/dist/ScriptedEvents/index.d.ts +42 -0
- package/dist/ScriptedEvents/index.d.ts.map +1 -0
- package/dist/ScriptedEvents/index.js +61 -0
- package/dist/ScriptedEvents/index.js.map +1 -0
- package/dist/SoundManager/index.d.ts +69 -0
- package/dist/SoundManager/index.d.ts.map +1 -0
- package/dist/SoundManager/index.js +208 -0
- package/dist/SoundManager/index.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/package.json +36 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Transformations.js","sourceRoot":"","sources":["../../src/CanvasController/Transformations.ts"],"names":[],"mappings":"AAGA,MAAM,CAAC,MAAM,YAAY,GAAG,CAC1B,KAAa,EACb,KAAsB,EACL,EAAE;IACnB,OAAO,CAAC,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE;QAC7B,GAAG,CAAC,IAAI,EAAE,CAAC;QACX,GAAG,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;QAChE,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAClB,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QACtE,KAAK,CAAC,GAAG,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;QAC3B,GAAG,CAAC,OAAO,EAAE,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,iBAAyB,EACzB,KAAsB,EACL,EAAE;IACnB,OAAO,CAAC,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE;QAC7B,GAAG,CAAC,IAAI,EAAE,CAAC;QACX,GAAG,CAAC,SAAS,CAAC,iBAAiB,CAAC,CAAC,EAAE,iBAAiB,CAAC,CAAC,CAAC,CAAC;QACxD,KAAK,CAAC,GAAG,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;QAC3B,GAAG,CAAC,OAAO,EAAE,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import type { Vector } from "../Lib/Vector";
|
|
2
|
+
import { SpriteLibrary } from "./SpriteLibrary";
|
|
3
|
+
type DrawTextOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* If provided, wraps the text to fit within this max width (in pixels).
|
|
6
|
+
* When omitted, no wrapping is applied.
|
|
7
|
+
*/
|
|
8
|
+
wrapText?: number;
|
|
9
|
+
/**
|
|
10
|
+
* Line height in pixels, used when wrapping is enabled.
|
|
11
|
+
* Defaults to `Math.ceil(fontSizePx * 1.3)`.
|
|
12
|
+
*/
|
|
13
|
+
lineHeight?: number;
|
|
14
|
+
/**
|
|
15
|
+
* Maximum lines to render when wrapping is enabled.
|
|
16
|
+
* Defaults to unlimited.
|
|
17
|
+
*/
|
|
18
|
+
maxLines?: number;
|
|
19
|
+
/**
|
|
20
|
+
* Optional text baseline override for this call.
|
|
21
|
+
*/
|
|
22
|
+
baseline?: CanvasTextBaseline;
|
|
23
|
+
};
|
|
24
|
+
declare class ShapeDrawer {
|
|
25
|
+
private context;
|
|
26
|
+
private spriteLibrary;
|
|
27
|
+
private defaultFont;
|
|
28
|
+
constructor(context: CanvasRenderingContext2D, spriteLibrary: SpriteLibrary);
|
|
29
|
+
/**
|
|
30
|
+
* Executes drawing instructions with a temporary alpha multiplier.
|
|
31
|
+
*/
|
|
32
|
+
withOpacity(opacity: number, draw: () => void): void;
|
|
33
|
+
/**
|
|
34
|
+
* Draws a rectangle on the canvas with configurable fill/stroke behavior.
|
|
35
|
+
*
|
|
36
|
+
* @param x - The x-coordinate of the rectangle's top-left corner
|
|
37
|
+
* @param y - The y-coordinate of the rectangle's top-left corner
|
|
38
|
+
* @param width - The width of the rectangle
|
|
39
|
+
* @param height - The height of the rectangle
|
|
40
|
+
* @param color - The color to use for filling or stroking (default: "black")
|
|
41
|
+
* @param filled - If true, fills the rectangle; if false, strokes the outline (default: true)
|
|
42
|
+
* @param opacity - Alpha multiplier applied while drawing (default: 1)
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* // Draw a filled red rectangle
|
|
46
|
+
* shapeDrawer.drawRectangle(10, 10, 100, 50, "red", true);
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* // Draw a blue rectangle outline
|
|
50
|
+
* shapeDrawer.drawRectangle(10, 10, 100, 50, "blue", false);
|
|
51
|
+
*/
|
|
52
|
+
drawRectangle(x: number, y: number, width: number, height: number, color?: string, filled?: boolean, opacity?: number): void;
|
|
53
|
+
/**
|
|
54
|
+
* Sets the default font family used for text rendering.
|
|
55
|
+
*
|
|
56
|
+
* @param font - The font family name to use as default (e.g., "Arial", "Verdana")
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* // Set default font to Arial
|
|
60
|
+
* shapeDrawer.setDefaultFont("Arial");
|
|
61
|
+
*/
|
|
62
|
+
setDefaultFont(font: string): void;
|
|
63
|
+
getDefaultFont(): string;
|
|
64
|
+
/**
|
|
65
|
+
* Draws a straight line between two points with customizable appearance.
|
|
66
|
+
*
|
|
67
|
+
* @param start - The starting point of the line as a Vector
|
|
68
|
+
* @param end - The ending point of the line as a Vector
|
|
69
|
+
* @param width - The thickness of the line in pixels
|
|
70
|
+
* @param color - The color of the line (default: "red")
|
|
71
|
+
* @param lineCap - The style of the line endings (default: "round")
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* // Draw a thick blue line with square caps
|
|
75
|
+
* shapeDrawer.drawLine(new Vector(10, 10), new Vector(100, 50), 5, "blue", "square");
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* // Draw a thin red line with default round caps
|
|
79
|
+
* shapeDrawer.drawLine(new Vector(0, 0), new Vector(100, 100), 1);
|
|
80
|
+
*/
|
|
81
|
+
drawLine(start: Vector, end: Vector, width: number, color?: string, lineCap?: CanvasLineCap): void;
|
|
82
|
+
/**
|
|
83
|
+
* Draws a circle with customizable appearance and optional center dot.
|
|
84
|
+
*
|
|
85
|
+
* @param x - The x-coordinate of the circle's center
|
|
86
|
+
* @param y - The y-coordinate of the circle's center
|
|
87
|
+
* @param radius - The radius of the circle in pixels
|
|
88
|
+
* @param color - The color to use for filling or stroking the circle (default: "black")
|
|
89
|
+
* @param filled - If true, fills the circle; if false, strokes the outline (default: true)
|
|
90
|
+
* @param centerDot - If true, draws a small dot at the center of the circle (default: true)
|
|
91
|
+
* @param centerDotColor - The color of the center dot (default: "red")
|
|
92
|
+
* @param centerDotRadius - The radius of the center dot in pixels (default: 2)
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* // Draw a filled blue circle with default center dot
|
|
96
|
+
* shapeDrawer.drawCircle(100, 100, 50, "blue", true);
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* // Draw a red circle outline with no center dot
|
|
100
|
+
* shapeDrawer.drawCircle(100, 100, 50, "red");
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* // Draw a green circle with custom center dot
|
|
104
|
+
* shapeDrawer.drawCircle(100, 100, 50, "green", true, true, "yellow", 3);
|
|
105
|
+
*/
|
|
106
|
+
drawCircle(x: number, y: number, radius: number, color?: string, filled?: boolean, centerDot?: boolean, centerDotColor?: string, centerDotRadius?: number): void;
|
|
107
|
+
/**
|
|
108
|
+
* Renders text at the specified position with customizable styling.
|
|
109
|
+
*
|
|
110
|
+
* @param text - The text string to render
|
|
111
|
+
* @param x - The x-coordinate of the text position
|
|
112
|
+
* @param y - The y-coordinate of the text position
|
|
113
|
+
* @param color - The color of the text (default: "black")
|
|
114
|
+
* @param size - The font size in pixels (default: "16px")
|
|
115
|
+
* @param align - The text alignment (default: "center")
|
|
116
|
+
* @param font - Optional font family to override the default font
|
|
117
|
+
* @param options - Optional rendering options (e.g. text wrapping)
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* // Draw centered black text with default font
|
|
121
|
+
* shapeDrawer.drawText("Hello", 100, 50);
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* // Draw right-aligned red text with custom font
|
|
125
|
+
* shapeDrawer.drawText("World", 100, 50, "red", "20px", "right", "Verdana");
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* // Draw left-aligned text with default font but custom size
|
|
129
|
+
* shapeDrawer.drawText("Welcome", 100, 50, "black", "12px", "left");
|
|
130
|
+
*/
|
|
131
|
+
drawText(text: string, x: number, y: number, color?: string, size?: string, align?: CanvasTextAlign, font?: string, options?: DrawTextOptions): void;
|
|
132
|
+
private defaultLineHeightPx;
|
|
133
|
+
private wrapText;
|
|
134
|
+
/**
|
|
135
|
+
* Clears the canvas and fills it with a solid background color.
|
|
136
|
+
*
|
|
137
|
+
* @param color - The color to fill the canvas with (default: "white")
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* // Clear canvas with default white background
|
|
141
|
+
* shapeDrawer.drawBackground();
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* // Clear canvas with custom gray background
|
|
145
|
+
* shapeDrawer.drawBackground("#f0f0f0");
|
|
146
|
+
*/
|
|
147
|
+
drawBackground(color?: string): void;
|
|
148
|
+
/**
|
|
149
|
+
* Executes drawing operations within a clipped rectangular region.
|
|
150
|
+
*
|
|
151
|
+
* This method saves the current canvas state, creates a clipping region,
|
|
152
|
+
* executes the provided drawing function, and then restores the original state.
|
|
153
|
+
*
|
|
154
|
+
* @param x - The x-coordinate of the clipping rectangle's top-left corner
|
|
155
|
+
* @param y - The y-coordinate of the clipping rectangle's top-left corner
|
|
156
|
+
* @param width - The width of the clipping rectangle
|
|
157
|
+
* @param height - The height of the clipping rectangle
|
|
158
|
+
* @param draw - A function containing the drawing operations to perform within the clipped region
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* // Draw shapes only within a specific region
|
|
162
|
+
* shapeDrawer.withClippingRect(50, 50, 200, 200, () => {
|
|
163
|
+
* shapeDrawer.drawCircle(100, 100, 50, "blue");
|
|
164
|
+
* shapeDrawer.drawText("Clipped", 100, 100);
|
|
165
|
+
* });
|
|
166
|
+
*/
|
|
167
|
+
withClippingRect(x: number, y: number, width: number, height: number, draw: () => void): void;
|
|
168
|
+
/**
|
|
169
|
+
* Executes drawing operations with a temporary rotation around a pivot point.
|
|
170
|
+
*
|
|
171
|
+
* This method saves the current canvas state, applies the rotation transform,
|
|
172
|
+
* executes the provided drawing function, and then restores the original state.
|
|
173
|
+
*
|
|
174
|
+
* @param x - The x-coordinate of the rotation pivot
|
|
175
|
+
* @param y - The y-coordinate of the rotation pivot
|
|
176
|
+
* @param angle - Rotation angle in radians
|
|
177
|
+
* @param draw - A function containing the drawing operations to perform while rotated
|
|
178
|
+
*/
|
|
179
|
+
withRotation(x: number, y: number, angle: number, draw: () => void): void;
|
|
180
|
+
/**
|
|
181
|
+
* Draws a sprite from the sprite library onto the canvas.
|
|
182
|
+
*
|
|
183
|
+
* @param spriteName - The name of the sprite to draw (must be loaded in the SpriteLibrary)
|
|
184
|
+
* @param x - The x-coordinate of the top-left corner where the sprite will be drawn
|
|
185
|
+
* @param y - The y-coordinate of the top-left corner where the sprite will be drawn
|
|
186
|
+
* @param width - The width to scale the sprite to
|
|
187
|
+
* @param height - The height to scale the sprite to
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* // Draw a sprite named "player" at position (100, 100) with size 50x50
|
|
191
|
+
* shapeDrawer.drawSprite("player", 100, 100, 50, 50);
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* // Draw a sprite with different dimensions
|
|
195
|
+
* shapeDrawer.drawSprite("enemy", 200, 200, 30, 40);
|
|
196
|
+
*/
|
|
197
|
+
drawSprite(spriteName: string, x: number, y: number, width: number, height: number): void;
|
|
198
|
+
/**
|
|
199
|
+
* Loads a font (local URL or Google Fonts stylesheet) and registers it for use.
|
|
200
|
+
*
|
|
201
|
+
* This method handles both local font files and Google Fonts URLs. For Google Fonts,
|
|
202
|
+
* it injects the stylesheet and waits for the font to be available. For local fonts,
|
|
203
|
+
* it creates a new FontFace object and adds it to the document's font collection.
|
|
204
|
+
*
|
|
205
|
+
* @param fontName - The name of the font to load (must match the font family name)
|
|
206
|
+
* @param url - The URL of the font file or Google Fonts stylesheet
|
|
207
|
+
* @returns A Promise that resolves when the font is successfully loaded and available
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* // Load a Google Font
|
|
211
|
+
* await shapeDrawer.loadFont("Roboto", "https://fonts.googleapis.com/css2?family=Roboto");
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* // Load a local font file
|
|
215
|
+
* await shapeDrawer.loadFont("CustomFont", "/fonts/custom-font.woff2");
|
|
216
|
+
*
|
|
217
|
+
* @throws Will throw an error if the font fails to load
|
|
218
|
+
*/
|
|
219
|
+
loadFont(fontName: string, url: string): Promise<void>;
|
|
220
|
+
}
|
|
221
|
+
declare class CanvasController {
|
|
222
|
+
private canvas;
|
|
223
|
+
private context;
|
|
224
|
+
private spriteLibrary;
|
|
225
|
+
private shapeDrawer;
|
|
226
|
+
constructor(width: number, height: number);
|
|
227
|
+
clearCanvas(): void;
|
|
228
|
+
getCanvas(): HTMLCanvasElement;
|
|
229
|
+
getContext(): CanvasRenderingContext2D;
|
|
230
|
+
getShapeDrawer(): ShapeDrawer;
|
|
231
|
+
getSpriteLibrary(): SpriteLibrary;
|
|
232
|
+
applyTranslation(translationVector: Vector): void;
|
|
233
|
+
reset(): void;
|
|
234
|
+
applyRotation(angle: number): void;
|
|
235
|
+
}
|
|
236
|
+
export * from "./Transformations";
|
|
237
|
+
export { CanvasController, ShapeDrawer, SpriteLibrary };
|
|
238
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/CanvasController/index.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
import { SpriteLibrary } from "./SpriteLibrary";
|
|
2
|
+
class ShapeDrawer {
|
|
3
|
+
context;
|
|
4
|
+
spriteLibrary;
|
|
5
|
+
defaultFont = "Arial";
|
|
6
|
+
constructor(context, spriteLibrary) {
|
|
7
|
+
this.context = context;
|
|
8
|
+
this.spriteLibrary = spriteLibrary;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Executes drawing instructions with a temporary alpha multiplier.
|
|
12
|
+
*/
|
|
13
|
+
withOpacity(opacity, draw) {
|
|
14
|
+
if (opacity >= 1) {
|
|
15
|
+
draw();
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
this.context.save();
|
|
19
|
+
this.context.globalAlpha *= opacity;
|
|
20
|
+
draw();
|
|
21
|
+
this.context.restore();
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Draws a rectangle on the canvas with configurable fill/stroke behavior.
|
|
25
|
+
*
|
|
26
|
+
* @param x - The x-coordinate of the rectangle's top-left corner
|
|
27
|
+
* @param y - The y-coordinate of the rectangle's top-left corner
|
|
28
|
+
* @param width - The width of the rectangle
|
|
29
|
+
* @param height - The height of the rectangle
|
|
30
|
+
* @param color - The color to use for filling or stroking (default: "black")
|
|
31
|
+
* @param filled - If true, fills the rectangle; if false, strokes the outline (default: true)
|
|
32
|
+
* @param opacity - Alpha multiplier applied while drawing (default: 1)
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* // Draw a filled red rectangle
|
|
36
|
+
* shapeDrawer.drawRectangle(10, 10, 100, 50, "red", true);
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* // Draw a blue rectangle outline
|
|
40
|
+
* shapeDrawer.drawRectangle(10, 10, 100, 50, "blue", false);
|
|
41
|
+
*/
|
|
42
|
+
drawRectangle(x, y, width, height, color = "black", filled = true, opacity = 1) {
|
|
43
|
+
this.withOpacity(opacity, () => {
|
|
44
|
+
this.context.fillStyle = color;
|
|
45
|
+
if (filled) {
|
|
46
|
+
this.context.fillRect(x, y, width, height);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
this.context.strokeStyle = color;
|
|
50
|
+
this.context.strokeRect(x, y, width, height);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Sets the default font family used for text rendering.
|
|
56
|
+
*
|
|
57
|
+
* @param font - The font family name to use as default (e.g., "Arial", "Verdana")
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* // Set default font to Arial
|
|
61
|
+
* shapeDrawer.setDefaultFont("Arial");
|
|
62
|
+
*/
|
|
63
|
+
setDefaultFont(font) {
|
|
64
|
+
this.defaultFont = font;
|
|
65
|
+
}
|
|
66
|
+
getDefaultFont() {
|
|
67
|
+
return this.defaultFont;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Draws a straight line between two points with customizable appearance.
|
|
71
|
+
*
|
|
72
|
+
* @param start - The starting point of the line as a Vector
|
|
73
|
+
* @param end - The ending point of the line as a Vector
|
|
74
|
+
* @param width - The thickness of the line in pixels
|
|
75
|
+
* @param color - The color of the line (default: "red")
|
|
76
|
+
* @param lineCap - The style of the line endings (default: "round")
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* // Draw a thick blue line with square caps
|
|
80
|
+
* shapeDrawer.drawLine(new Vector(10, 10), new Vector(100, 50), 5, "blue", "square");
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* // Draw a thin red line with default round caps
|
|
84
|
+
* shapeDrawer.drawLine(new Vector(0, 0), new Vector(100, 100), 1);
|
|
85
|
+
*/
|
|
86
|
+
drawLine(start, end, width, color = "red", lineCap = "round") {
|
|
87
|
+
this.context.beginPath();
|
|
88
|
+
this.context.lineWidth = width;
|
|
89
|
+
this.context.strokeStyle = color;
|
|
90
|
+
this.context.lineCap = lineCap;
|
|
91
|
+
this.context.moveTo(start.x, start.y);
|
|
92
|
+
this.context.lineTo(end.x, end.y);
|
|
93
|
+
this.context.stroke();
|
|
94
|
+
this.context.closePath();
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Draws a circle with customizable appearance and optional center dot.
|
|
98
|
+
*
|
|
99
|
+
* @param x - The x-coordinate of the circle's center
|
|
100
|
+
* @param y - The y-coordinate of the circle's center
|
|
101
|
+
* @param radius - The radius of the circle in pixels
|
|
102
|
+
* @param color - The color to use for filling or stroking the circle (default: "black")
|
|
103
|
+
* @param filled - If true, fills the circle; if false, strokes the outline (default: true)
|
|
104
|
+
* @param centerDot - If true, draws a small dot at the center of the circle (default: true)
|
|
105
|
+
* @param centerDotColor - The color of the center dot (default: "red")
|
|
106
|
+
* @param centerDotRadius - The radius of the center dot in pixels (default: 2)
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* // Draw a filled blue circle with default center dot
|
|
110
|
+
* shapeDrawer.drawCircle(100, 100, 50, "blue", true);
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* // Draw a red circle outline with no center dot
|
|
114
|
+
* shapeDrawer.drawCircle(100, 100, 50, "red");
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* // Draw a green circle with custom center dot
|
|
118
|
+
* shapeDrawer.drawCircle(100, 100, 50, "green", true, true, "yellow", 3);
|
|
119
|
+
*/
|
|
120
|
+
drawCircle(x, y, radius, color = "black", filled = true, centerDot = true, centerDotColor = "red", centerDotRadius = 2) {
|
|
121
|
+
this.context.beginPath();
|
|
122
|
+
this.context.arc(x, y, radius, 0, Math.PI * 2);
|
|
123
|
+
if (filled) {
|
|
124
|
+
this.context.fillStyle = color;
|
|
125
|
+
this.context.fill();
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
this.context.strokeStyle = color;
|
|
129
|
+
this.context.stroke();
|
|
130
|
+
}
|
|
131
|
+
if (centerDot) {
|
|
132
|
+
this.context.beginPath();
|
|
133
|
+
this.context.arc(x, y, centerDotRadius, 0, Math.PI * 2);
|
|
134
|
+
this.context.fillStyle = centerDotColor;
|
|
135
|
+
this.context.fill();
|
|
136
|
+
}
|
|
137
|
+
this.context.closePath();
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Renders text at the specified position with customizable styling.
|
|
141
|
+
*
|
|
142
|
+
* @param text - The text string to render
|
|
143
|
+
* @param x - The x-coordinate of the text position
|
|
144
|
+
* @param y - The y-coordinate of the text position
|
|
145
|
+
* @param color - The color of the text (default: "black")
|
|
146
|
+
* @param size - The font size in pixels (default: "16px")
|
|
147
|
+
* @param align - The text alignment (default: "center")
|
|
148
|
+
* @param font - Optional font family to override the default font
|
|
149
|
+
* @param options - Optional rendering options (e.g. text wrapping)
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* // Draw centered black text with default font
|
|
153
|
+
* shapeDrawer.drawText("Hello", 100, 50);
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* // Draw right-aligned red text with custom font
|
|
157
|
+
* shapeDrawer.drawText("World", 100, 50, "red", "20px", "right", "Verdana");
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* // Draw left-aligned text with default font but custom size
|
|
161
|
+
* shapeDrawer.drawText("Welcome", 100, 50, "black", "12px", "left");
|
|
162
|
+
*/
|
|
163
|
+
drawText(text, x, y, color = "black", size = "16px", align = "center", font, options) {
|
|
164
|
+
const fontToUse = font ?? this.defaultFont;
|
|
165
|
+
const previousBaseline = this.context.textBaseline;
|
|
166
|
+
this.context.fillStyle = color;
|
|
167
|
+
this.context.font = `${size} ${fontToUse}`;
|
|
168
|
+
this.context.textAlign = align;
|
|
169
|
+
if (options?.baseline) {
|
|
170
|
+
this.context.textBaseline = options.baseline;
|
|
171
|
+
}
|
|
172
|
+
const maxWidth = options?.wrapText;
|
|
173
|
+
if (maxWidth === undefined) {
|
|
174
|
+
this.context.fillText(text, x, y);
|
|
175
|
+
if (options?.baseline)
|
|
176
|
+
this.context.textBaseline = previousBaseline;
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
const lines = this.wrapText(this.context, text, maxWidth);
|
|
180
|
+
const lineHeight = options?.lineHeight ?? this.defaultLineHeightPx(size);
|
|
181
|
+
const maxLines = options?.maxLines ?? Number.POSITIVE_INFINITY;
|
|
182
|
+
for (let i = 0; i < Math.min(lines.length, maxLines); i += 1) {
|
|
183
|
+
this.context.fillText(lines[i] ?? "", x, y + i * lineHeight);
|
|
184
|
+
}
|
|
185
|
+
if (options?.baseline)
|
|
186
|
+
this.context.textBaseline = previousBaseline;
|
|
187
|
+
}
|
|
188
|
+
defaultLineHeightPx(size) {
|
|
189
|
+
const match = /(-?\d+(\.\d+)?)px/.exec(size);
|
|
190
|
+
const parsed = match ? Number(match[1]) : 16;
|
|
191
|
+
const fontPx = Number.isFinite(parsed) ? parsed : 16;
|
|
192
|
+
return Math.ceil(fontPx * 1.3);
|
|
193
|
+
}
|
|
194
|
+
wrapText(ctx, text, maxWidth) {
|
|
195
|
+
if (maxWidth <= 0)
|
|
196
|
+
return [];
|
|
197
|
+
const hardLines = text.split("\n");
|
|
198
|
+
const out = [];
|
|
199
|
+
for (const hardLine of hardLines) {
|
|
200
|
+
const line = hardLine.trimEnd();
|
|
201
|
+
if (line.trim().length === 0) {
|
|
202
|
+
out.push("");
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
const words = line.split(/\s+/g);
|
|
206
|
+
let current = "";
|
|
207
|
+
for (const word of words) {
|
|
208
|
+
const candidate = current.length === 0 ? word : `${current} ${word}`;
|
|
209
|
+
if (ctx.measureText(candidate).width <= maxWidth) {
|
|
210
|
+
current = candidate;
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
if (current.length > 0) {
|
|
214
|
+
out.push(current);
|
|
215
|
+
current = word;
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
// Word is longer than maxWidth; hard-break it.
|
|
219
|
+
let chunk = "";
|
|
220
|
+
for (const char of word) {
|
|
221
|
+
const next = `${chunk}${char}`;
|
|
222
|
+
if (ctx.measureText(next).width <= maxWidth) {
|
|
223
|
+
chunk = next;
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
if (chunk.length > 0) {
|
|
227
|
+
out.push(chunk);
|
|
228
|
+
chunk = char;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
if (chunk.length > 0)
|
|
232
|
+
out.push(chunk);
|
|
233
|
+
current = "";
|
|
234
|
+
}
|
|
235
|
+
if (current.length > 0)
|
|
236
|
+
out.push(current);
|
|
237
|
+
}
|
|
238
|
+
return out;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Clears the canvas and fills it with a solid background color.
|
|
242
|
+
*
|
|
243
|
+
* @param color - The color to fill the canvas with (default: "white")
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* // Clear canvas with default white background
|
|
247
|
+
* shapeDrawer.drawBackground();
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* // Clear canvas with custom gray background
|
|
251
|
+
* shapeDrawer.drawBackground("#f0f0f0");
|
|
252
|
+
*/
|
|
253
|
+
drawBackground(color = "white") {
|
|
254
|
+
this.context.fillStyle = color;
|
|
255
|
+
this.context.fillRect(0, 0, this.context.canvas.width, this.context.canvas.height);
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Executes drawing operations within a clipped rectangular region.
|
|
259
|
+
*
|
|
260
|
+
* This method saves the current canvas state, creates a clipping region,
|
|
261
|
+
* executes the provided drawing function, and then restores the original state.
|
|
262
|
+
*
|
|
263
|
+
* @param x - The x-coordinate of the clipping rectangle's top-left corner
|
|
264
|
+
* @param y - The y-coordinate of the clipping rectangle's top-left corner
|
|
265
|
+
* @param width - The width of the clipping rectangle
|
|
266
|
+
* @param height - The height of the clipping rectangle
|
|
267
|
+
* @param draw - A function containing the drawing operations to perform within the clipped region
|
|
268
|
+
*
|
|
269
|
+
* @example
|
|
270
|
+
* // Draw shapes only within a specific region
|
|
271
|
+
* shapeDrawer.withClippingRect(50, 50, 200, 200, () => {
|
|
272
|
+
* shapeDrawer.drawCircle(100, 100, 50, "blue");
|
|
273
|
+
* shapeDrawer.drawText("Clipped", 100, 100);
|
|
274
|
+
* });
|
|
275
|
+
*/
|
|
276
|
+
withClippingRect(x, y, width, height, draw) {
|
|
277
|
+
this.context.save();
|
|
278
|
+
this.context.beginPath();
|
|
279
|
+
this.context.rect(x, y, width, height);
|
|
280
|
+
this.context.clip();
|
|
281
|
+
draw();
|
|
282
|
+
this.context.restore();
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Executes drawing operations with a temporary rotation around a pivot point.
|
|
286
|
+
*
|
|
287
|
+
* This method saves the current canvas state, applies the rotation transform,
|
|
288
|
+
* executes the provided drawing function, and then restores the original state.
|
|
289
|
+
*
|
|
290
|
+
* @param x - The x-coordinate of the rotation pivot
|
|
291
|
+
* @param y - The y-coordinate of the rotation pivot
|
|
292
|
+
* @param angle - Rotation angle in radians
|
|
293
|
+
* @param draw - A function containing the drawing operations to perform while rotated
|
|
294
|
+
*/
|
|
295
|
+
withRotation(x, y, angle, draw) {
|
|
296
|
+
if (angle === 0) {
|
|
297
|
+
draw();
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
this.context.save();
|
|
301
|
+
this.context.translate(x, y);
|
|
302
|
+
this.context.rotate(angle);
|
|
303
|
+
this.context.translate(-x, -y);
|
|
304
|
+
draw();
|
|
305
|
+
this.context.restore();
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Draws a sprite from the sprite library onto the canvas.
|
|
309
|
+
*
|
|
310
|
+
* @param spriteName - The name of the sprite to draw (must be loaded in the SpriteLibrary)
|
|
311
|
+
* @param x - The x-coordinate of the top-left corner where the sprite will be drawn
|
|
312
|
+
* @param y - The y-coordinate of the top-left corner where the sprite will be drawn
|
|
313
|
+
* @param width - The width to scale the sprite to
|
|
314
|
+
* @param height - The height to scale the sprite to
|
|
315
|
+
*
|
|
316
|
+
* @example
|
|
317
|
+
* // Draw a sprite named "player" at position (100, 100) with size 50x50
|
|
318
|
+
* shapeDrawer.drawSprite("player", 100, 100, 50, 50);
|
|
319
|
+
*
|
|
320
|
+
* @example
|
|
321
|
+
* // Draw a sprite with different dimensions
|
|
322
|
+
* shapeDrawer.drawSprite("enemy", 200, 200, 30, 40);
|
|
323
|
+
*/
|
|
324
|
+
drawSprite(spriteName, x, y, width, height) {
|
|
325
|
+
const sprite = this.spriteLibrary.getSprite(spriteName);
|
|
326
|
+
if (sprite) {
|
|
327
|
+
this.context.drawImage(sprite, x, y, width, height);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Loads a font (local URL or Google Fonts stylesheet) and registers it for use.
|
|
332
|
+
*
|
|
333
|
+
* This method handles both local font files and Google Fonts URLs. For Google Fonts,
|
|
334
|
+
* it injects the stylesheet and waits for the font to be available. For local fonts,
|
|
335
|
+
* it creates a new FontFace object and adds it to the document's font collection.
|
|
336
|
+
*
|
|
337
|
+
* @param fontName - The name of the font to load (must match the font family name)
|
|
338
|
+
* @param url - The URL of the font file or Google Fonts stylesheet
|
|
339
|
+
* @returns A Promise that resolves when the font is successfully loaded and available
|
|
340
|
+
*
|
|
341
|
+
* @example
|
|
342
|
+
* // Load a Google Font
|
|
343
|
+
* await shapeDrawer.loadFont("Roboto", "https://fonts.googleapis.com/css2?family=Roboto");
|
|
344
|
+
*
|
|
345
|
+
* @example
|
|
346
|
+
* // Load a local font file
|
|
347
|
+
* await shapeDrawer.loadFont("CustomFont", "/fonts/custom-font.woff2");
|
|
348
|
+
*
|
|
349
|
+
* @throws Will throw an error if the font fails to load
|
|
350
|
+
*/
|
|
351
|
+
async loadFont(fontName, url) {
|
|
352
|
+
// Google Fonts endpoints return CSS, so inject the stylesheet first and wait for the font to be available
|
|
353
|
+
if (url.includes("fonts.googleapis.com")) {
|
|
354
|
+
await new Promise((resolve, reject) => {
|
|
355
|
+
const link = document.createElement("link");
|
|
356
|
+
link.rel = "stylesheet";
|
|
357
|
+
link.href = url;
|
|
358
|
+
link.onload = () => {
|
|
359
|
+
document.fonts.load(`1em ${fontName}`).then(() => resolve());
|
|
360
|
+
};
|
|
361
|
+
link.onerror = () => reject(new Error(`Failed to load font stylesheet: ${fontName}`));
|
|
362
|
+
document.head.appendChild(link);
|
|
363
|
+
});
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
const newFont = new FontFace(fontName, `url(${url})`);
|
|
367
|
+
const loadedFont = await newFont.load();
|
|
368
|
+
document.fonts.add(loadedFont);
|
|
369
|
+
await document.fonts.load(`1em ${fontName}`);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
class CanvasController {
|
|
373
|
+
canvas;
|
|
374
|
+
context;
|
|
375
|
+
spriteLibrary = new SpriteLibrary();
|
|
376
|
+
shapeDrawer;
|
|
377
|
+
constructor(width, height) {
|
|
378
|
+
this.canvas = document.createElement("canvas");
|
|
379
|
+
this.canvas.width = width;
|
|
380
|
+
this.canvas.height = height;
|
|
381
|
+
this.context = this.canvas.getContext("2d");
|
|
382
|
+
this.shapeDrawer = new ShapeDrawer(this.context, this.spriteLibrary);
|
|
383
|
+
const canvasContainer = document.getElementById("canvas-container");
|
|
384
|
+
if (canvasContainer) {
|
|
385
|
+
canvasContainer.appendChild(this.canvas);
|
|
386
|
+
}
|
|
387
|
+
else {
|
|
388
|
+
document.body.appendChild(this.canvas);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
clearCanvas() {
|
|
392
|
+
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
393
|
+
}
|
|
394
|
+
getCanvas() {
|
|
395
|
+
return this.canvas;
|
|
396
|
+
}
|
|
397
|
+
getContext() {
|
|
398
|
+
return this.context;
|
|
399
|
+
}
|
|
400
|
+
getShapeDrawer() {
|
|
401
|
+
return this.shapeDrawer;
|
|
402
|
+
}
|
|
403
|
+
getSpriteLibrary() {
|
|
404
|
+
return this.spriteLibrary;
|
|
405
|
+
}
|
|
406
|
+
applyTranslation(translationVector) {
|
|
407
|
+
this.context.translate(translationVector.x, translationVector.y);
|
|
408
|
+
}
|
|
409
|
+
reset() {
|
|
410
|
+
this.context.setTransform(1, 0, 0, 1, 0, 0);
|
|
411
|
+
}
|
|
412
|
+
applyRotation(angle) {
|
|
413
|
+
this.context.rotate(angle);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
export * from "./Transformations";
|
|
417
|
+
export { CanvasController, ShapeDrawer, SpriteLibrary };
|
|
418
|
+
//# sourceMappingURL=index.js.map
|