Skip to content

Commit 8683d41

Browse files
committed
New Text examples.
1 parent e9ae465 commit 8683d41

6 files changed

Lines changed: 195 additions & 6 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { create: create, update: update });
3+
4+
var text;
5+
6+
function create() {
7+
8+
game.stage.setBackgroundColor(0xfbf6d5);
9+
10+
text = game.add.text(game.world.centerX, 250, ' dynamic shadows ');
11+
text.anchor.set(0.5);
12+
text.align = 'center';
13+
14+
text.font = 'Arial Black';
15+
text.fontSize = 70;
16+
text.fontWeight = 'bold';
17+
text.fill = '#ec008c';
18+
19+
text.setShadow(0, 0, 'rgba(0, 0, 0, 0.5)', 0);
20+
21+
}
22+
23+
function update() {
24+
25+
var offset = moveToXY(game.input.activePointer, text.x, text.y, 8);
26+
27+
text.setShadow(offset.x, offset.y, 'rgba(0, 0, 0, 0.5)', distanceToPointer(text, game.input.activePointer) / 30);
28+
29+
}
30+
31+
function distanceToPointer(displayObject, pointer) {
32+
33+
this._dx = displayObject.x - pointer.x;
34+
this._dy = displayObject.y - pointer.y;
35+
36+
return Math.sqrt(this._dx * this._dx + this._dy * this._dy);
37+
38+
}
39+
40+
function moveToXY(displayObject, x, y, speed) {
41+
42+
var _angle = Math.atan2(y - displayObject.y, x - displayObject.x);
43+
44+
var x = Math.cos(_angle) * speed;
45+
var y = Math.sin(_angle) * speed;
46+
47+
return { x: x, y: y };
48+
49+
}

examples/text/text gradient.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { create: create });
3+
4+
var text = null;
5+
6+
function create() {
7+
8+
text = game.add.text(game.world.centerX, game.world.centerY, "- phaser gradient text -");
9+
10+
// Centers the text
11+
text.anchor.set(0.5);
12+
text.align = 'center';
13+
14+
// Our font + size
15+
text.font = 'Arial';
16+
text.fontWeight = 'bold';
17+
text.fontSize = 70;
18+
19+
// Here we create a linear gradient on the Text context.
20+
// This uses the exact same method of creating a gradient as you do on a normal Canvas context.
21+
var grd = text.context.createLinearGradient(0, 0, 0, text.canvas.height);
22+
23+
// Add in 2 color stops
24+
grd.addColorStop(0, '#8ED6FF');
25+
grd.addColorStop(1, '#004CB3');
26+
27+
// And apply to the Text
28+
text.fill = grd;
29+
30+
}

examples/text/text reflect.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { create: create });
3+
4+
var text = null;
5+
var textReflect = null;
6+
7+
function create() {
8+
9+
game.stage.backgroundColor = 0x3b0760;
10+
11+
text = game.add.text(game.world.centerX, game.world.centerY, "- PHASER -");
12+
13+
// Centers the text
14+
text.anchor.set(0.5);
15+
text.align = 'center';
16+
17+
// Our font + size
18+
text.font = 'Arial';
19+
text.fontWeight = 'bold';
20+
text.fontSize = 70;
21+
text.fill = '#ffffff';
22+
23+
// Here we create our fake reflection :)
24+
// It's just another Text object, with an alpha gradient and flipped vertically
25+
26+
textReflect = game.add.text(game.world.centerX, game.world.centerY + 50, "- PHASER -");
27+
28+
// Centers the text
29+
textReflect.anchor.set(0.5);
30+
textReflect.align = 'center';
31+
textReflect.scale.y = -1;
32+
33+
// Our font + size
34+
textReflect.font = 'Arial';
35+
textReflect.fontWeight = 'bold';
36+
textReflect.fontSize = 70;
37+
38+
// Here we create a linear gradient on the Text context.
39+
// This uses the exact same method of creating a gradient as you do on a normal Canvas context.
40+
var grd = textReflect.context.createLinearGradient(0, 0, 0, text.canvas.height);
41+
42+
// Add in 2 color stops
43+
grd.addColorStop(0, 'rgba(255,255,255,0)');
44+
grd.addColorStop(1, 'rgba(255,255,255,0.08)');
45+
46+
// And apply to the Text
47+
textReflect.fill = grd;
48+
49+
}

