JavaScript SVG how to get the mouse position on the internal matrix

jsFiddle: http://jsfiddle.net/az6Ug/

Using Snap.svg library, I want drag on the SVG and get the mouse position. I need the mouse position on the internal matrix, not the browser mouse position. I have this working with one problem, which is if there is any scroll on the window that the SVG belongs to, the calculated mouse position is offset by the amount of scroll on the scrollbar.

For example, with no scroll then it works fine. Or with 10px of vertical scroll on the scrollbar, the mouse position is calculated as: true position + 10px. It's the same deal if there's any horizontal scroll: offset by the amount of scroll.

In the jsFiddle, I use a rectangle to show the calculared mouse position while dragging. As you can see if there's no scroll, then the rectangle stays with the mouse cursor (upper left corner). But with some scroll, the rectangle is offset from the mouse cursor.

Although I'm using the Snap.svg library, I only use this to get the drag event, the mouse calculation is pure Javascript. A possible solution is to subtract the X and Y by the amount of scroll but I think there will be a better way using the SVG's tranform functions.

var S;
var pt;
var svg
var box;

window.onload = function(){
    svg = $('#mysvg')[0];
    S = Snap(svg);

    pt = pt = svg.createSVGPoint(); // create the point

    // add the rectangle
    box = S.rect(10, 10, 50, 50);
    box.attr({ fill : 'red', stroke : 'none' });

    S.drag(
        function(dx, dy, posX, posY, e){
            //onmove
            pt.x = posX;
            pt.y = posY;

            // convert the mouse X and Y so that it's relative to the svg element
            var transformed = pt.matrixTransform(svg.getScreenCTM().inverse());
            box.attr({ x : transformed.x, y : transformed.y });
        },
        function(){
            //onstart
        },
        function(){
            //onend
        }
    );

}
Answer:1

Maybe you could just use getCTM rather than getScreenCTM, so...

var transformed = pt.matrixTransform(svg.getCTM().inverse());

and I think you may need to adjust with something like

pt.x = posX - S.node.offsetLeft;
pt.y = posY - S.node.offsetTop;

jsfiddle here

Update: as above doesn't work in Firefox, you could take the clientX/Y with the original getScreenCTM, so

pt.x = e.clientX
pt.y = e.clientY;

fiddle here

I'm guessing you need more than just the example here though (in which case you could just use Snaps own drag funcs), so its difficult to tell if this solution will work for other cases you need.

Answer:2

I am still newbie to integrate elfinder and tinymce, i already follow tutorial in elfinder wiki in form.php in tinymce init already added file_browser_callback:elFinderBrowser: tinyMCE.init({ ...

I am still newbie to integrate elfinder and tinymce, i already follow tutorial in elfinder wiki in form.php in tinymce init already added file_browser_callback:elFinderBrowser: tinyMCE.init({ ...

  1. integrate elfinder with codeigniter

For example, i have an array like this: var arr = [ {"keyword": "Sample 1", "item": {"title":"Sample Title", "url": "/sample"}}, {"keyword": "Foo 1", "item": {"title":"Foo Title", "url": "/...

For example, i have an array like this: var arr = [ {"keyword": "Sample 1", "item": {"title":"Sample Title", "url": "/sample"}}, {"keyword": "Foo 1", "item": {"title":"Foo Title", "url": "/...

  1. javascript search object for value
  2. javascript search object for key
  3. javascript search object array
  4. javascript search object for property
  5. javascript search object for string
  6. javascript search object array by property
  7. javascript search object for key value
  8. javascript search object array for value
  9. javascript search object recursively
  10. javascript search object property value

Following regex replaces " >" or " > " or "> " character with "/". tempKeyword = tempKeyword.replace(/( > )|( >)|(> )/g,'/'); How can I make this better? Thanks!

Following regex replaces " >" or " > " or "> " character with "/". tempKeyword = tempKeyword.replace(/( > )|( >)|(> )/g,'/'); How can I make this better? Thanks!

I want to catch all key presses on my webpage so that when you start typing the text appears in the search box (input) and this box gets focus. Can someone provide me some hints?

I want to catch all key presses on my webpage so that when you start typing the text appears in the search box (input) and this box gets focus. Can someone provide me some hints?