Skip to content

Commit d027bf8

Browse files
committed
BitmapData.generateTexture will take a snapshot of the BitmapDatas canvas at that moment in time and convert it into an Image, which is then stored in the Phaser image Cache based on the key given. You can then use the new texture for any future sprites or texture based objects.
1 parent 7253ac3 commit d027bf8

4 files changed

Lines changed: 42 additions & 57 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ Version 2.4 - "Katar" - in dev
329329
* BitmapData.moveV(distance) allows you to vertically shift the BitmapData with wrap-around the edges.
330330
* Text.addStrokeColor works in the same way as `Text.addColor` but allows you to define a color stop for the stroke color instead of the fill color.
331331
* All Game Objects have a new boolean property called `pendingDestroy`. If you set this to `true` then the object will automatically destroy itself in the *next* logic update, rather than immediately. This is useful for cases when you wish to destroy an object from within one of its own callbacks, such as with buttons or other input events (thanks @alamboley #1748)
332+
* BitmapData.generateTexture will take a snapshot of the BitmapDatas canvas at that moment in time and convert it into an Image, which is then stored in the Phaser image Cache based on the key given. You can then use the new texture for any future sprites or texture based objects.
332333

333334
### Updates
334335

src/gameobjects/BitmapData.js

Lines changed: 38 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,6 @@ Phaser.BitmapData = function (game, key, width, height) {
215215
*/
216216
this._swapCanvas = Phaser.Canvas.create(width, height, '', true);
217217

218-
// http://androidarts.com/palette/16pal.htm
219-
220-
this.palettes = {
221-
'arne': { 0: '#000000', 1: '#9D9D9D', 2: '#FFFFFF', 3: '#BE2633', 4: '#E06F8B', 5: '#493C2B', 6: '#A46422', 7: '#EB8931', 8: '#F7E26B', 9: '#2F484E', A: '#44891A', B: '#A3CE27', C: '#1B2632', D: '#005784', E: '#31A2F2', F: '#B2DCEF' },
222-
'jmp': { 0: '#000000', 1: '#191028', 2: '#46af45', 3: '#a1d685', 4: '#453e78', 5: '#7664fe', 6: '#833129', 7: '#9ec2e8', 8: '#dc534b', 9: '#e18d79', A: '#d6b97b', B: '#e9d8a1', C: '#216c4b', D: '#d365c8', E: '#afaab9', F: '#f5f4eb' },
223-
'cga': { 0: '#000000', 1: '#2234d1', 2: '#0c7e45', 3: '#44aacc', 4: '#8a3622', 5: '#5c2e78', 6: '#aa5c3d', 7: '#b5b5b5', 8: '#5e606e', 9: '#4c81fb', A: '#6cd947', B: '#7be2f9', C: '#eb8a60', D: '#e23d69', E: '#ffd93f', F: '#fffff' }
224-
};
225-
226218
};
227219

228220
Phaser.BitmapData.prototype = {
@@ -351,53 +343,9 @@ Phaser.BitmapData.prototype = {
351343

352344
},
353345

354-
grid: function (w, h, color) {
355-
356-
this.context.fillStyle = color;
357-
358-
for (var y = 0; y < this.height; y += h)
359-
{
360-
this.context.fillRect(0, y, this.width, 1);
361-
}
362-
363-
for (var x = 0; x < this.width; x += w)
364-
{
365-
this.context.fillRect(x, 0, 1, this.height);
366-
}
367-
368-
},
369-
370-
generate: function (data, palette, size) {
371-
372-
if (typeof palette === 'undefined') { palette = 'arne'; }
373-
if (typeof size === 'undefined') { size = 8; }
374-
375-
var w = data[0].length * size;
376-
var h = data.length * size;
377-
378-
this.resize(w, h);
379-
380-
// Draw it
381-
for (var y = 0; y < data.length; y++)
382-
{
383-
var row = data[y];
384-
385-
for (var x = 0; x < row.length; x++)
386-
{
387-
var d = row[x];
388-
389-
if (d !== '.' && d !== ' ')
390-
{
391-
this.context.fillStyle = this.palettes[palette][d];
392-
this.context.fillRect(x * size, y * size, size, size);
393-
}
394-
}
395-
}
396-
397-
},
398-
399346
/**
400-
* Updates the given objects so that they use this BitmapData as their texture. This will replace any texture they will currently have set.
347+
* Updates the given objects so that they use this BitmapData as their texture.
348+
* This will replace any texture they will currently have set.
401349
*
402350
* @method Phaser.BitmapData#add
403351
* @param {Phaser.Sprite|Phaser.Sprite[]|Phaser.Image|Phaser.Image[]} object - Either a single Sprite/Image or an Array of Sprites/Images.
@@ -503,6 +451,42 @@ Phaser.BitmapData.prototype = {
503451

504452
},
505453

454+
/**
455+
* Creates a new Image element by converting this BitmapDatas canvas into a dataURL.
456+
*
457+
* The image is then stored in the Cache using the key given.
458+
*
459+
* Finally a PIXI.Texture is created based on the image and returned.
460+
*
461+
* You can apply the texture to a sprite or any other supporting object by using either the
462+
* key or the texture. First call generateTexture:
463+
*
464+
* `var texture = bitmapdata.generateTexture('ball');`
465+
*
466+
* Then you can either apply the texture to a sprite:
467+
*
468+
* `game.add.sprite(0, 0, texture);`
469+
*
470+
* or by using the string based key:
471+
*
472+
* `game.add.sprite(0, 0, 'ball');`
473+
*
474+
* @method Phaser.BitmapData#generateTexture
475+
* @param {string} key - The key which will be used to store the image in the Cache.
476+
* @return {PIXI.Texture} The newly generated texture.
477+
*/
478+
generateTexture: function (key) {
479+
480+
var image = new Image();
481+
482+
image.src = this.canvas.toDataURL("image/png");
483+
484+
this.game.cache.addImage(key, '', image);
485+
486+
return new PIXI.Texture(PIXI.BaseTextureCache[key]);
487+
488+
},
489+
506490
/**
507491
* Resizes the BitmapData. This changes the size of the underlying canvas and refreshes the buffer.
508492
*

src/loader/Cache.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,12 +535,11 @@ Phaser.Cache.prototype = {
535535

536536
this._images[key] = { url: url, data: data };
537537

538-
this._images[key].frame = new Phaser.Frame(0, 0, 0, data.width, data.height, key, this.game.rnd.uuid());
538+
this._images[key].frame = new Phaser.Frame(0, 0, 0, data.width, data.height, key);
539539
this._images[key].frameData = new Phaser.FrameData();
540-
this._images[key].frameData.addFrame(new Phaser.Frame(0, 0, 0, data.width, data.height, url, this.game.rnd.uuid()));
540+
this._images[key].frameData.addFrame(new Phaser.Frame(0, 0, 0, data.width, data.height, url));
541541

542542
PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data);
543-
// PIXI.TextureCache[key] = new PIXI.Texture(PIXI.BaseTextureCache[key]);
544543

545544
this._resolveURL(url, this._images[key]);
546545

typescript/phaser.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ declare module Phaser {
253253
drawGroup(group: Phaser.Group, blendMode?: number, roundPx?: boolean): Phaser.BitmapData;
254254
extract(destination: Phaser.BitmapData, r: number, g: number, b: number, a?: number, resize?: boolean, r2?: number, g2?: number, b2?: number): Phaser.BitmapData;
255255
fill(r: number, g: number, b: number, a?: number): Phaser.BitmapData;
256+
generateTexture(key: string): PIXI.Texture;
256257
getBounds(rect?: Phaser.Rectangle): Phaser.Rectangle;
257258
getFirstPixel(direction: number): { r: number; g: number; b: number; x: number; y: number; };
258259
getPixel(x: number, y: number, out?: any): number;

0 commit comments

Comments
 (0)