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

added scale support to Render

This commit is contained in:
liabru 2014-05-04 12:37:45 +01:00
parent 0c107b21ed
commit 1f8ee5ccca

View file

@ -132,6 +132,11 @@ var Render = {};
context.globalCompositeOperation = 'source-over'; context.globalCompositeOperation = 'source-over';
// handle bounds // handle bounds
var boundsWidth = render.bounds.max.x - render.bounds.min.x,
boundsHeight = render.bounds.max.y - render.bounds.min.y,
boundsScaleX = boundsWidth / render.options.width,
boundsScaleY = boundsHeight / render.options.height;
if (options.hasBounds) { if (options.hasBounds) {
// filter out bodies that are not in view // filter out bodies that are not in view
for (i = 0; i < allBodies.length; i++) { for (i = 0; i < allBodies.length; i++) {
@ -158,7 +163,8 @@ var Render = {};
constraints.push(constraint); constraints.push(constraint);
} }
// translate the view // transform the view
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);
} else { } else {
constraints = allConstraints; constraints = allConstraints;
@ -199,8 +205,10 @@ var Render = {};
if (options.showDebug) if (options.showDebug)
Render.debug(engine, context); Render.debug(engine, context);
if (options.hasBounds) if (options.hasBounds) {
context.translate(render.bounds.min.x, render.bounds.min.y); // revert view transforms
context.setTransform(1, 0, 0, 1, 0, 0);
}
}; };
/** /**
@ -751,8 +759,15 @@ var Render = {};
options = render.options, options = render.options,
bounds; bounds;
if (options.hasBounds) if (options.hasBounds) {
var boundsWidth = render.bounds.max.x - render.bounds.min.x,
boundsHeight = render.bounds.max.y - render.bounds.min.y,
boundsScaleX = boundsWidth / render.options.width,
boundsScaleY = boundsHeight / render.options.height;
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);
}
for (var i = 0; i < selected.length; i++) { for (var i = 0; i < selected.length; i++) {
var item = selected[i].data; var item = selected[i].data;
@ -812,7 +827,7 @@ var Render = {};
} }
if (options.hasBounds) if (options.hasBounds)
context.translate(render.bounds.min.x, render.bounds.min.y); context.setTransform(1, 0, 0, 1, 0, 0);
}; };
/** /**