0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-23 09:26:51 -05:00

decouple Matter.RenderPixi

This commit is contained in:
liabru 2016-04-26 20:48:13 +01:00
parent de823ebb08
commit 72f90b99dd
4 changed files with 562 additions and 546 deletions

View file

@ -131,11 +131,6 @@
World.add(demo.engine.world, demo.mouseConstraint); World.add(demo.engine.world, demo.mouseConstraint);
}); });
// need to rebind mouse on render change
Events.on(demo.gui, 'setRenderer', function() {
Mouse.setElement(demo.mouseConstraint.mouse, demo.render.canvas);
});
} }
// create a Matter.Inspector // create a Matter.Inspector

View file

@ -1,5 +1,5 @@
/** /**
* matter-tools-dev.min.js 0.5.0-dev 2016-04-24 * matter-tools-dev.min.js 0.5.0-dev 2016-04-26
* https://github.com/liabru/matter-tools * https://github.com/liabru/matter-tools
* License: MIT * License: MIT
*/ */
@ -202,9 +202,6 @@
physics.add(runner, "enabled"); physics.add(runner, "enabled");
physics.open(); physics.open();
var render = datGui.addFolder("Render"); var render = datGui.addFolder("Render");
render.add(gui, "renderer", [ "canvas", "webgl" ]).onFinishChange(function(value) {
_setRenderer(gui, value);
});
render.add(gui.render.options, "wireframes"); render.add(gui.render.options, "wireframes");
render.add(gui.render.options, "showDebug"); render.add(gui.render.options, "showDebug");
render.add(gui.render.options, "showPositions"); render.add(gui.render.options, "showPositions");
@ -223,19 +220,6 @@
render.add(gui.render.options, "enabled"); render.add(gui.render.options, "enabled");
render.open(); render.open();
}; };
var _setRenderer = function(gui, rendererName) {
var engine = gui.engine, controller;
if (rendererName === "canvas") controller = Render;
if (rendererName === "webgl") controller = RenderPixi;
gui.render.element.removeChild(gui.render.canvas);
var options = gui.render.options;
gui.render = controller.create({
element:gui.render.element,
options:options
});
gui.render = options;
Events.trigger(gui, "setRenderer");
};
var _addBody = function(gui) { var _addBody = function(gui) {
var engine = gui.engine; var engine = gui.engine;
var options = { var options = {

File diff suppressed because one or more lines are too long

View file

@ -13,6 +13,18 @@ var Composite = require('../body/Composite');
var Common = require('../core/Common'); var Common = require('../core/Common');
(function() { (function() {
var _requestAnimationFrame,
_cancelAnimationFrame;
if (typeof window !== 'undefined') {
_requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame
|| window.mozRequestAnimationFrame || window.msRequestAnimationFrame
|| function(callback){ window.setTimeout(function() { callback(Common.now()); }, 1000 / 60); };
_cancelAnimationFrame = window.cancelAnimationFrame || window.mozCancelAnimationFrame
|| window.webkitCancelAnimationFrame || window.msCancelAnimationFrame;
}
/** /**
* Creates a new Pixi.js WebGL renderer * Creates a new Pixi.js WebGL renderer
@ -23,7 +35,9 @@ var Common = require('../core/Common');
RenderPixi.create = function(options) { RenderPixi.create = function(options) {
var defaults = { var defaults = {
controller: RenderPixi, controller: RenderPixi,
engine: null,
element: null, element: null,
frameRequestId: null,
canvas: null, canvas: null,
renderer: null, renderer: null,
container: null, container: null,
@ -62,6 +76,8 @@ var Common = require('../core/Common');
backgroundColor: options.background backgroundColor: options.background
}; };
render.mouse = options.mouse;
render.engine = options.engine;
render.renderer = render.renderer || new PIXI.WebGLRenderer(render.options.width, render.options.height, render.pixiOptions); render.renderer = render.renderer || new PIXI.WebGLRenderer(render.options.width, render.options.height, render.pixiOptions);
render.container = render.container || new PIXI.Container(); render.container = render.container || new PIXI.Container();
render.spriteContainer = render.spriteContainer || new PIXI.Container(); render.spriteContainer = render.spriteContainer || new PIXI.Container();
@ -99,6 +115,27 @@ var Common = require('../core/Common');
return render; return render;
}; };
/**
* Continuously updates the render canvas on the `requestAnimationFrame` event.
* @method run
* @param {render} render
*/
RenderPixi.run = function(render) {
(function loop(time){
render.frameRequestId = _requestAnimationFrame(loop);
RenderPixi.world(render);
})();
};
/**
* Ends execution of `Render.run` on the given `render`, by canceling the animation frame request event loop.
* @method stop
* @param {render} render
*/
RenderPixi.stop = function(render) {
_cancelAnimationFrame(render.frameRequestId);
};
/** /**
* Clears the scene graph * Clears the scene graph
* @method clear * @method clear
@ -181,8 +218,8 @@ var Common = require('../core/Common');
* @method world * @method world
* @param {engine} engine * @param {engine} engine
*/ */
RenderPixi.world = function(engine) { RenderPixi.world = function(render) {
var render = engine.render, var engine = render.engine,
world = engine.world, world = engine.world,
renderer = render.renderer, renderer = render.renderer,
container = render.container, container = render.container,
@ -237,10 +274,10 @@ var Common = require('../core/Common');
} }
for (i = 0; i < bodies.length; i++) for (i = 0; i < bodies.length; i++)
RenderPixi.body(engine, bodies[i]); RenderPixi.body(render, bodies[i]);
for (i = 0; i < constraints.length; i++) for (i = 0; i < constraints.length; i++)
RenderPixi.constraint(engine, constraints[i]); RenderPixi.constraint(render, constraints[i]);
renderer.render(container); renderer.render(container);
}; };
@ -252,8 +289,8 @@ var Common = require('../core/Common');
* @param {engine} engine * @param {engine} engine
* @param {constraint} constraint * @param {constraint} constraint
*/ */
RenderPixi.constraint = function(engine, constraint) { RenderPixi.constraint = function(render, constraint) {
var render = engine.render, var engine = render.engine,
bodyA = constraint.bodyA, bodyA = constraint.bodyA,
bodyB = constraint.bodyB, bodyB = constraint.bodyB,
pointA = constraint.pointA, pointA = constraint.pointA,
@ -303,8 +340,8 @@ var Common = require('../core/Common');
* @param {engine} engine * @param {engine} engine
* @param {body} body * @param {body} body
*/ */
RenderPixi.body = function(engine, body) { RenderPixi.body = function(render, body) {
var render = engine.render, var engine = render.engine,
bodyRender = body.render; bodyRender = body.render;
if (!bodyRender.visible) if (!bodyRender.visible)