11// import * as Phaser from 'phaser';
22import CanvasGameObjectBase from '../../../utils/types/CanvasGameObjectBase' ;
33
4+ /**
5+ * Canvas-backed game object with texture update helpers.
6+ */
47export default class Canvas extends CanvasGameObjectBase {
8+ /**
9+ * Create a canvas game object.
10+ * @param scene - The Scene to which this object belongs.
11+ * @param x - The x position.
12+ * @param y - The y position.
13+ * @param width - The canvas width.
14+ * @param height - The canvas height.
15+ * @param resolution - Canvas resolution.
16+ */
517 constructor (
618 scene : Phaser . Scene ,
719 x ?: number , y ?: number ,
820 width ?: number , height ?: number ,
921 resolution ?: number ,
1022 ) ;
1123
24+ /**
25+ * Current resolution value.
26+ */
1227 readonly resolution : number ;
28+ /**
29+ * Set resolution.
30+ * @param resolution - Resolution value.
31+ * @returns This instance.
32+ */
1333 setResolution ( resolution : number ) : this;
1434
35+ /**
36+ * Canvas element.
37+ */
1538 canvas : HTMLCanvasElement ;
39+ /**
40+ * Canvas 2D context.
41+ */
1642 context : CanvasRenderingContext2D ;
43+ /**
44+ * Get the canvas element.
45+ * @param readOnly - True to return without marking dirty.
46+ * @returns The canvas element.
47+ */
1748 getCanvas ( readOnly ?: boolean ) : HTMLCanvasElement ;
49+ /**
50+ * Get the canvas 2D context.
51+ * @param readOnly - True to return without marking dirty.
52+ * @returns The 2D context.
53+ */
1854 getContext ( readOnly ?: boolean ) : CanvasRenderingContext2D ;
1955
56+ /**
57+ * Set size of the game object.
58+ * @param width - Width value.
59+ * @param height - Height value.
60+ * @returns This instance.
61+ */
2062 setSize ( width : number , height : number ) : this;
63+ /**
64+ * Resize the canvas.
65+ * @param width - Width value.
66+ * @param height - Height value.
67+ * @returns This instance.
68+ */
2169 resize ( width : number , height : number ) : this;
70+ /**
71+ * Set the canvas size.
72+ * @param width - Width value.
73+ * @param height - Height value.
74+ * @returns This instance.
75+ */
2276 setCanvasSize ( width : number , height : number ) : this;
2377
78+ /**
79+ * Mark the canvas as needing redraw.
80+ * @returns This instance.
81+ */
2482 needRedraw ( ) : this;
83+ /**
84+ * True if the canvas is dirty.
85+ */
2586 dirty : boolean ;
2687
88+ /**
89+ * Clear the canvas.
90+ * @returns This instance.
91+ */
2792 clear ( ) : this;
93+ /**
94+ * Fill the canvas with a color.
95+ * @param color - Fill color.
96+ * @returns This instance.
97+ */
2898 fill ( color : string ) : this;
2999
100+ /**
101+ * Update the texture from the canvas.
102+ * @param callback - Optional draw callback.
103+ * @param scope - Callback scope.
104+ * @returns This instance.
105+ */
30106 updateTexture (
31107 callback ?: ( canvasElem : HTMLCanvasElement , context : CanvasRenderingContext2D ) => void ,
32108 scope ?: object
33109 ) : this;
34110
111+ /**
112+ * Generate a texture from the canvas.
113+ * @param key - Texture key.
114+ * @param x - Source x.
115+ * @param y - Source y.
116+ * @param width - Source width.
117+ * @param height - Source height.
118+ * @returns This instance.
119+ */
35120 generateTexture (
36121 key : string | number ,
37122 x ?: number , y ?: number ,
38123 width ?: number , height ?: number
39124 ) : this;
40125
126+ /**
127+ * Load a texture into the canvas.
128+ * @param key - Texture key.
129+ * @param frame - Frame name.
130+ * @returns This instance.
131+ */
41132 loadTexture (
42133 key : string ,
43134 frame ?: string ,
44135 ) : this;
45136
137+ /**
138+ * Draw a frame to the canvas.
139+ * @param key - Texture key.
140+ * @param frame - Frame name.
141+ * @param dx - Destination x.
142+ * @param dy - Destination y.
143+ * @param dWidth - Destination width.
144+ * @param dHeight - Destination height.
145+ * @param sxOffset - Source x offset.
146+ * @param syOffset - Source y offset.
147+ * @param sWidth - Source width.
148+ * @param sHeight - Source height.
149+ * @returns This instance.
150+ */
46151 drawFrame (
47152 key : string ,
48153 frame ?: string ,
@@ -56,15 +161,37 @@ export default class Canvas extends CanvasGameObjectBase {
56161 sHeight ?: number ,
57162 ) : this;
58163
164+ /**
165+ * Get canvas data URL.
166+ * @param type - Mime type.
167+ * @param encoderOptions - Encoder options.
168+ * @returns Data URL string.
169+ */
59170 getDataURL (
60171 type ?: string ,
61172 encoderOptions ?: number
62173 ) : string ;
63174
175+ /**
176+ * Get pixel color at a position.
177+ * @param x - X position.
178+ * @param y - Y position.
179+ * @returns The color.
180+ */
64181 getPixel (
65182 x : number , y : number
66183 ) : Phaser . Display . Color ;
67184
185+ /**
186+ * Set pixel color at a position.
187+ * @param x - X position.
188+ * @param y - Y position.
189+ * @param r - Red value or color object.
190+ * @param g - Green value.
191+ * @param b - Blue value.
192+ * @param a - Alpha value.
193+ * @returns This instance.
194+ */
68195 setPixel (
69196 x : number , y : number ,
70197 r : number | Phaser . Display . Color ,
@@ -73,4 +200,4 @@ export default class Canvas extends CanvasGameObjectBase {
73200 a ?: number
74201 ) : this;
75202
76- }
203+ }
0 commit comments