Open lilrichan opened 1 year ago
it's a nice little short around, but what I did and what you could do is take the position of your mouse to calculate where the dropped element should be snapped to, so after dropping it, get it inside a container which will be your interactable object, and then add a transform translate to the container, with the values you calculated earlier for the 'real' position on screen for you object to be 'snapped' to, you can also use that method to just snap it to it, by saying 'real' position I mean the position on screen that you're mouse at but relative to the element where you drop or contain all of your interactable elements at.
to do that calculation, you will check your interactable elements container offsets or the left and top attributes with parentContainer.getBoundingClientRect(),
and you will do
droppedElementX = event.dx - parentContainer.getBoundingClientRect().left
droppedElementY = event.dy - parentContainer.getBoundingClientRect().top
which will give you the positions you want to set your dropped elements too,
I'm not sure about snapping cause I've just started dealing with it myself, but I think you could then set the modifier on drop
interact.modifiers.snap({ targets: [{droppedElementX, droppedElementY}], relativePoints: [{ x: 0.5, y: 0.5 }] })
and like it said in the docs -
interact(element).draggable({ modifiers: [ interact.modifiers.snap({ targets: [ { x: 300, y: 300 } ], relativePoints: [ { x: 0 , y: 0 }, // snap relative to the element's top-left, { x: 0.5, y: 0.5 }, // to the center { x: 1 , y: 1 } // and to the bottom-right ] }) ] })
so setting the relativePoints to { x: 0.5, y: 0.5 } should position it from the center. https://interactjs.io/docs/snapping/
I'm sorry if this sounds troublesome as I feel this question has been asked many times or this could possibly be a duplicate.
I'm actually working on a click and drag bunny rabbit dissection game and what's supposed to happen is when I place one of the divs within the transparent dropzone, it should snap back into it's original position.
There is only one dropzone that spans all the way from the top of the rabbit's head (including the little hair) all the way down to the bottom of the torso. All draggable elements are positioned relatively using left and bottom.
The issue is, I've tried every method I found on here and none of them have worked to my expectations, I have a feeling it's due to the way my website is structured as it depends on HTML elements rather than a JavaScript canvas.
The game is here, feel free to mess around in Inspect Element=> https://yumekawahanachi.neocities.org/dissectbunny Be warned as there are elements of light gore, no jumpscares or screamers though.
Methods I've tried => results:
79 => Nothing Happens
265 => Snaps to a random corner of the HTML document
265 combined with #79 => Snaps to a random corner of the HTML document
I'm still very new to JavaScript as a whole and I'm coming here from HTML and CSS, which is why my game is based on HTML elements.
My code as of writing this looks like this: