Skip to content

Commit ad09a18

Browse files
committed
Finished JSDocs
1 parent 64572c4 commit ad09a18

3 files changed

Lines changed: 105 additions & 6 deletions

File tree

src/gameobjects/domelement/DOMElement.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,57 @@ var Vector4 = require('../../math/Vector4');
1414

1515
/**
1616
* @classdesc
17-
* [description]
17+
* DOM Element Game Objects are a way to control and manipulate HTML Elements over the top of your game.
18+
*
19+
* In order for DOM Elements to display you have to enable them by adding the following to your game
20+
* configuration object:
21+
*
22+
* ```javascript
23+
* dom {
24+
* createContainer: true
25+
* }
26+
* ```
27+
*
28+
* When this is added, Phaser will automatically create a DOM Container div that is positioned over the top
29+
* of the game canvas. This div is sized to match the canvas, and if the canvas size changes, as a result of
30+
* settings within the Scale Manager, the dom container is resized accordingly.
31+
*
32+
* You can create a DOM Element by either passing in DOMStrings, or by passing in a reference to an existing
33+
* Element that you wish to be placed under the control of Phaser. For example:
34+
*
35+
* ```javascript
36+
* this.add.dom(x, y, 'div', 'background-color: lime; width: 220px; height: 100px; font: 48px Arial', 'Phaser');
37+
* ```
38+
*
39+
* The above code will insert a div element into the DOM Container at the given x/y coordinate. The DOMString in
40+
* the 4th argument sets the initial CSS style of the div and the final argument is the inner text. In this case,
41+
* it will create a lime colored div that is 220px by 100px in size with the text Phaser in it, in an Arial font.
42+
*
43+
* You should nearly always, without exception, use explicitly sized HTML Elements, in order to fully control
44+
* alignment and positioning of the elements next to regular game content.
45+
*
46+
* Rather than specify the CSS and HTML directly you can use the `load.html` File Loader to load it into the
47+
* cache and then use the `createFromCache` method instead. You can also use `createFromHTML` and various other
48+
* methods available in this class to help construct your elements.
49+
*
50+
* Once the element has been created you can then control it like you would any other Game Object. You can set its
51+
* position, scale, rotation, alpha and other properties. It will move as the main Scene Camera moves and be clipped
52+
* at the edge of the canvas. It's important to remember some limitations of DOM Elements: The obvious one is that
53+
* they appear above or below your game canvas. You cannot blend them into the display list, meaning you cannot have
54+
* a DOM Element, then a Sprite, then another DOM Element behind it.
55+
*
56+
* They also cannot be enabled for input. To do that, you have to use the `addListener` method to add native event
57+
* listeners directly. The final limitation is to do with cameras. The DOM Container is sized to match the game canvas
58+
* entirely and clipped accordingly. DOM Elements respect camera scrolling and scrollFactor settings, but if you
59+
* change the size of the camera so it no longer matches the size of the canvas, they won't be clipped accordingly.
60+
*
61+
* Also, all DOM Elements are inserted into the same DOM Container, regardless of which Scene they are created in.
62+
*
63+
* DOM Elements are a powerful way to align native HTML with your Phaser Game Objects. For example, you can insert
64+
* a login form for a multiplayer game directly into your title screen. Or a text input box for a highscore table.
65+
* Or a banner ad from a 3rd party service. Or perhaps you'd like to use them for high resolution text display and
66+
* UI. The choice is up to you, just remember that you're dealing with standard HTML and CSS floating over the top
67+
* of your game, and should treat it accordingly.
1868
*
1969
* @class DOMElement
2070
* @extends Phaser.GameObjects.GameObject

src/gameobjects/domelement/DOMElementCSSRenderer.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ var GameObject = require('../GameObject');
2020
* @param {Phaser.GameObjects.DOMElement} src - The Game Object being rendered in this call.
2121
* @param {number} interpolationPercentage - Reserved for future use and custom pipelines.
2222
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object.
23+
* @param {Phaser.GameObjects.Components.TransformMatrix} parentMatrix - This transform matrix is defined if the game object is nested
2324
*/
24-
var DOMElementCSSRenderer = function (renderer, src, interpolationPercentage, camera, parentTransformMatrix)
25+
var DOMElementCSSRenderer = function (renderer, src, interpolationPercentage, camera, parentMatrix)
2526
{
2627
var node = src.node;
2728
var style = node.style;
@@ -54,7 +55,7 @@ var DOMElementCSSRenderer = function (renderer, src, interpolationPercentage, ca
5455
var tx = '0%';
5556
var ty = '0%';
5657

57-
if (parentTransformMatrix)
58+
if (parentMatrix)
5859
{
5960
dx = (src.width * src.scaleX) * src.originX;
6061
dy = (src.height * src.scaleY) * src.originY;
@@ -64,7 +65,7 @@ var DOMElementCSSRenderer = function (renderer, src, interpolationPercentage, ca
6465
camMatrix.copyFrom(camera.matrix);
6566

6667
// Multiply the camera by the parent matrix
67-
camMatrix.multiplyWithOffset(parentTransformMatrix, -camera.scrollX * src.scrollFactorX, -camera.scrollY * src.scrollFactorY);
68+
camMatrix.multiplyWithOffset(parentMatrix, -camera.scrollX * src.scrollFactorX, -camera.scrollY * src.scrollFactorY);
6869

6970
// Undo the camera scroll
7071
srcMatrix.e = src.x - dx;

src/gameobjects/domelement/DOMElementFactory.js

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,57 @@ var DOMElement = require('./DOMElement');
88
var GameObjectFactory = require('../GameObjectFactory');
99

1010
/**
11-
* Creates a new DOM Element Game Object and adds it to the parent DOM Container.
11+
* DOM Element Game Objects are a way to control and manipulate HTML Elements over the top of your game.
1212
*
13-
* The game must have been configured with the `dom.createContainer` property set to `true` for this to work.
13+
* In order for DOM Elements to display you have to enable them by adding the following to your game
14+
* configuration object:
15+
*
16+
* ```javascript
17+
* dom {
18+
* createContainer: true
19+
* }
20+
* ```
21+
*
22+
* When this is added, Phaser will automatically create a DOM Container div that is positioned over the top
23+
* of the game canvas. This div is sized to match the canvas, and if the canvas size changes, as a result of
24+
* settings within the Scale Manager, the dom container is resized accordingly.
25+
*
26+
* You can create a DOM Element by either passing in DOMStrings, or by passing in a reference to an existing
27+
* Element that you wish to be placed under the control of Phaser. For example:
28+
*
29+
* ```javascript
30+
* this.add.dom(x, y, 'div', 'background-color: lime; width: 220px; height: 100px; font: 48px Arial', 'Phaser');
31+
* ```
32+
*
33+
* The above code will insert a div element into the DOM Container at the given x/y coordinate. The DOMString in
34+
* the 4th argument sets the initial CSS style of the div and the final argument is the inner text. In this case,
35+
* it will create a lime colored div that is 220px by 100px in size with the text Phaser in it, in an Arial font.
36+
*
37+
* You should nearly always, without exception, use explicitly sized HTML Elements, in order to fully control
38+
* alignment and positioning of the elements next to regular game content.
39+
*
40+
* Rather than specify the CSS and HTML directly you can use the `load.html` File Loader to load it into the
41+
* cache and then use the `createFromCache` method instead. You can also use `createFromHTML` and various other
42+
* methods available in this class to help construct your elements.
43+
*
44+
* Once the element has been created you can then control it like you would any other Game Object. You can set its
45+
* position, scale, rotation, alpha and other properties. It will move as the main Scene Camera moves and be clipped
46+
* at the edge of the canvas. It's important to remember some limitations of DOM Elements: The obvious one is that
47+
* they appear above or below your game canvas. You cannot blend them into the display list, meaning you cannot have
48+
* a DOM Element, then a Sprite, then another DOM Element behind it.
49+
*
50+
* They also cannot be enabled for input. To do that, you have to use the `addListener` method to add native event
51+
* listeners directly. The final limitation is to do with cameras. The DOM Container is sized to match the game canvas
52+
* entirely and clipped accordingly. DOM Elements respect camera scrolling and scrollFactor settings, but if you
53+
* change the size of the camera so it no longer matches the size of the canvas, they won't be clipped accordingly.
54+
*
55+
* Also, all DOM Elements are inserted into the same DOM Container, regardless of which Scene they are created in.
56+
*
57+
* DOM Elements are a powerful way to align native HTML with your Phaser Game Objects. For example, you can insert
58+
* a login form for a multiplayer game directly into your title screen. Or a text input box for a highscore table.
59+
* Or a banner ad from a 3rd party service. Or perhaps you'd like to use them for high resolution text display and
60+
* UI. The choice is up to you, just remember that you're dealing with standard HTML and CSS floating over the top
61+
* of your game, and should treat it accordingly.
1462
*
1563
* Note: This method will only be available if the DOM Element Game Object has been built into Phaser.
1664
*

0 commit comments

Comments
 (0)