mirror of
https://github.com/liabru/matter-js.git
synced 2024-12-25 13:39:06 -05:00
improved custom engine renderer passing
This commit is contained in:
parent
99346160aa
commit
e619be0d01
2 changed files with 18 additions and 9 deletions
|
@ -29,6 +29,11 @@ var Engine = {};
|
|||
* @return {engine} engine
|
||||
*/
|
||||
Engine.create = function(element, options) {
|
||||
|
||||
// options may be passed as the first (and only) argument
|
||||
options = Common.isElement(element) ? options : element;
|
||||
element = Common.isElement(element) ? element : null;
|
||||
|
||||
var defaults = {
|
||||
enabled: true,
|
||||
positionIterations: 6,
|
||||
|
@ -52,20 +57,16 @@ var Engine = {};
|
|||
correction: 1,
|
||||
deltaMin: 1000 / _fps,
|
||||
deltaMax: 1000 / (_fps * 0.5)
|
||||
},
|
||||
render: {
|
||||
element: element,
|
||||
controller: Render
|
||||
}
|
||||
};
|
||||
|
||||
var engine = Common.extend(defaults, options);
|
||||
engine = Common.isElement(element) ? (engine || {}) : element;
|
||||
|
||||
// create default renderer only if no custom renderer set
|
||||
// but still allow engine.render.engine to pass through if set
|
||||
if (!engine.render || (engine.render && !engine.render.controller)) {
|
||||
engine.render = Render.create(engine.render);
|
||||
if (Common.isElement(element))
|
||||
element.appendChild(engine.render.canvas);
|
||||
}
|
||||
|
||||
engine.render = engine.render.controller.create(engine.render);
|
||||
engine.world = World.create(engine.world);
|
||||
engine.metrics = engine.metrics || Metrics.create();
|
||||
engine.input.mouse = engine.input.mouse || Mouse.create(engine.render.canvas);
|
||||
|
|
|
@ -21,6 +21,8 @@ var Render = {};
|
|||
Render.create = function(options) {
|
||||
var defaults = {
|
||||
controller: Render,
|
||||
element: null,
|
||||
canvas: null,
|
||||
options: {
|
||||
width: 800,
|
||||
height: 600,
|
||||
|
@ -49,6 +51,12 @@ var Render = {};
|
|||
|
||||
Render.setBackground(render, render.options.background);
|
||||
|
||||
if (Common.isElement(render.element)) {
|
||||
render.element.appendChild(render.canvas);
|
||||
} else {
|
||||
Common.log('No "render.element" passed, "render.canvas" was not inserted into document.', 'warn');
|
||||
}
|
||||
|
||||
return render;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue