0

我想计算阻力的长度。即通过在html画布上将光标从一个点拖动到另一个点创建的线的长度。如何在 kineticjs 中实现它?即在dragstart 到dragend。

4

2 回答 2

1

我不知道动力学,但使用 jquery 看起来像这样:

var telemetry = {
    $target: null,
    startPosition: {x:0,y:0},
    distance: 0,
    getMousePosition: function(event){
        var position = {
            x: event.pageX - this.$target.offset().left,
            y: event.pageY - this.$target.offset().top
        }
        return position;
    },
    getDistance: function(startPosition, endPosition){
        //find distance in each x and y directions
        var dx = endPosition.x - startPosition.x;
        var dy = endPosition.y - startPosition.y;

        // use pythagorean theorem
        return Math.sqrt((dx*dx) + (dy*dy));
    },
    onMouseDown: function(event){
        this.startPosition = this.getMousePosition(event);
    },
    onMouseUp: function(event){
        this.distance = this.getDistance(this.startPosition, this.getMousePosition(event));
    }
}

telemetry.$target = $('#myCanvas');

telemetry.$target.mousedown(function(event){
    telemetry.onMouseDown(event);
}).mouseup(function(event){
    telemetry.onMouseUp(event);
    alert('you dragged ' + telemetry.distance + 'px');
});

http://jsfiddle.net/gunderson/HUdUH/

于 2013-02-25T07:11:41.727 回答
0

好吧,在 kineticjs 你会做这样的事情:

var point1;
var point2;
layer.on('dragstart', function(){
     point1 = stage.getUserPosition();
});
layer.on('dragend', function(){
     point2 = stage.getUserPosition();
     var xDist = point2.x - point1.x;
     var yDist = point2.y - point1.y;
     alert( Math.sqrt((xDist*xDist) + (yDist*yDist)) ); // this pops up a message of the length of the line, you could just do a return or assign a value to something
     //distance = Math.sqrt((xDist*xDist) + (yDist*yDist)); //alternative
})
于 2013-02-25T20:10:20.397 回答