0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-12-17 12:42:17 -05:00
liabru-matter-js/examples/views.js

191 lines
5.9 KiB
JavaScript
Raw Normal View History

2016-11-20 19:41:57 -05:00
var Example = Example || {};
2015-08-25 14:12:52 -04:00
2016-11-20 19:41:57 -05:00
Example.views = function() {
var Engine = Matter.Engine,
Render = Matter.Render,
Runner = Matter.Runner,
Events = Matter.Events,
2015-08-25 14:12:52 -04:00
Composites = Matter.Composites,
Common = Matter.Common,
2016-11-20 19:41:57 -05:00
MouseConstraint = Matter.MouseConstraint,
Mouse = Matter.Mouse,
Composite = Matter.Composite,
2015-08-25 14:12:52 -04:00
Vector = Matter.Vector,
2016-11-20 19:41:57 -05:00
Bounds = Matter.Bounds,
Bodies = Matter.Bodies;
// create engine
var engine = Engine.create(),
world = engine.world;
// create renderer
var render = Render.create({
element: document.body,
engine: engine,
options: {
width: 800,
height: 600,
2016-11-20 19:41:57 -05:00
hasBounds: true,
showAngleIndicator: true
}
});
Render.run(render);
// create runner
var runner = Runner.create();
Runner.run(runner, engine);
// add mouse control
var mouse = Mouse.create(render.canvas),
mouseConstraint = MouseConstraint.create(engine, {
mouse: mouse,
constraint: {
stiffness: 0.2,
render: {
visible: false
2015-08-25 14:12:52 -04:00
}
}
});
Composite.add(world, mouseConstraint);
2015-08-25 14:12:52 -04:00
2016-11-20 19:41:57 -05:00
// keep the mouse in sync with rendering
render.mouse = mouse;
2015-08-25 14:12:52 -04:00
2016-11-20 19:41:57 -05:00
// add bodies
var stack = Composites.stack(20, 20, 10, 4, 0, 0, function(x, y) {
2016-11-20 19:41:57 -05:00
switch (Math.round(Common.random(0, 1))) {
2015-08-25 14:12:52 -04:00
2016-11-20 19:41:57 -05:00
case 0:
if (Common.random() < 0.8) {
return Bodies.rectangle(x, y, Common.random(20, 50), Common.random(20, 50));
} else {
return Bodies.rectangle(x, y, Common.random(80, 120), Common.random(20, 30));
}
case 1:
var sides = Math.round(Common.random(1, 8));
sides = (sides === 3) ? 4 : sides;
return Bodies.polygon(x, y, sides, Common.random(20, 50));
}
});
Composite.add(world, [
2016-11-20 19:41:57 -05:00
stack,
2016-11-27 20:13:22 -05:00
// walls
Bodies.rectangle(400, 0, 800, 50, { isStatic: true }),
2016-11-20 19:41:57 -05:00
Bodies.rectangle(400, 600, 800, 50, { isStatic: true }),
Bodies.rectangle(800, 300, 50, 600, { isStatic: true }),
Bodies.rectangle(0, 300, 50, 600, { isStatic: true })
]);
// get the centre of the viewport
var viewportCentre = {
x: render.options.width * 0.5,
y: render.options.height * 0.5
};
2015-08-25 14:12:52 -04:00
// create limits for the viewport
var extents = {
min: { x: -300, y: -300 },
max: { x: 1100, y: 900 }
};
2016-11-20 19:41:57 -05:00
// keep track of current bounds scale (view zoom)
var boundsScaleTarget = 1,
boundsScale = {
x: 1,
y: 1
};
2015-08-25 14:12:52 -04:00
// use a render event to control our view
Events.on(render, 'beforeRender', function() {
2016-11-20 19:41:57 -05:00
var world = engine.world,
mouse = mouseConstraint.mouse,
translate;
// mouse wheel controls zoom
var scaleFactor = mouse.wheelDelta * -0.1;
if (scaleFactor !== 0) {
if ((scaleFactor < 0 && boundsScale.x >= 0.6) || (scaleFactor > 0 && boundsScale.x <= 1.4)) {
boundsScaleTarget += scaleFactor;
}
}
// if scale has changed
if (Math.abs(boundsScale.x - boundsScaleTarget) > 0.01) {
// smoothly tween scale factor
scaleFactor = (boundsScaleTarget - boundsScale.x) * 0.2;
boundsScale.x += scaleFactor;
boundsScale.y += scaleFactor;
// scale the render bounds
render.bounds.max.x = render.bounds.min.x + render.options.width * boundsScale.x;
render.bounds.max.y = render.bounds.min.y + render.options.height * boundsScale.y;
// translate so zoom is from centre of view
translate = {
x: render.options.width * scaleFactor * -0.5,
y: render.options.height * scaleFactor * -0.5
};
2015-08-25 14:12:52 -04:00
2016-11-20 19:41:57 -05:00
Bounds.translate(render.bounds, translate);
2015-08-25 14:12:52 -04:00
2016-11-20 19:41:57 -05:00
// update mouse
Mouse.setScale(mouse, boundsScale);
Mouse.setOffset(mouse, render.bounds.min);
}
2015-08-25 14:12:52 -04:00
2016-11-20 19:41:57 -05:00
// get vector from mouse relative to centre of viewport
var deltaCentre = Vector.sub(mouse.absolute, viewportCentre),
centreDist = Vector.magnitude(deltaCentre);
2015-08-25 14:12:52 -04:00
2016-11-20 19:41:57 -05:00
// translate the view if mouse has moved over 50px from the centre of viewport
if (centreDist > 50) {
// create a vector to translate the view, allowing the user to control view speed
var direction = Vector.normalise(deltaCentre),
speed = Math.min(10, Math.pow(centreDist - 50, 2) * 0.0002);
2015-08-25 14:12:52 -04:00
2016-11-20 19:41:57 -05:00
translate = Vector.mult(direction, speed);
2015-08-25 14:12:52 -04:00
// prevent the view moving outside the extents
if (render.bounds.min.x + translate.x < extents.min.x)
translate.x = extents.min.x - render.bounds.min.x;
2015-08-25 14:12:52 -04:00
if (render.bounds.max.x + translate.x > extents.max.x)
translate.x = extents.max.x - render.bounds.max.x;
2015-08-25 14:12:52 -04:00
if (render.bounds.min.y + translate.y < extents.min.y)
translate.y = extents.min.y - render.bounds.min.y;
2015-08-25 14:12:52 -04:00
if (render.bounds.max.y + translate.y > extents.max.y)
translate.y = extents.max.y - render.bounds.max.y;
2015-08-25 14:12:52 -04:00
2016-11-20 19:41:57 -05:00
// move the view
Bounds.translate(render.bounds, translate);
2015-08-25 14:12:52 -04:00
2016-11-20 19:41:57 -05:00
// we must update the mouse too
Mouse.setOffset(mouse, render.bounds.min);
}
});
2015-08-25 14:12:52 -04:00
2016-11-20 19:41:57 -05:00
// context for MatterTools.Demo
return {
engine: engine,
runner: runner,
render: render,
canvas: render.canvas,
stop: function() {
Matter.Render.stop(render);
Matter.Runner.stop(runner);
}
2015-08-25 14:12:52 -04:00
};
2019-09-14 13:54:50 -04:00
};
2021-02-07 18:56:08 -05:00
Example.views.title = 'Views';
2020-12-30 18:18:38 -05:00
Example.views.for = '>=0.14.2';
if (typeof module !== 'undefined') {
2020-12-30 18:18:38 -05:00
module.exports = Example.views;
}