Skip to content

Commit eda4961

Browse files
committed
Math.getNextPowerOfTwo will get the next power of two for the given value.
Math.isPowerOfTwo will return a boolean if the given width and height are a power of two.
1 parent bffdb8f commit eda4961

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,10 @@ You can read all about the philosophy behind Lazer [here](http://phaser.io/news/
338338
* PIXI.TilingSprite has been removed, and all functionality merged in to Phaser.TileSprite, to cut down on the number of internal classes and inheritance going on.
339339
* PIXI.CanvasPool has been moved into the Phaser `utils` folder, and renamed to `Phaser.CanvasPool`. All references to PIXI.CanvasPool have been updated to match the new namespace.
340340
* PIXI.EarCut has been moved into the Phaser `utils` folder, and renamed to `Phaser.EarCut`. All references to PIXI.EarCut have been updated to match the new namespace.
341+
* Device.canHandleAlpha is a new boolean property that stores is the browser is capable of tinting with alpha.
342+
* Device.canUseMultiply is a new boolean property that stores whether or not the Canvas BlendModes are supported, consequently the ability to tint using the multiply method.
343+
* Math.getNextPowerOfTwo will get the next power of two for the given value.
344+
* Math.isPowerOfTwo will return a boolean if the given width and height are a power of two.
341345

342346
### Bug Fixes
343347

@@ -360,6 +364,7 @@ Please note that Phaser uses a custom build of Pixi and always has done. The fol
360364
* PIXI.RenderTexture has been removed as it's no longer used internally.
361365
* PIXI.TileSprite has been removed as it's no longer used internally.
362366
* PIXI.EarCut has been removed as it's no longer used internally.
367+
* PIXI.Utils has been removed. All functionality is now available in Phaser.
363368

364369
For changes in previous releases please see the extensive [Version History](https://github.com/photonstorm/phaser/blob/master/CHANGELOG.md).
365370

src/math/Math.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,49 @@ Phaser.Math = {
2323
*/
2424
PI2: Math.PI * 2,
2525

26+
/**
27+
* Given a number, this function returns the closest number that is a power of two.
28+
* This function is from the Starling Framework.
29+
*
30+
* @method Phaser.Math#getNextPowerOfTwo
31+
* @param {number} value - The value to get the closest power of two from.
32+
* @return {number} The closest number that is a power of two.
33+
*/
34+
getNextPowerOfTwo: function (value) {
35+
36+
if (value > 0 && (value & (value - 1)) === 0)
37+
{
38+
// http://goo.gl/D9kPj
39+
return value;
40+
}
41+
else
42+
{
43+
var result = 1;
44+
45+
while (result < value)
46+
{
47+
result <<= 1;
48+
}
49+
50+
return result;
51+
}
52+
53+
},
54+
55+
/**
56+
* Checks if the given dimensions make a power of two texture.
57+
*
58+
* @method Phaser.Math#isPowerOfTwo
59+
* @param {number} width - The width to check.
60+
* @param {number} height - The height to check.
61+
* @return {boolean} True if the width and height are a power of two.
62+
*/
63+
isPowerOfTwo: function (width, height) {
64+
65+
return (width > 0 && (width & (width - 1)) === 0 && height > 0 && (height & (height - 1)) === 0);
66+
67+
},
68+
2669
/**
2770
* Returns a random float in the range `[min, max)`. If these parameters are not in order than they will be put in order.
2871
* Default is 0 for `min` and 1 for `max`.

typescript/phaser.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2590,8 +2590,10 @@ declare module Phaser {
25902590
static fuzzyGreaterThan(a: number, b: number, epsilon?: number): boolean;
25912591
static fuzzyLessThan(a: number, b: number, epsilon?: number): boolean;
25922592
static getShortestAngle(angle1: number, angle2: number): number;
2593+
static getNextPowerOfTwo(value: number): number;
25932594
static isEven(n: number): boolean;
25942595
static isOdd(n: number): boolean;
2596+
static isPowerOfTwo(width: number, height: number): boolean;
25952597
static linear(p0: number, p1: number, t: number): number;
25962598
static linearInterpolation(v: number[], k: number): number;
25972599
static mapLinear(x: number, a1: number, a2: number, b1: number, b2: number): number;

0 commit comments

Comments
 (0)