svg - SnapSVG drag rotated rectangle -


i've managed drag rectangle taking account mouse location using following code (demonstrated small square)

when rectangle rotated rectangle dragged in parallel shape, please show me how correct this. thought done bit of trigonometry haven't been successful calculate opposite (x) , adjacent(y)

a demo can seen here http://jsbin.com/tihobu/2/edit?html,js,output

var s = snap(400,400); var smallsquare = s.rect(100, 100, 50,50).attr({fill:"#ffcc00"}); var bigsquare = s.rect(100,20,150,150).attr({fill:"#ff6600"});  var startx, starty;   var t = bigsquare.transform().localmatrix;     t.rotate(45); bigsquare.transform(t);  var movefunc = function (dx, dy, posx, posy) {    var xadj = startx - (0 - dx);    var yadj = starty - (0 - dy);    this.attr('x',xadj);    this.attr('y',yadj); };  var startfunc = function(){                 startx = this.attr('x');                 starty = this.attr('y');                 console.log("move started");             }; var stopfunc = function(){};  bigsquare.drag( movefunc,startfunc,stopfunc ); smallsquare.drag( movefunc,startfunc,stopfunc ); 

thanks david

the problem coordinate space has been rotated, have couple of options. either work out new x,y be, or use drag transforms rather changing x,y. use transforms, there may reasons why want change x,y attributes.

note, if keep on working changing x,y attributes, have added issues not elements have this. eg circles positioned cx, cy, may have other complex elements.

here code used make transform handler...(edit: have changed code cope complex groups)

example

   snap.plugin( function( snap, element, paper, global ) {      element.prototype.globaltolocal = function( globalpoint ) {             var globaltolocal = this.node.gettransformtoelement( this.paper.node ).inverse();             globaltolocal.e = globaltolocal.f = 0;             return globalpoint.matrixtransform( globaltolocal );     };      element.prototype.getcursorpoint = function( x, y ) {             var pt = this.paper.node.createsvgpoint();                   pt.x = x; pt.y = y;             return pt.matrixtransform( this.paper.node.getscreenctm().inverse());      }      element.prototype.altdrag = function() {             return this.drag( altmovedrag, altstartdrag );     };      function altmovedrag( xxdx, xxdy, ax, ay ) {             var tdx, tdy;             var cursorpoint = this.getcursorpoint( ax, ay );             var pt = this.paper.node.createsvgpoint();              pt.x = cursorpoint.x - this.data('op').x;             pt.y = cursorpoint.y - this.data('op').y;              var localpt = this.globaltolocal( pt );              this.transform( this.data('ot').totransformstring() + "t" + [  localpt.x, localpt.y ] );      };      function altstartdrag( x, y, ev ) {             this.data('ibb', this.getbbox());             this.data('op', this.getcursorpoint( x, y ));             this.data('ot', this.transform().localmatrix);     }; 

});


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -