Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to limit the draggable area in snap.svg? #650

Closed
kevinG73 opened this issue Jun 10, 2021 · 11 comments
Closed

how to limit the draggable area in snap.svg? #650

kevinG73 opened this issue Jun 10, 2021 · 11 comments

Comments

@kevinG73
Copy link


I am working on a puzzle game and I use snap.svg and javascript to move the elements of the game . But the problem is that any user can drag the element of svg anywhere on the screen, including out of the droppable area. I want to prevent it , I found some resources on internet about snap.svg but it doesn't like i want

I share the code here :
https://jsfiddle.net/j058aoyn/2/

my script :

       var s = Snap("#mysvg");
        var dragging = false;
        var tmp = null
        
        Snap.load("puzzle.svg", onSVGLoaded)
        
        function onSVGLoaded(frag) {
            s.append(frag);
            s.mouseover(clickDrag)
        }
        
        function clickDrag(e) {
            tmp = e.target.parentNode.getAttribute('index');
            if (tmp != null) {
                var t = Snap(e.target);
                t.drag();
            }
            if (t !== undefined && (t.data('dragEl') === undefined) && (dragging === false)) {
                t.drag(dragMove, dragStart, dragStop).data('dragEl', t);
            }
        }
        
        function dragStart(x, y, e) {
            this.data('targetPiece', e.target);
        }
        
        function dragMove(dx, dy, x, y, e) {
            if (tmp !== this.data('targetPiece')) {
                return
            }
            // Check the event isn't coming from a different state
            this.data('dragEl').attr({cx: x - 10, cy: y - 10})
            dragging = true;
        }
        
        function dragStop() {
        }
@ibrierley
Copy link
Collaborator

If you look here, I did a plugin for it... http://svg.dabbles.info/snaptut-drag-limittransform

@kevinG73
Copy link
Author

@ibrierley i tried to use this plugin but it doesn't work with my projet , the code I posted here is just a quick snapshot .
I don't know if there are other method without use viewbox

@ibrierley
Copy link
Collaborator

Can you clarify "doesnt work", do you get an error or something ? Can you post a jsfiddle ?

@kevinG73
Copy link
Author

https://jsfiddle.net/f3gjw576/3/

@ibrierley i don't know why i can't move any where in the area of the viewbox , can you chekout ?

@ibrierley
Copy link
Collaborator

ibrierley commented Jun 10, 2021 via email

@kevinG73
Copy link
Author

I do not understand what you mean ?
I can always drag and drop even on the code I sent you , it's just how to limit the drag and drop that i look for

@ibrierley
Copy link
Collaborator

The drag and drop on the original doesn't work correctly. If you drag it 100px, it only moves 50px (roughly).

@ibrierley
Copy link
Collaborator

Btw you can probably hack/fix it like the example, but you may find you have later issues with different setups, not sure. If you don't mind that, then you just need to add basic collision detection in your move handler.

@kevinG73
Copy link
Author

I worked on it , but i have still the same issue
https://jsfiddle.net/ncL94f5v/3/

@ibrierley
Copy link
Collaborator

Sorry, I haven't had chance to look at this, and may not for a while currently. The transform stuff I did back in those days may be overcomplicated stuff, I'm really not sure without a bit of time to sit down and check if it really should work for this case actually...I note this old (potentially buggier if nested transforms are used) but simpler plugin version feels better possibly... http://svg.dabbles.info/snaptut-drag-limit it feels like it just needs bounding check offset minus the starting position or something.

Sorry I haven't had any real time to check, I like this stuff, it's possible I may get a bit more time in the future, but I can't guarantee anything atm.

@kevinG73
Copy link
Author

thank you , it works now , problem solved . @ibrierley

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants