简体   繁体   English

Kinetic.js:触摸时旋转图像

[英]Kinetic.js: Rotating image on touch

I have this fiddle: 我有这个小提琴:

http://jsfiddle.net/WDjpx/2/ http://jsfiddle.net/WDjpx/2/

The image is not rotating correctly. 图像旋转不正确。 The code that i used was: 我使用的代码是:

var stage = new Kinetic.Stage({
    container: 'd',
    width: 300,
    height: 300
});

var layer = new Kinetic.Layer();
var isDragging = false;
var refRotation = null;

var rect = new Kinetic.Rect({
    x: 150,
    y: 150,
    width: 100,
    height: 100,
    fill: 'green',
    stroke: 'black',
    strokeWidth: 4,
    offset: [50, 50],
    dragOnTop: true,
    draggable: true,
    dragBoundFunc: function (pos) {

        var xd = 150 - pos.x ;
        var yd = 150 - pos.y ;
        var theta = Math.atan2(yd, xd);
        var deg = theta * 180 / Math.PI;

        if (!isDragging) {
           isDragging = true;
           refRotation = deg;
        } else {
            var rotate = deg - refRotation;
            rect.setRotationDeg(deg);
        }

        return {
            x: this.getAbsolutePosition().x,
            y: this.getAbsolutePosition().y
        }
    }
});

layer.add(rect); layer.add(RECT); stage.add(layer); stage.add(层);

Anyone know what is wrong with my Math??? 谁知道我的数学有什么问题???

--- EDIT --- ---编辑---

New feddle with what i wanted: http://jsfiddle.net/zk9cn/ 新的feddle与我想要的: http//jsfiddle.net/zk9cn/

Don't know about Math.atan2, but it seems calculation of x and y is not right to me. 不知道Math.atan2,但似乎x和y的计算对我来说不对。 I would deduct position from center, not center from position. 我会从中心扣除位置,而不是从位置中心。 http://jsfiddle.net/bighostkim/qZDcg/ http://jsfiddle.net/bighostkim/qZDcg/

    var x = 150 - pos.x;
    var y = 150 - pos.y;
    var radian =  Math.PI + Math.atan(y/x);
    this.setRotation(radian);

Other thing, I also see your variables are not in use; 另外,我也看到你的变量没有被使用; isDragging, refRotate, and rotate. isDragging,refRotate和rotate。

----- Edit ---- -----编辑----

If you want to rotate rectangle by picking up the corner, you can use the following code. 如果要通过拾取角来旋转矩形,可以使用以下代码。 When you pick up the corner, degree calculated by center positon is already 45. That's why you cannot pick up the corner properly. 当你拿起角落时,由中心位置计算的度数已经是45.这就是你不能正确拿起角落的原因。 By adjusting 45 makes it seem right, but when you pick up the straight line, it will go wrong again. 通过调整45使它看起来是正确的,但是当你拿起直线时,它会再次出错。 It seems your requirement has a flaw in it unless it's intentional. 看来你的要求有缺陷,除非是有意的。 http://jsfiddle.net/bighostkim/7Q5Hd/ http://jsfiddle.net/bighostkim/7Q5Hd/

    var pos = stage.getMousePosition();
    var xd = 150 - pos.x ;
    var yd = 150 - pos.y ;
    var theta = Math.atan2(yd, xd);
    var degree = theta / (Math.PI / 180) - 45;
    this.setRotationDeg(degree);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM