Skip to content

Commit 86cac20

Browse files
committed
BitmapData.move(x, y) allows you to shift the contents of the BitmapData horizontally and vertically by the given amounts. The image wraps-around the edges of the BitmapData.
BitmapData.moveH(distance) allows you to horizontally shift the BitmapData with wrap-around the edges. BitmapData.moveV(distance) allows you to vertically shift the BitmapData with wrap-around the edges.
1 parent a7e1813 commit 86cac20

3 files changed

Lines changed: 193 additions & 1 deletion

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ Version 2.4 - "Katar" - in dev
324324
* TilemapLayer.resize allows you to resize a TilemapLayer. It will update the internal canvas object and corresponding texture dimensions (#1881)
325325
* Pointer button handling has been given an overhaul. It has the following new boolean properties: `leftButton`, `rightButton`, `middleButton`, `backButton`, `forwardButton` and `eraserButton`. So you can now easily check which buttons are active and build right or middle click support into your games. The Pointer object normalises these properties for you, regardless if they came from a MouseEvent or PointerEvent (thanks @youssefdetovernickr for the idea #1848)
326326
* Text has a new style property: tabs. This allows you to specify a pixel value (or values) that allows you to space out text that contains tab characters within it. `Text.tabs` can be either an integer, in which case all tabs share the same spacing, or an array of pixel values corresponding exactly to the number of tabs per line of text. This allows you to easily align columns of data in a single Text object.
327+
* BitmapData.move(x, y) allows you to shift the contents of the BitmapData horizontally and vertically by the given amounts. The image wraps-around the edges of the BitmapData.
328+
* BitmapData.moveH(distance) allows you to horizontally shift the BitmapData with wrap-around the edges.
329+
* BitmapData.moveV(distance) allows you to vertically shift the BitmapData with wrap-around the edges.
327330

328331
### Updates
329332

src/gameobjects/BitmapData.js

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,193 @@ Phaser.BitmapData = function (game, key, width, height) {
209209
*/
210210
this._circle = new Phaser.Circle();
211211

212+
/**
213+
* @property {HTMLCanvasElement} _swapCanvas - A swap canvas.
214+
* @private
215+
*/
216+
this._swapCanvas = Phaser.Canvas.create(width, height, '', true);
217+
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+
212226
};
213227

214228
Phaser.BitmapData.prototype = {
215229

230+
/**
231+
* Shifts the contents of this BitmapData by the distances given.
232+
*
233+
* The image will wrap-around the edges on all sides.
234+
*
235+
* @method Phaser.BitmapData#move
236+
* @param {integer} x - The amount of pixels to horizontally shift the canvas by. Use a negative value to shift to the left, positive to the right.
237+
* @param {integer} y - The amount of pixels to vertically shift the canvas by. Use a negative value to shift up, positive to shift down.
238+
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
239+
*/
240+
move: function (x, y) {
241+
242+
if (x !== 0)
243+
{
244+
this.moveH(x);
245+
}
246+
247+
if (y !== 0)
248+
{
249+
this.moveV(y);
250+
}
251+
252+
return this;
253+
254+
},
255+
256+
/**
257+
* Shifts the contents of this BitmapData horizontally.
258+
*
259+
* The image will wrap-around the sides.
260+
*
261+
* @method Phaser.BitmapData#moveH
262+
* @param {integer} distance - The amount of pixels to horizontally shift the canvas by. Use a negative value to shift to the left, positive to the right.
263+
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
264+
*/
265+
moveH: function (distance) {
266+
267+
var c = this._swapCanvas;
268+
var ctx = c.getContext('2d');
269+
var h = this.height;
270+
var src = this.canvas;
271+
272+
ctx.clearRect(0, 0, this.width, this.height);
273+
274+
if (distance < 0)
275+
{
276+
distance = Math.abs(distance);
277+
278+
// Moving to the left
279+
var w = this.width - distance;
280+
281+
// Left-hand chunk
282+
ctx.drawImage(src, 0, 0, distance, h, w, 0, distance, h);
283+
284+
// Rest of the image
285+
ctx.drawImage(src, distance, 0, w, h, 0, 0, w, h);
286+
}
287+
else
288+
{
289+
// Moving to the right
290+
var w = this.width - distance;
291+
292+
// Right-hand chunk
293+
ctx.drawImage(src, w, 0, distance, h, 0, 0, distance, h);
294+
295+
// Rest of the image
296+
ctx.drawImage(src, 0, 0, w, h, distance, 0, w, h);
297+
}
298+
299+
this.clear();
300+
301+
return this.copy(this._swapCanvas);
302+
303+
},
304+
305+
/**
306+
* Shifts the contents of this BitmapData vertically.
307+
*
308+
* The image will wrap-around the sides.
309+
*
310+
* @method Phaser.BitmapData#moveV
311+
* @param {integer} distance - The amount of pixels to vertically shift the canvas by. Use a negative value to shift up, positive to shift down.
312+
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
313+
*/
314+
moveV: function (distance) {
315+
316+
var c = this._swapCanvas;
317+
var ctx = c.getContext('2d');
318+
var w = this.width;
319+
var src = this.canvas;
320+
321+
ctx.clearRect(0, 0, this.width, this.height);
322+
323+
if (distance < 0)
324+
{
325+
distance = Math.abs(distance);
326+
327+
// Moving up
328+
var h = this.height - distance;
329+
330+
// Top chunk
331+
ctx.drawImage(src, 0, 0, w, distance, 0, h, w, distance);
332+
333+
// Rest of the image
334+
ctx.drawImage(src, 0, distance, w, h, 0, 0, w, h);
335+
}
336+
else
337+
{
338+
// Moving down
339+
var h = this.height - distance;
340+
341+
// Bottom chunk
342+
ctx.drawImage(src, 0, h, w, distance, 0, 0, w, distance);
343+
344+
// Rest of the image
345+
ctx.drawImage(src, 0, 0, w, h, 0, distance, w, h);
346+
}
347+
348+
this.clear();
349+
350+
return this.copy(this._swapCanvas);
351+
352+
},
353+
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+
216399
/**
217400
* Updates the given objects so that they use this BitmapData as their texture. This will replace any texture they will currently have set.
218401
*
@@ -336,6 +519,9 @@ Phaser.BitmapData.prototype = {
336519
this.canvas.width = width;
337520
this.canvas.height = height;
338521

522+
this._swapCanvas.width = width;
523+
this._swapCanvas.height = height;
524+
339525
this.baseTexture.width = width;
340526
this.baseTexture.height = height;
341527

typescript/phaser.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// <reference path="pixi.d.ts" />
22
/// <reference path="p2.d.ts" />
33

4-
// Type definitions for Phaser 2.4.0 2015-Jul-07
4+
// Type definitions for Phaser 2.4.0 2015-Jul-08
55
// Project: https://github.com/photonstorm/phaser
66

77
declare class Phaser {
@@ -261,6 +261,9 @@ declare module Phaser {
261261
getPixels(rect: Phaser.Rectangle): ImageData;
262262
getTransform(translateX: number, translateY: number, scaleX: number, scaleY: number, skewX: number, skewY: number): any;
263263
load(source: any): Phaser.BitmapData;
264+
move(x: number, y: number): Phaser.BitmapData;
265+
moveH(distance: number): Phaser.BitmapData;
266+
moveV(distance: number): Phaser.BitmapData;
264267
processPixel(callback: Function, callbackContext: any, x?: number, y?: Number, width?: number, height?: number): Phaser.BitmapData;
265268
processPixelRGB(callback: Function, callbackContext: any, x?: number, y?: Number, width?: number, height?: number): Phaser.BitmapData;
266269
rect(x: number, y: number, width: number, height: number, fillStyle?: string): Phaser.BitmapData;

0 commit comments

Comments
 (0)