Skip to content

Commit f9776f3

Browse files
committed
Added the CanvasUtils class and moved some stuff out of Stage into it.
1 parent 8b2f1cc commit f9776f3

7 files changed

Lines changed: 244 additions & 206 deletions

File tree

Phaser/Phaser.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
<IISExpressUseClassicPipelineMode />
1717
</PropertyGroup>
1818
<ItemGroup>
19+
<TypeScriptCompile Include="utils\CanvasUtils.ts" />
20+
<Content Include="utils\CanvasUtils.js">
21+
<DependentUpon>CanvasUtils.ts</DependentUpon>
22+
</Content>
1923
<Content Include="_definitions.ts" />
2024
<TypeScriptCompile Include="Phaser.ts" />
2125
<TypeScriptCompile Include="particles\Emitter.ts" />

Phaser/Stage.ts

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,13 @@ module Phaser {
3232
this.canvas = <HTMLCanvasElement> document.createElement('canvas');
3333
this.canvas.width = width;
3434
this.canvas.height = height;
35+
this.context = this.canvas.getContext('2d');
3536

36-
if ((parent !== '' || parent !== null) && document.getElementById(parent))
37-
{
38-
document.getElementById(parent).appendChild(this.canvas);
39-
document.getElementById(parent).style.overflow = 'hidden';
40-
}
41-
else
42-
{
43-
document.body.appendChild(this.canvas);
44-
}
37+
Phaser.CanvasUtils.addToDOM(this.canvas, parent, true);
38+
Phaser.CanvasUtils.setTouchAction(this.canvas);
4539

46-
// Consume default actions on the canvas
47-
this.canvas.style.msTouchAction = 'none';
48-
this.canvas.style['ms-touch-action'] = 'none';
49-
this.canvas.style['touch-action'] = 'none';
50-
this.canvas.style.backgroundColor = 'rgb(0,0,0)';
5140
this.canvas.oncontextmenu = function(event) { event.preventDefault(); };
5241

53-
this.context = this.canvas.getContext('2d');
54-
5542
this.css3 = new Phaser.Display.CSS3Filters(this.canvas);
5643

5744
this.scaleMode = StageScaleMode.NO_SCALE;
@@ -280,13 +267,6 @@ module Phaser {
280267

281268
}
282269

283-
public setImageRenderingCrisp() {
284-
this.canvas.style['image-rendering'] = 'crisp-edges';
285-
this.canvas.style['image-rendering'] = '-moz-crisp-edges';
286-
this.canvas.style['image-rendering'] = '-webkit-optimize-contrast';
287-
this.canvas.style['-ms-interpolation-mode'] = 'nearest-neighbor';
288-
}
289-
290270
public pauseGame() {
291271

292272
this.game.paused = true;

Phaser/_definitions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393

9494
/// <reference path="ui/Button.ts" />
9595

96+
/// <reference path="utils/CanvasUtils.ts" />
9697
/// <reference path="utils/CircleUtils.ts" />
9798
/// <reference path="utils/ColorUtils.ts" />
9899
/// <reference path="utils/PointUtils.ts" />

Phaser/utils/CanvasUtils.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var Shapes;
2+
(function (Shapes) {
3+
4+
var Point = Shapes.Point = (function () {
5+
function Point(x, y) {
6+
this.x = x;
7+
this.y = y;
8+
}
9+
Point.prototype.getDist = function () {
10+
return Math.sqrt((this.x * this.x) + (this.y * this.y));
11+
};
12+
Point.origin = new Point(0, 0);
13+
return Point;
14+
})();
15+
16+
})(Shapes || (Shapes = {}));
17+
18+
var p = new Shapes.Point(3, 4);
19+
var dist = p.getDist();

Phaser/utils/CanvasUtils.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/// <reference path="../_definitions.ts" />
2+
3+
/**
4+
* Phaser - CanvasUtils
5+
*
6+
* A collection of methods useful for manipulating canvas objects.
7+
*/
8+
9+
module Phaser {
10+
11+
export class CanvasUtils {
12+
13+
public static getAspectRatio(canvas: HTMLCanvasElement): number {
14+
return canvas.width / canvas.height;
15+
}
16+
17+
public static setBackgroundColor(canvas: HTMLCanvasElement, color: string = 'rgb(0,0,0)'): HTMLCanvasElement {
18+
19+
canvas.style.backgroundColor = color;
20+
return canvas;
21+
22+
}
23+
24+
public static setTouchAction(canvas: HTMLCanvasElement, value: string= 'none'): HTMLCanvasElement {
25+
26+
canvas.style.msTouchAction = value;
27+
canvas.style['ms-touch-action'] = value;
28+
canvas.style['touch-action'] = value;
29+
30+
return canvas;
31+
32+
}
33+
34+
public static addToDOM(canvas: HTMLCanvasElement, parent: string = '', overflowHidden: boolean = true): HTMLCanvasElement {
35+
36+
if ((parent !== '' || parent !== null) && document.getElementById(parent))
37+
{
38+
document.getElementById(parent).appendChild(canvas);
39+
40+
if (overflowHidden)
41+
{
42+
document.getElementById(parent).style.overflow = 'hidden';
43+
}
44+
}
45+
else
46+
{
47+
document.body.appendChild(canvas);
48+
}
49+
50+
return canvas;
51+
52+
}
53+
54+
public static setTransform(context: CanvasRenderingContext2D, translateX: number, translateY: number, scaleX: number, scaleY: number, skewX: number, skewY: number): CanvasRenderingContext2D {
55+
56+
context.setTransform(scaleX, skewX, skewY, scaleY, translateX, translateY);
57+
58+
return context;
59+
60+
}
61+
62+
public static setSmoothingEnabled(context: CanvasRenderingContext2D, value: boolean): CanvasRenderingContext2D {
63+
64+
context['imageSmoothingEnabled'] = value;
65+
context['mozImageSmoothingEnabled'] = value;
66+
context['oImageSmoothingEnabled'] = value;
67+
context['webkitImageSmoothingEnabled'] = value;
68+
context['msImageSmoothingEnabled'] = value;
69+
return context;
70+
71+
}
72+
73+
public static setImageRenderingCrisp(canvas: HTMLCanvasElement): HTMLCanvasElement {
74+
75+
canvas.style['image-rendering'] = 'crisp-edges';
76+
canvas.style['image-rendering'] = '-moz-crisp-edges';
77+
canvas.style['image-rendering'] = '-webkit-optimize-contrast';
78+
canvas.style.msInterpolationMode = 'nearest-neighbor';
79+
return canvas;
80+
81+
}
82+
83+
public static setImageRenderingBicubic(canvas: HTMLCanvasElement): HTMLCanvasElement {
84+
85+
canvas.style['image-rendering'] = 'auto';
86+
canvas.style.msInterpolationMode = 'bicubic';
87+
return canvas;
88+
89+
}
90+
91+
}
92+
93+
}

Tests/phaser-debug.js

Lines changed: 13 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -13120,6 +13120,10 @@ var Phaser;
1312013120
* @type {string}
1312113121
*/
1312213122
this.crossOrigin = '';
13123+
// If you want to append a URL before the path of any asset you can set this here.
13124+
// Useful if you need to allow an asset url to be configured outside of the game code.
13125+
// MUST have / on the end of it!
13126+
this.baseURL = '';
1312313127
this.game = game;
1312413128

1312513129
this._keys = [];
@@ -13326,15 +13330,15 @@ var Phaser;
1332613330
return _this.fileError(file.key);
1332713331
};
1332813332
file.data.crossOrigin = this.crossOrigin;
13329-
file.data.src = file.url;
13333+
file.data.src = this.baseURL + file.url;
1333013334
break;
1333113335

1333213336
case 'audio':
1333313337
file.url = this.getAudioURL(file.url);
1333413338

1333513339
if (file.url !== null) {
1333613340
if (this.game.sound.usingWebAudio) {
13337-
this._xhr.open("GET", file.url, true);
13341+
this._xhr.open("GET", this.baseURL + file.url, true);
1333813342
this._xhr.responseType = "arraybuffer";
1333913343
this._xhr.onload = function () {
1334013344
return _this.fileComplete(file.key);
@@ -13349,7 +13353,7 @@ var Phaser;
1334913353
file.data = new Audio();
1335013354
file.data.name = file.key;
1335113355
file.data.preload = 'auto';
13352-
file.data.src = file.url;
13356+
file.data.src = this.baseURL + file.url;
1335313357
this.fileComplete(file.key);
1335413358
} else {
1335513359
file.data = new Audio();
@@ -13358,7 +13362,7 @@ var Phaser;
1335813362
return _this.fileError(file.key);
1335913363
};
1336013364
file.data.preload = 'auto';
13361-
file.data.src = file.url;
13365+
file.data.src = this.baseURL + file.url;
1336213366
file.data.addEventListener('canplaythrough', Phaser.GAMES[this.game.id].load.fileComplete(file.key), false);
1336313367
file.data.load();
1336413368
}
@@ -13368,7 +13372,7 @@ var Phaser;
1336813372
break;
1336913373

1337013374
case 'text':
13371-
this._xhr.open("GET", file.url, true);
13375+
this._xhr.open("GET", this.baseURL + file.url, true);
1337213376
this._xhr.responseType = "text";
1337313377
this._xhr.onload = function () {
1337413378
return _this.fileComplete(file.key);
@@ -13442,7 +13446,7 @@ var Phaser;
1344213446
} else {
1344313447
// Load the JSON or XML before carrying on with the next file
1344413448
loadNext = false;
13445-
this._xhr.open("GET", file.atlasURL, true);
13449+
this._xhr.open("GET", this.baseURL + file.atlasURL, true);
1344613450
this._xhr.responseType = "text";
1344713451

1344813452
if (file.format == Loader.TEXTURE_ATLAS_JSON_ARRAY) {
@@ -14796,71 +14800,6 @@ var Phaser;
1479614800
enumerable: true,
1479714801
configurable: true
1479814802
});
14799-
14800-
// MOVE THESE TO A UTIL
14801-
Body.prototype.render = function (context) {
14802-
context.beginPath();
14803-
context.strokeStyle = 'rgb(0,255,0)';
14804-
context.strokeRect(this.position.x - this.bounds.halfWidth, this.position.y - this.bounds.halfHeight, this.bounds.width, this.bounds.height);
14805-
context.stroke();
14806-
context.closePath();
14807-
14808-
// center point
14809-
context.fillStyle = 'rgb(0,255,0)';
14810-
context.fillRect(this.position.x, this.position.y, 2, 2);
14811-
14812-
if (this.touching & Phaser.Types.LEFT) {
14813-
context.beginPath();
14814-
context.strokeStyle = 'rgb(255,0,0)';
14815-
context.moveTo(this.position.x - this.bounds.halfWidth, this.position.y - this.bounds.halfHeight);
14816-
context.lineTo(this.position.x - this.bounds.halfWidth, this.position.y + this.bounds.halfHeight);
14817-
context.stroke();
14818-
context.closePath();
14819-
}
14820-
if (this.touching & Phaser.Types.RIGHT) {
14821-
context.beginPath();
14822-
context.strokeStyle = 'rgb(255,0,0)';
14823-
context.moveTo(this.position.x + this.bounds.halfWidth, this.position.y - this.bounds.halfHeight);
14824-
context.lineTo(this.position.x + this.bounds.halfWidth, this.position.y + this.bounds.halfHeight);
14825-
context.stroke();
14826-
context.closePath();
14827-
}
14828-
14829-
if (this.touching & Phaser.Types.UP) {
14830-
context.beginPath();
14831-
context.strokeStyle = 'rgb(255,0,0)';
14832-
context.moveTo(this.position.x - this.bounds.halfWidth, this.position.y - this.bounds.halfHeight);
14833-
context.lineTo(this.position.x + this.bounds.halfWidth, this.position.y - this.bounds.halfHeight);
14834-
context.stroke();
14835-
context.closePath();
14836-
}
14837-
if (this.touching & Phaser.Types.DOWN) {
14838-
context.beginPath();
14839-
context.strokeStyle = 'rgb(255,0,0)';
14840-
context.moveTo(this.position.x - this.bounds.halfWidth, this.position.y + this.bounds.halfHeight);
14841-
context.lineTo(this.position.x + this.bounds.halfWidth, this.position.y + this.bounds.halfHeight);
14842-
context.stroke();
14843-
context.closePath();
14844-
}
14845-
};
14846-
14847-
/**
14848-
* Render debug infos. (including name, bounds info, position and some other properties)
14849-
* @param x {number} X position of the debug info to be rendered.
14850-
* @param y {number} Y position of the debug info to be rendered.
14851-
* @param [color] {number} color of the debug info to be rendered. (format is css color string)
14852-
*/
14853-
Body.prototype.renderDebugInfo = function (x, y, color) {
14854-
if (typeof color === "undefined") { color = 'rgb(255,255,255)'; }
14855-
this.sprite.texture.context.fillStyle = color;
14856-
this.sprite.texture.context.fillText('Sprite: (' + this.sprite.width + ' x ' + this.sprite.height + ')', x, y);
14857-
14858-
//this.sprite.texture.context.fillText('x: ' + this._sprite.frameBounds.x.toFixed(1) + ' y: ' + this._sprite.frameBounds.y.toFixed(1) + ' rotation: ' + this._sprite.rotation.toFixed(1), x, y + 14);
14859-
this.sprite.texture.context.fillText('x: ' + this.bounds.x.toFixed(1) + ' y: ' + this.bounds.y.toFixed(1) + ' rotation: ' + this.sprite.transform.rotation.toFixed(0), x, y + 14);
14860-
this.sprite.texture.context.fillText('vx: ' + this.velocity.x.toFixed(1) + ' vy: ' + this.velocity.y.toFixed(1), x, y + 28);
14861-
this.sprite.texture.context.fillText('acx: ' + this.acceleration.x.toFixed(1) + ' acy: ' + this.acceleration.y.toFixed(1), x, y + 42);
14862-
this.sprite.texture.context.fillText('angVx: ' + this.angularVelocity.toFixed(1) + ' angAc: ' + this.angularAcceleration.toFixed(1), x, y + 56);
14863-
};
1486414803
return Body;
1486514804
})();
1486614805
Physics.Body = Body;
@@ -15013,7 +14952,7 @@ var Phaser;
1501314952
*/
1501414953
Sprite.prototype.bringToTop = function () {
1501514954
if (this.group) {
15016-
//this.group.bringToTop(this);
14955+
this.group.bringToTop(this);
1501714956
}
1501814957
};
1501914958

@@ -19757,7 +19696,7 @@ var Phaser;
1975719696
//this.physics = new Phaser.Physics.Manager(this);
1975819697
this.plugins = new Phaser.PluginManager(this, this);
1975919698

19760-
this.load.onLoadComplete.addOnce(this.loadComplete, this);
19699+
this.load.onLoadComplete.add(this.loadComplete, this);
1976119700

1976219701
this.setRenderer(Phaser.Types.RENDERER_CANVAS);
1976319702

@@ -19883,6 +19822,7 @@ var Phaser;
1988319822
this._loadComplete = true;
1988419823
} else {
1988519824
// Start the loader going as we have something in the queue
19825+
this.load.onLoadComplete.add(this.loadComplete, this);
1988619826
this.load.start();
1988719827
}
1988819828
} else {

0 commit comments

Comments
 (0)