Skip to content

Commit a7ff5f8

Browse files
committed
Added basic Webcam plugin.
Added Device.getUserMedia detection. Updated config.php so you can toggle physics engines on/off via flags. Updated Gruntfile.js so it builds a Phaser + Pixi but no Physics libs.
1 parent 511b2f0 commit a7ff5f8

5 files changed

Lines changed: 183 additions & 28 deletions

File tree

Gruntfile.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,15 @@ module.exports = function (grunt) {
276276
dest: '<%= compile_dir %>/phaser-no-libs.js'
277277
},
278278

279+
// Phaser with pixi but no physics libs
280+
phaserNoPhysics: {
281+
options: {
282+
banner: '<%= banner %>'
283+
},
284+
src: ['<%= compile_dir %>/pixi.js', '<%= compile_dir %>/phaser-no-libs.js'],
285+
dest: '<%= compile_dir %>/phaser-no-physics.js'
286+
},
287+
279288
// One ring to rule them all
280289
standalone: {
281290
options: {
@@ -321,6 +330,14 @@ module.exports = function (grunt) {
321330
dest: '<%= compile_dir %>/phaser-no-libs.min.js'
322331
},
323332

333+
phaserNoPhysics: {
334+
options: {
335+
banner: '/* Phaser (no physics) v<%= pkg.version %> - http://phaser.io - @photonstorm - (c) 2014 Photon Storm Ltd. */\n'
336+
},
337+
src: ['<%= concat.phaserNoPhysics.dest %>'],
338+
dest: '<%= compile_dir %>/phaser-no-physics.min.js'
339+
},
340+
324341
standalone: {
325342
options: {
326343
sourceMap: true,
@@ -347,7 +364,10 @@ module.exports = function (grunt) {
347364
{ src: ['dist/pixi.js'], dest: 'build/custom/pixi.js' },
348365
{ src: ['dist/pixi.min.js'], dest: 'build/custom/pixi.min.js' },
349366
{ src: ['dist/ninja.js'], dest: 'build/custom/ninja.js' },
350-
{ src: ['dist/ninja.min.js'], dest: 'build/custom/ninja.min.js' }
367+
{ src: ['dist/ninja.min.js'], dest: 'build/custom/ninja.min.js' },
368+
{ src: ['dist/phaser-no-physics.js'], dest: 'build/custom/phaser-no-physics.js' },
369+
{ src: ['dist/phaser-no-physics.min.js'], dest: 'build/custom/phaser-no-physics.min.js' }
370+
351371
]
352372
}
353373
},

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ Updated:
7676
* P2.removeBody will check if the body is part of the world before removing, this avoids a TypeError from the p2 layer.
7777
* Tilemap.createFromObjects has a new parameter: adjustY, which is true by default. Because Tiled uses a bottom-left coordinate system Phaser used to set the Sprite anchor to 0,1 to compensate. If adjustY is true it now reduces the y value by the object height instead.
7878
* Swapped the order of the _pollGamepads gamepads check, to stop the Chrome 'webkitGamepads is deprecated' error in the console.
79+
* Lots of TypeScript definitions updates (thanks as always to clark for these)
80+
* Removed Device.patchAndroidClearRectBug as it's no longer used internally.
81+
* Math.wrapAngle now supports radians (thanks Cryszon, #597)
82+
83+
84+
New Features:
85+
86+
* Device.getUserMedia boolean added, useful if you need access to the webcam or microphone.
7987

8088

8189
TODO:

build/config.php

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,27 @@
44
$path = '..';
55
}
66

7-
echo <<<EOL
7+
if (!isset($p2))
8+
{
9+
$p2 = true;
10+
}
11+
12+
if (!isset($ninja))
13+
{
14+
$ninja = true;
15+
}
816

9-
<script src="$path/src/physics/p2/p2.js"></script>
17+
if (!isset($arcade))
18+
{
19+
$arcade = true;
20+
}
21+
22+
if ($p2)
23+
{
24+
echo " <script src=\"$path/src/physics/p2/p2.js\"></script>";
25+
}
26+
27+
echo <<<EOL
1028
1129
<script src="$path/src/pixi/Pixi.js"></script>
1230
<script src="$path/src/pixi/core/Point.js"></script>
@@ -137,14 +155,30 @@
137155
138156
<script src="$path/src/physics/Physics.js"></script>
139157
158+
<script src="$path/src/particles/Particles.js"></script>
159+
<script src="$path/src/particles/arcade/ArcadeParticles.js"></script>
160+
<script src="$path/src/particles/arcade/Emitter.js"></script>
161+
162+
<script src="$path/src/tilemap/Tile.js"></script>
163+
<script src="$path/src/tilemap/Tilemap.js"></script>
164+
<script src="$path/src/tilemap/TilemapLayer.js"></script>
165+
<script src="$path/src/tilemap/TilemapParser.js"></script>
166+
<script src="$path/src/tilemap/Tileset.js"></script>
167+
EOL;
168+
169+
if ($arcade)
170+
{
171+
echo <<<EOL
172+
140173
<script src="$path/src/physics/arcade/World.js"></script>
141174
<script src="$path/src/physics/arcade/Body.js"></script>
175+
EOL;
176+
}
142177

143-
<script src="$path/src/physics/ninja/World.js"></script>
144-
<script src="$path/src/physics/ninja/Body.js"></script>
145-
<script src="$path/src/physics/ninja/AABB.js"></script>
146-
<script src="$path/src/physics/ninja/Tile.js"></script>
147-
<script src="$path/src/physics/ninja/Circle.js"></script>
178+
179+
if ($p2)
180+
{
181+
echo <<<EOL
148182
149183
<script src="$path/src/physics/p2/World.js"></script>
150184
<script src="$path/src/physics/p2/PointProxy.js"></script>
@@ -160,20 +194,22 @@
160194
<script src="$path/src/physics/p2/LockConstraint.js"></script>
161195
<script src="$path/src/physics/p2/PrismaticConstraint.js"></script>
162196
<script src="$path/src/physics/p2/RevoluteConstraint.js"></script>
163-
164-
<script src="$path/src/particles/Particles.js"></script>
165-
<script src="$path/src/particles/arcade/ArcadeParticles.js"></script>
166-
<script src="$path/src/particles/arcade/Emitter.js"></script>
167-
168-
<script src="$path/src/tilemap/Tile.js"></script>
169-
<script src="$path/src/tilemap/Tilemap.js"></script>
170-
<script src="$path/src/tilemap/TilemapLayer.js"></script>
171-
<script src="$path/src/tilemap/TilemapParser.js"></script>
172-
<script src="$path/src/tilemap/Tileset.js"></script>
173197
EOL;
198+
}
174199

175-
/*
200+
if ($ninja)
201+
{
202+
echo <<<EOL
203+
204+
<script src="$path/src/physics/arcade/World.js"></script>
205+
<script src="$path/src/physics/arcade/Body.js"></script>
176206
177-
*/
207+
<script src="$path/src/physics/ninja/World.js"></script>
208+
<script src="$path/src/physics/ninja/Body.js"></script>
209+
<script src="$path/src/physics/ninja/AABB.js"></script>
210+
<script src="$path/src/physics/ninja/Tile.js"></script>
211+
<script src="$path/src/physics/ninja/Circle.js"></script>
212+
EOL;
213+
}
178214

179215
?>

plugins/Webcam.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* Provides access to the Webcam (if available)
3+
*/
4+
Phaser.Plugin.Webcam = function (game, parent) {
5+
6+
Phaser.Plugin.call(this, game, parent);
7+
8+
if (!game.device.getUserMedia)
9+
{
10+
return false;
11+
}
12+
13+
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
14+
15+
this.context = null;
16+
this.stream = null;
17+
18+
this.video = document.createElement('video');
19+
this.video.autoplay = true;
20+
21+
};
22+
23+
Phaser.Plugin.Webcam.prototype = Object.create(Phaser.Plugin.prototype);
24+
Phaser.Plugin.Webcam.prototype.constructor = Phaser.Plugin.Webcam;
25+
26+
Phaser.Plugin.Webcam.prototype.start = function (width, height, context) {
27+
28+
console.log('Webcam start', width, height);
29+
30+
this.context = context;
31+
32+
if (!this.stream)
33+
{
34+
navigator.getUserMedia( { video: { mandatory: { minWidth: width, minHeight: height } } }, this.connectCallback.bind(this), this.errorCallback.bind(this));
35+
}
36+
37+
};
38+
39+
Phaser.Plugin.Webcam.prototype.stop = function () {
40+
41+
if (this.stream)
42+
{
43+
this.stream.stop();
44+
this.stream = null;
45+
}
46+
47+
};
48+
49+
Phaser.Plugin.Webcam.prototype.connectCallback = function (stream) {
50+
51+
this.stream = stream;
52+
53+
this.video.src = window.URL.createObjectURL(this.stream);
54+
55+
};
56+
57+
Phaser.Plugin.Webcam.prototype.errorCallback = function (e) {
58+
59+
console.log('Video Stream rejected', e);
60+
61+
};
62+
63+
Phaser.Plugin.Webcam.prototype.grab = function (context, x, y) {
64+
65+
if (this.stream)
66+
{
67+
context.drawImage(this.video, x, y);
68+
}
69+
70+
};
71+
72+
Phaser.Plugin.Webcam.prototype.update = function () {
73+
74+
if (this.stream)
75+
{
76+
this.context.drawImage(this.video, 0, 0);
77+
}
78+
79+
};
80+
81+
/**
82+
* @name Phaser.Plugin.Webcam#active
83+
* @property {boolean} active - Is this Webcam plugin capturing a video stream or not?
84+
* @readonly
85+
*/
86+
Object.defineProperty(Phaser.Plugin.Webcam.prototype, "active", {
87+
88+
get: function() {
89+
return (this.stream);
90+
}
91+
92+
});

src/system/Device.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@ Phaser.Device = function (game) {
1818
*/
1919
this.game = game;
2020

21-
/**
22-
* An optional 'fix' for the horrendous Android stock browser bug https://code.google.com/p/android/issues/detail?id=39247
23-
* @property {boolean} patchAndroidClearRectBug - Description.
24-
* @default
25-
*/
26-
this.patchAndroidClearRectBug = false;
27-
2821
// Operating System
2922

3023
/**
@@ -161,6 +154,12 @@ Phaser.Device = function (game) {
161154
*/
162155
this.vibration = false;
163156

157+
/**
158+
* @property {boolean} getUserMedia - Does the device support the getUserMedia API?
159+
* @default
160+
*/
161+
this.getUserMedia = false;
162+
164163
/**
165164
* @property {boolean} quirksMode - Is the browser running in strict mode (false) or quirks mode? (true)
166165
* @default
@@ -468,7 +467,7 @@ Phaser.Device.prototype = {
468467

469468
this.quirksMode = (document.compatMode === 'CSS1Compat') ? false : true;
470469

471-
470+
this.getUserMedia = !!(navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
472471

473472
},
474473

0 commit comments

Comments
 (0)