javascript - How to draw line on drag using mouse coordinates -
i able mouse coordinates on mousedown , mouseup events. want use these coordinates draw line on mousemove(drag). tried move,start,up didn't work left blank now.
here's jsbin demo: https://jsbin.com/kuhisesede/edit?html,console,output
there's no click event in raphael out using element. mean cannot coordinates of screen in raphael click event. click has associated element rectangle or circle coordinates.
now able coordinates, how draw line on mouse drag/mouse move
any ideas if body implemented it?
code:
<!doctype html> <html> <head> <title>editor</title> <meta http-equiv="x-ua-compatible" content="ie=9"/> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.4/raphael-min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript"> window.onload = function () { var paper = new raphael(raphael("container", "100%", "100%")); var line = paper.path(); //get coordinates $("svg").mousedown(function (e) { console.log("mouse down: " + e.offsetx + " " + e.offsety); var x = e.offsetx; var y = e.offsety; line = paper.path("m" + x + "," + y); }); $("svg").mouseup(function (e) { console.log("mouse up: " + e.offsetx + " " + e.offsety); var x = e.offsetx; var y = e.offsety; line = paper.path("l" + x + "," + "l" + y); }); paper.set(line); line.drag(move,start,up); var start = function (x, y, event) { this.ox = this.attr("x"); this.oy = this.attr("y"); }, move = function (dx, dy) { this.attr({ x1: this.x + dx, y1: this.x + dy }); }, = function () { }; }; </script> </head> <body> <div id="container"> <div id="header" style="margin-bottom: 0;"> <h1 id="title">editor</h1> <div id="footer"></div> </div> </div> </body> </html>
i hope (or else).
<!doctype html> <html> <head> <title>editor</title> <meta http-equiv="x-ua-compatible" content="ie=9"/> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.4/raphael-min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript"> var paper; $(function(){ paper = new raphael("container", "100%", "100%"); c = paper.circle(50, 50, 40).attr({fill: "#29ecc7", stroke: "none", opacity: .5}); startpoint = {} endpoint = {} startdragfunction = function(x, y, e) { startpoint.x = x; startpoint.y = y; } enddragfunction = function(x, y, e) { paper.path("m"+ startpoint.x +","+ startpoint.y +"l"+ (startpoint.x+endpoint.x) +","+(startpoint.y+endpoint.y)); } draggingfunction = function(x, y) { endpoint.x = x; endpoint.y = y; } paper.set(c).drag(draggingfunction, startdragfunction, enddragfunction); }); </script> </head> <body> <div id="container"> <div id="header" style="margin-bottom: 0;"> <h1 id="title">editor</h1> <div id="footer"></div> </div> </div> </body> </html> a few important things note:
- if want able drag element has filled color (otherwise wont able drag it)
- i didn't handle dragging or element, drawing of line.
- the "drop" function (enddragfunction) indicate element dropped. has no information regarding drop-point. use draggingfunction.
- the x, y values of dragging function diff original point.
Comments
Post a Comment