Skip to content

Commit c3524b3

Browse files
committed
Added CanvasTexture.drawFrame method.
1 parent 8e495da commit c3524b3

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ one set of bindings ever created, which makes things a lot cleaner.
154154
* The Animation class now emits the `restart` event when it restarts playing on any Game Object.
155155
* The Animation class now emits the `complete` event when it finishes playing on any Game Object.
156156
* The Animation Component has a new method called `chain` which allows you to line-up another animation to start playing as soon as the current one stops, no matter how it stops (either by reaching its natural end, or directly by having stop called on it). You can chain a new animation at any point, including before the current one starts playing, during it, or when it ends (via its `animationcomplete` callback). Chained animations are specific to a Game Object, meaning different Game Objects can have different chained animations without impacting the global animation they're playing.
157+
* `CanvasTexture.drawFrame` is a new method that allows you to draw a texture frame to the CanvasTexture based on the texture key and frame given.
157158

158159
### Updates
159160

src/textures/CanvasTexture.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,45 @@ var CanvasTexture = new Class({
223223
return this.update();
224224
},
225225

226+
/**
227+
* Draws the given texture frame to this CanvasTexture, then updates the internal
228+
* ImageData buffer and arrays.
229+
*
230+
* @method Phaser.Textures.CanvasTexture#drawFrame
231+
* @since 3.16.0
232+
*
233+
* @param {string} key - The unique string-based key of the Texture.
234+
* @param {(string|integer)} [frame] - The string-based name, or integer based index, of the Frame to get from the Texture.
235+
* @param {integer} [x=0] - The x coordinate to draw the source at.
236+
* @param {integer} [y=0] - The y coordinate to draw the source at.
237+
*
238+
* @return {Phaser.Textures.CanvasTexture} This CanvasTexture.
239+
*/
240+
drawFrame: function (key, frame, x, y)
241+
{
242+
if (x === undefined) { x = 0; }
243+
if (y === undefined) { y = 0; }
244+
245+
var frame = this.manager.getFrame(key, frame);
246+
247+
if (frame)
248+
{
249+
var cd = frame.canvasData;
250+
251+
var frameWidth = frame.cutWidth;
252+
var frameHeight = frame.cutHeight;
253+
var res = frame.source.resolution;
254+
255+
this.context.drawImage(frame.source.image, cd.x, cd.y, frameWidth, frameHeight, x, y, frameWidth / res, frameHeight / res);
256+
257+
return this.update();
258+
}
259+
else
260+
{
261+
return this;
262+
}
263+
},
264+
226265
/**
227266
* Get the color of a specific pixel from this texture and store it in a Color object.
228267
*

0 commit comments

Comments
 (0)