使用kineticjs如何将图像对象旋转到位?

时间:2012-07-06 15:39:13

标签: javascript animation kineticjs

考虑以下代码:

var stage = new Kinetic.Stage({
            container : "container",
            width : 600,
            height : 200
        });

var layer = new Kinetic.Layer();


// one revolution per 4 seconds
var angularSpeed = Math.PI / 2;

var imageObj = new Image();
var image = {};
imageObj.onload = function() {
    image = new Kinetic.Image({
                x : 500,
                y : 135,
                image : imageObj,
                width : 99,
                height : 99,
                offsetX: 0,
                offsetY: 0
            });
    image.rotation = 0;

    layer.add(image);
    stage.add(layer);
    stage.onFrame(function(frame) {
                var angleDiff = frame.timeDiff * angularSpeed / 1000;
                image.rotateDeg(angleDiff);

                layer.draw();
            });
                stage.start();


};
imageObj.src = "images/tire-brands.png";

如何使图像旋转到位,如360度,但枢轴点位于中心?

因此,当我制作图像对象时,目标是在那里运行动画。 目前它只在一侧旋转图像。

2 个答案:

答案 0 :(得分:5)

你需要使用offset属性(不确定你从哪里获得offsetX和offsetY - 旧版本的KinectJS?):

image = new Kinetic.Image({
            x : 500,
            y : 135,
            image : imageObj,
            width : 99,
            height : 99,
            offset: [50, 50]
        });

答案 1 :(得分:3)

function rotate(angle){ // 90 or -90
    if(stage.getHeight() <= image.getWidth()){ 
        aspect = image.getWidth() / image.getHeight();
        height = stage.getHeight() / aspect;
        image.setWidth(stage.getHeight());
        image.setHeight(height);
    }
    image.setOffset(image.getWidth()/2,image.getHeight()/2);
    image.setPosition(stage.getWidth()/2,stage.getHeight()/2);
    image.rotateDeg(angle);
    layer.draw();
}

以上代码帮助我旋转图像