Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://jsfiddle.net/eGjak/503/" rel="nofollow">http://jsfiddle.net/eGjak/503/</a></p> <p>You have to find the local x and y on the canvas, then calculate the distance ( think pythagorean theorem, aSquared + bSquare = cSquared ), and see if the distance is less than the radius of the circle( aka mouse is within the circle )</p> <p>Add this code after the code you currently have</p> <pre><code>var focused_circle, lastX, lastY ; function test_distance( n, test_circle ){ //see if the mouse is clicking circles var dx = lastX - test_circle.x, dy = lastY - test_circle.y; //see if the distance between the click is less than radius if( dx * dx + dy * dy &lt; test_circle.r * test_circle.r ){ focused_circle = n; $(document).bind( 'mousemove.move_circle' , drag_circle ); $(document).bind( 'mouseup.move_circle' , clear_bindings); return false; // in jquery each, this is like break; stops checking future circles } } $('#cv').mousedown( function( e ){ lastX = e.pageX - $(this).offset().left; lastY = e.pageY - $(this).offset().top; $.each( circles, test_distance ); }); function drag_circle( e ){ var newX = e.pageX - $('#cv').offset().left, newY = e.pageY - $('#cv').offset().top; //set new values circles[ focused_circle ].x += newX - lastX; circles[ focused_circle ].y += newY - lastY; //remember these for next time lastX = newX, lastY = newY; //clear canvas and redraw everything ctx.clearRect( 0, 0, ctx.canvas.width, ctx.canvas.height ); drawCircle(mainCircle); $.each(circles, function() { drawCircle(this); drawLine(mainCircle, this); }); } function clear_bindings( e ){ // mouse up event, clear the moving and mouseup bindings $(document).unbind( 'mousemove.move_circle mouseup.move_circle' ); focused_circle=undefined; } </code></pre> <p>There are other ways to do this and ways to save on speed, though this should do the trick.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload