Git Product home page Git Product logo

Comments (6)

liabru avatar liabru commented on May 5, 2024 1

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.

liabru avatar liabru commented on May 5, 2024

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.

abataille avatar abataille commented on May 5, 2024

Great.

from matter-js.

abataille avatar abataille commented on May 5, 2024

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.

liabru avatar liabru commented on May 5, 2024

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.

df257 avatar df257 commented on May 5, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.