Comments (6)
That won't work, probably the easiest way would be the startdrag event on a MouseConstraint
though I'd suggest looking at the source to see how it works.
from matter-js.
An event system is now available in the Matter.js edge build, under the namespace Matter.Events
.
See the events demo of the event system in use.
Usage is generally:
Matter.Events.on(object, eventNames, callback)
E.g.
Matter.Events.on(myEngine, 'collisionStart', function(event) {
var pairs = event.pairs;
// do something with the pairs that have started collision
});
A full example of usage is shown in the latest demo code.
Currently only the Engine has defined events, which include tick
and collisionStart
, collisionActive
, collisionEnd
among others.
See the API Docs (edge) for more info, specifically Events and Engine Events.
This system is very new, so please log any issues you find, cheers.
from matter-js.
Great.
from matter-js.
I suggest another event, when the mouse touched a body.
Here is what I added to:
MouseConstraint.update = function(mouseConstraint, bodies) {
var mouse = mouseConstraint.mouse,
constraint = mouseConstraint.constraints[0];
if (mouse.button === 0 || mouse.button === 2) {
if (!constraint.bodyB) {
for (var i = 0; i < bodies.length; i++) {
var body = bodies[i];
if (Bounds.contains(body.bounds, mouse.position) && Vertices.contains(body.vertices, mouse.position)) {
constraint.pointA = mouse.position;
constraint.bodyB = body;
constraint.pointB = { x: mouse.position.x - body.position.x, y: mouse.position.y - body.position.y };
constraint.angleB = body.angle;
Sleeping.set(body, false);
----> Events.trigger(_engine, 'bodytouched', body);
}
}
}
} else {
constraint.bodyB = null;
constraint.pointB = null;
}
if (constraint.bodyB) {
Sleeping.set(constraint.bodyB, false);
constraint.pointA = mouse.position;
}
};
from matter-js.
The event system is now included in the latest release:
https://github.com/liabru/matter-js/releases/tag/0.7.0-alpha
If you still need to detect if the mouse has clicked on a body, see here for reference:
https://github.com/liabru/matter-js/blob/master/demo/js/Demo.js#L837
This is the demo code that allows body removal by right clicking in the demo.
Since the event system is now in place, I will close this thread.
Any new issues should be opened in a new thread, cheers.
from matter-js.
I want use event like this:
var circleA = Bodies.circle(100, 80, 50, null, 20)
Matter.Events.on(circleA , 'mousedown', function(e) {
console.log(111);
});
Can it be achieved?
from matter-js.
Related Issues (20)
- How to suppress body tremors? HOT 2
- Why does the attractor only work on one of the two BODYs? HOT 1
- Using compound bodies with Bodies.fromVertices does not work it just shows a filled in shape. HOT 1
- Why don't bodies sometimes collide? HOT 1
- Constraints with distance limits (e.g. for ropes)
- Why might there be such an error? HOT 1
- Simple Polygon Physics Issues HOT 1
- How to add project to the Wiki gallery ?
- How to scale the world? HOT 2
- [Phaser 3] Position of matter sprite's image not correct in container HOT 1
- loss of sprite quality on mobile HOT 1
- `Composite.translate` didn't move the bodies included
- Create Matter.Bodies.fromVertices with rounded vertices
- MouseConstraint preventing clicks on buttons on mobile. HOT 2
- Vulnerability related to the formula of universal gravitation
- Body with high mass and low inertia behaves erratically
- The startdrag event of dragging a body does not trigger after losing focus.
- How to move camera in matter.js world?
- How to use matter.js on a web worker?
- Moving objects penetrate each other. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from matter-js.