0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-12-25 13:39:06 -05:00

Merge branch 'pr/440'

* pr/440:
  Do not warn on missing render.element if the canvas is already parented (because it was passed in at construction).
This commit is contained in:
liabru 2017-11-25 17:31:58 +00:00
commit 2dc7a0bf5e

View file

@ -19,16 +19,16 @@ var Vector = require('../geometry/Vector');
var Mouse = require('../core/Mouse'); var Mouse = require('../core/Mouse');
(function() { (function() {
var _requestAnimationFrame, var _requestAnimationFrame,
_cancelAnimationFrame; _cancelAnimationFrame;
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
_requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame _requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame
|| window.mozRequestAnimationFrame || window.msRequestAnimationFrame || window.mozRequestAnimationFrame || window.msRequestAnimationFrame
|| function(callback){ window.setTimeout(function() { callback(Common.now()); }, 1000 / 60); }; || function(callback){ window.setTimeout(function() { callback(Common.now()); }, 1000 / 60); };
_cancelAnimationFrame = window.cancelAnimationFrame || window.mozCancelAnimationFrame _cancelAnimationFrame = window.cancelAnimationFrame || window.mozCancelAnimationFrame
|| window.webkitCancelAnimationFrame || window.msCancelAnimationFrame; || window.webkitCancelAnimationFrame || window.msCancelAnimationFrame;
} }
@ -89,12 +89,12 @@ var Mouse = require('../core/Mouse');
render.context = render.canvas.getContext('2d'); render.context = render.canvas.getContext('2d');
render.textures = {}; render.textures = {};
render.bounds = render.bounds || { render.bounds = render.bounds || {
min: { min: {
x: 0, x: 0,
y: 0 y: 0
}, },
max: { max: {
x: render.canvas.width, x: render.canvas.width,
y: render.canvas.height y: render.canvas.height
} }
@ -106,7 +106,7 @@ var Mouse = require('../core/Mouse');
if (Common.isElement(render.element)) { if (Common.isElement(render.element)) {
render.element.appendChild(render.canvas); render.element.appendChild(render.canvas);
} else { } else if (!render.canvas.parentNode) {
Common.log('Render.create: options.element was undefined, render.canvas was created but not appended', 'warn'); Common.log('Render.create: options.element was undefined, render.canvas was created but not appended', 'warn');
} }
@ -188,19 +188,19 @@ var Mouse = require('../core/Mouse');
for (var i = 0; i < objects.length; i += 1) { for (var i = 0; i < objects.length; i += 1) {
var object = objects[i], var object = objects[i],
min = object.bounds ? object.bounds.min : (object.min || object.position || object), min = object.bounds ? object.bounds.min : (object.min || object.position || object),
max = object.bounds ? object.bounds.max : (object.max || object.position || object); max = object.bounds ? object.bounds.max : (object.max || object.position || object);
if (min && max) { if (min && max) {
if (min.x < bounds.min.x) if (min.x < bounds.min.x)
bounds.min.x = min.x; bounds.min.x = min.x;
if (max.x > bounds.max.x) if (max.x > bounds.max.x)
bounds.max.x = max.x; bounds.max.x = max.x;
if (min.y < bounds.min.y) if (min.y < bounds.min.y)
bounds.min.y = min.y; bounds.min.y = min.y;
if (max.y > bounds.max.y) if (max.y > bounds.max.y)
bounds.max.y = max.y; bounds.max.y = max.y;
} }
} }
@ -375,7 +375,7 @@ var Mouse = require('../core/Mouse');
if (options.showAxes || options.showAngleIndicator) if (options.showAxes || options.showAngleIndicator)
Render.bodyAxes(render, bodies, context); Render.bodyAxes(render, bodies, context);
if (options.showPositions) if (options.showPositions)
Render.bodyPositions(render, bodies, context); Render.bodyPositions(render, bodies, context);
@ -456,7 +456,7 @@ var Mouse = require('../core/Mouse');
text += "mid: " + metrics.midEff + space; text += "mid: " + metrics.midEff + space;
text += "narrow: " + metrics.narrowEff + space; text += "narrow: " + metrics.narrowEff + space;
} }
// @endif // @endif
render.debugString = text; render.debugString = text;
render.debugTimestamp = engine.timing.timestamp; render.debugTimestamp = engine.timing.timestamp;
@ -555,7 +555,7 @@ var Mouse = require('../core/Mouse');
} }
} }
}; };
/** /**
* Description * Description
* @private * @private
@ -647,20 +647,20 @@ var Mouse = require('../core/Mouse');
var sprite = part.render.sprite, var sprite = part.render.sprite,
texture = _getTexture(render, sprite.texture); texture = _getTexture(render, sprite.texture);
c.translate(part.position.x, part.position.y); c.translate(part.position.x, part.position.y);
c.rotate(part.angle); c.rotate(part.angle);
c.drawImage( c.drawImage(
texture, texture,
texture.width * -sprite.xOffset * sprite.xScale, texture.width * -sprite.xOffset * sprite.xScale,
texture.height * -sprite.yOffset * sprite.yScale, texture.height * -sprite.yOffset * sprite.yScale,
texture.width * sprite.xScale, texture.width * sprite.xScale,
texture.height * sprite.yScale texture.height * sprite.yScale
); );
// revert translation, hopefully faster than save / restore // revert translation, hopefully faster than save / restore
c.rotate(-part.angle); c.rotate(-part.angle);
c.translate(-part.position.x, -part.position.y); c.translate(-part.position.x, -part.position.y);
} else { } else {
// part polygon // part polygon
if (part.circleRadius) { if (part.circleRadius) {
@ -681,7 +681,7 @@ var Mouse = require('../core/Mouse');
c.moveTo(part.vertices[(j + 1) % part.vertices.length].x, part.vertices[(j + 1) % part.vertices.length].y); c.moveTo(part.vertices[(j + 1) % part.vertices.length].x, part.vertices[(j + 1) % part.vertices.length].y);
} }
} }
c.lineTo(part.vertices[0].x, part.vertices[0].y); c.lineTo(part.vertices[0].x, part.vertices[0].y);
c.closePath(); c.closePath();
} }
@ -751,7 +751,7 @@ var Mouse = require('../core/Mouse');
c.moveTo(part.vertices[(j + 1) % part.vertices.length].x, part.vertices[(j + 1) % part.vertices.length].y); c.moveTo(part.vertices[(j + 1) % part.vertices.length].x, part.vertices[(j + 1) % part.vertices.length].y);
} }
} }
c.lineTo(part.vertices[0].x, part.vertices[0].y); c.lineTo(part.vertices[0].x, part.vertices[0].y);
} }
} }
@ -791,7 +791,7 @@ var Mouse = require('../core/Mouse');
for (j = 1; j < body.vertices.length; j++) { for (j = 1; j < body.vertices.length; j++) {
c.lineTo(body.vertices[j].x, body.vertices[j].y); c.lineTo(body.vertices[j].x, body.vertices[j].y);
} }
c.lineTo(body.vertices[0].x, body.vertices[0].y); c.lineTo(body.vertices[0].x, body.vertices[0].y);
} }
@ -919,7 +919,7 @@ var Mouse = require('../core/Mouse');
for (k = 0; k < part.axes.length; k++) { for (k = 0; k < part.axes.length; k++) {
// render a single axis indicator // render a single axis indicator
c.moveTo(part.position.x, part.position.y); c.moveTo(part.position.x, part.position.y);
c.lineTo((part.vertices[0].x + part.vertices[part.vertices.length-1].x) / 2, c.lineTo((part.vertices[0].x + part.vertices[part.vertices.length-1].x) / 2,
(part.vertices[0].y + part.vertices[part.vertices.length-1].y) / 2); (part.vertices[0].y + part.vertices[part.vertices.length-1].y) / 2);
} }
} }
@ -1094,7 +1094,7 @@ var Mouse = require('../core/Mouse');
c.fill(); c.fill();
c.beginPath(); c.beginPath();
// render collision normals // render collision normals
for (i = 0; i < pairs.length; i++) { for (i = 0; i < pairs.length; i++) {
pair = pairs[i]; pair = pairs[i];
@ -1112,7 +1112,7 @@ var Mouse = require('../core/Mouse');
normalPosX = (pair.activeContacts[0].vertex.x + pair.activeContacts[1].vertex.x) / 2; normalPosX = (pair.activeContacts[0].vertex.x + pair.activeContacts[1].vertex.x) / 2;
normalPosY = (pair.activeContacts[0].vertex.y + pair.activeContacts[1].vertex.y) / 2; normalPosY = (pair.activeContacts[0].vertex.y + pair.activeContacts[1].vertex.y) / 2;
} }
if (collision.bodyB === collision.supports[0].body || collision.bodyA.isStatic === true) { if (collision.bodyB === collision.supports[0].body || collision.bodyA.isStatic === true) {
c.moveTo(normalPosX - collision.normal.x * 8, normalPosY - collision.normal.y * 8); c.moveTo(normalPosX - collision.normal.x * 8, normalPosY - collision.normal.y * 8);
} else { } else {
@ -1219,9 +1219,9 @@ var Mouse = require('../core/Mouse');
continue; continue;
var region = bucketId.split(/C|R/); var region = bucketId.split(/C|R/);
c.rect(0.5 + parseInt(region[1], 10) * grid.bucketWidth, c.rect(0.5 + parseInt(region[1], 10) * grid.bucketWidth,
0.5 + parseInt(region[2], 10) * grid.bucketHeight, 0.5 + parseInt(region[2], 10) * grid.bucketHeight,
grid.bucketWidth, grid.bucketWidth,
grid.bucketHeight); grid.bucketHeight);
} }
@ -1248,7 +1248,7 @@ var Mouse = require('../core/Mouse');
boundsHeight = render.bounds.max.y - render.bounds.min.y, boundsHeight = render.bounds.max.y - render.bounds.min.y,
boundsScaleX = boundsWidth / render.options.width, boundsScaleX = boundsWidth / render.options.width,
boundsScaleY = boundsHeight / render.options.height; boundsScaleY = boundsHeight / render.options.height;
context.scale(1 / boundsScaleX, 1 / boundsScaleY); context.scale(1 / boundsScaleX, 1 / boundsScaleY);
context.translate(-render.bounds.min.x, -render.bounds.min.y); context.translate(-render.bounds.min.x, -render.bounds.min.y);
} }
@ -1268,7 +1268,7 @@ var Mouse = require('../core/Mouse');
// render body selections // render body selections
bounds = item.bounds; bounds = item.bounds;
context.beginPath(); context.beginPath();
context.rect(Math.floor(bounds.min.x - 3), Math.floor(bounds.min.y - 3), context.rect(Math.floor(bounds.min.x - 3), Math.floor(bounds.min.y - 3),
Math.floor(bounds.max.x - bounds.min.x + 6), Math.floor(bounds.max.y - bounds.min.y + 6)); Math.floor(bounds.max.x - bounds.min.x + 6), Math.floor(bounds.max.y - bounds.min.y + 6));
context.closePath(); context.closePath();
context.stroke(); context.stroke();
@ -1302,7 +1302,7 @@ var Mouse = require('../core/Mouse');
context.fillStyle = 'rgba(255,165,0,0.1)'; context.fillStyle = 'rgba(255,165,0,0.1)';
bounds = inspector.selectBounds; bounds = inspector.selectBounds;
context.beginPath(); context.beginPath();
context.rect(Math.floor(bounds.min.x), Math.floor(bounds.min.y), context.rect(Math.floor(bounds.min.x), Math.floor(bounds.min.y),
Math.floor(bounds.max.x - bounds.min.x), Math.floor(bounds.max.y - bounds.min.y)); Math.floor(bounds.max.x - bounds.min.x), Math.floor(bounds.max.y - bounds.min.y));
context.closePath(); context.closePath();
context.stroke(); context.stroke();
@ -1480,7 +1480,7 @@ var Mouse = require('../core/Mouse');
*/ */
/** /**
* A `Bounds` object that specifies the drawing view region. * A `Bounds` object that specifies the drawing view region.
* Rendering will be automatically transformed and scaled to fit within the canvas size (`render.options.width` and `render.options.height`). * Rendering will be automatically transformed and scaled to fit within the canvas size (`render.options.width` and `render.options.height`).
* This allows for creating views that can pan or zoom around the scene. * This allows for creating views that can pan or zoom around the scene.
* You must also set `render.options.hasBounds` to `true` to enable bounded rendering. * You must also set `render.options.hasBounds` to `true` to enable bounded rendering.