1

以下のようなHTML5サークル描画の例があります

http://i.stack.imgur.com/SVPO9.jpg

これの描画スクリプトは次のとおりです(HTML5およびjQuery)

http://jsfiddle.net/eGjak/275/

$.each(circles, function() {
   drawCircle(this);
   drawLine(mainCircle, this);
});

これをドラッグアンドドロップにアップグレードする必要があります(ユーザーは子の円をメインの円の周りに線でドラッグできます)

html5、css3、jQueryを使用してこれを行うにはどうすればよいですか?

4

1 に答える 1

2

http://jsfiddle.net/eGjak/503/

キャンバス上でローカルのxとyを見つけてから、距離を計算し(ピタゴラスの定理、aSquared + bSquare = cSquaredと考えてください)、距離が円の半径よりも小さいかどうかを確認する必要があります(別名マウスは円内にあります)。

現在お持ちのコードの後に​​このコードを追加してください

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 < 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;
}

これを行う方法と速度を節約する方法は他にもありますが、これでうまくいくはずです。

于 2012-06-09T23:22:24.623 に答える