Skip to content

Commit b908ebd

Browse files
committed
Added fromSVG method
1 parent 08b30a2 commit b908ebd

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

src/physics/matter-js/Factory.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var Body = require('./lib/body/Body');
99
var Class = require('../../utils/Class');
1010
var Composites = require('./lib/factory/Composites');
1111
var Constraint = require('./lib/constraint/Constraint');
12+
var Svg = require('./lib/geometry/Svg');
1213
var MatterGameObject = require('./MatterGameObject');
1314
var MatterImage = require('./MatterImage');
1415
var MatterSprite = require('./MatterSprite');
@@ -185,6 +186,52 @@ var Factory = new Class({
185186
return body;
186187
},
187188

189+
/**
190+
* Creates a body using the supplied body data, as provided by an SVG file.
191+
*
192+
* @method Phaser.Physics.Matter.Factory#fromSVG
193+
* @since 3.22.0
194+
*
195+
* @param {number} x - The X coordinate of the body.
196+
* @param {number} y - The Y coordinate of the body.
197+
* @param {object} xml - The SVG Path data.
198+
* @param {number} [scale=1] - Scale the vertices by this amount after creation.
199+
* @param {object} [options] - Optional Matter body configuration object, as passed to `Body.create`.
200+
* @param {boolean} [addToWorld=true] - Should the newly created body be immediately added to the World?
201+
*
202+
* @return {MatterJS.Body} A Matter JS Body.
203+
*/
204+
fromSVG: function (x, y, xml, scale, options, addToWorld)
205+
{
206+
if (scale === undefined) { scale = 1; }
207+
if (options === undefined) { options = {}; }
208+
if (addToWorld === undefined) { addToWorld = true; }
209+
210+
var path = xml.getElementsByTagName('path');
211+
var vertexSets = [];
212+
213+
for (var i = 0; i < path.length; i++)
214+
{
215+
var points = Svg.pathToVertices(path[i], 30);
216+
217+
if (scale !== 1)
218+
{
219+
Vertices.scale(points, scale, scale);
220+
}
221+
222+
vertexSets.push(points);
223+
}
224+
225+
var body = Bodies.fromVertices(x, y, vertexSets, options);
226+
227+
if (addToWorld)
228+
{
229+
this.world.add(body);
230+
}
231+
232+
return body;
233+
},
234+
188235
/**
189236
* **This function is still in development**
190237
*

0 commit comments

Comments
 (0)