examples/text/text shadow.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { create: create, update: update });
3+
4+
function create() {
5+
6+
game.stage.setBackgroundColor(0xefefef);
7+
8+
// Here we set the shadow:
9+
// The first 2 parameters control x and y distance
10+
// The 3rd the shadow colour
11+
// The 4th the amount of blur
12+
13+
var text = createText(100, 'shadow 5');
14+
text.setShadow(3, 3, 'rgba(0,0,0,0.5)', 5);
15+
16+
text = createText(300, 'shadow 0');
17+
text.setShadow(5, 5, 'rgba(0,0,0,0.5)', 15);
18+
19+
text = createText(500, 'shadow 10');
20+
text.setShadow(-5, 5, 'rgba(0,0,0,0.5)', 0);
21+
22+
}
23+
24+
function createText(y) {
25+
26+
var text = game.add.text(game.world.centerX, y, '- phaser text shadow -');
27+
text.anchor.set(0.5);
28+
text.align = 'center';
29+
30+
// Font style
31+
text.font = 'Arial Black';
32+
text.fontSize = 50;
33+
text.fontWeight = 'bold';
34+
text.fill = '#ff00ff';
35+
36+
return text;
37+
38+
}
39+
40+
function update() {
41+
42+
}

examples/text/text stroke.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11

22
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { create: create, update: update });
33

4-
var s;
4+
var text;
55

66
function create() {
77

8-
var text = "--- phaser ---\nwith a sprinkle of\npixi dust";
9-
var style = { font: "bold 40pt Arial", fill: "#ffffff", align: "center", stroke: "#258acc", strokeThickness: 8 };
8+
game.stage.setBackgroundColor(0x2d2d2d);
109

11-
s = game.add.text(game.world.centerX, game.world.centerY, text, style);
12-
s.anchor.setTo(0.5, 0.5);
10+
text = game.add.text(game.world.centerX, game.world.centerY, '- phaser text stroke -');
11+
12+
// Center align
13+
text.anchor.set(0.5);
14+
text.align = 'center';
15+
16+
// Font style
17+
text.font = 'Arial Black';
18+
text.fontSize = 50;
19+
text.fontWeight = 'bold';
20+
21+
// Stroke color and thickness
22+
text.stroke = '#000000';
23+
text.strokeThickness = 6;
24+
text.fill = '#43d637';
1325

1426
}
1527

1628
function update() {
17-
s.angle += 1;
29+
30+
// text.angle += 1;
31+
1832
}

src/gameobjects/Text.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ Object.defineProperty(Phaser.Text.prototype, 'text', {
470470
{
471471
this._text = value.toString() || ' ';
472472
this.dirty = true;
473+
this.updateTransform();
473474
}
474475

475476
}
@@ -493,6 +494,7 @@ Object.defineProperty(Phaser.Text.prototype, 'font', {
493494
this._font = value.trim();
494495
this.style.font = this._fontWeight + ' ' + this._fontSize + "px '" + this._font + "'";
495496
this.dirty = true;
497+
this.updateTransform();
496498
}
497499

498500
}
@@ -518,6 +520,7 @@ Object.defineProperty(Phaser.Text.prototype, 'fontSize', {
518520
this._fontSize = value;
519521
this.style.font = this._fontWeight + ' ' + this._fontSize + "px '" + this._font + "'";
520522
this.dirty = true;
523+
this.updateTransform();
521524
}
522525

523526
}
@@ -541,6 +544,7 @@ Object.defineProperty(Phaser.Text.prototype, 'fontWeight', {
541544
this._fontWeight = value;
542545
this.style.font = this._fontWeight + ' ' + this._fontSize + "px '" + this._font + "'";
543546
this.dirty = true;
547+
this.updateTransform();
544548
}
545549

546550
}
@@ -695,6 +699,7 @@ Object.defineProperty(Phaser.Text.prototype, 'lineSpacing', {
695699
{
696700
this._lineSpacing = parseFloat(value);
697701
this.dirty = true;
702+
this.updateTransform();
698703
}
699704

700705
}

0 commit comments

Comments
 (0)