2

考虑以下代码:

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 度,但枢轴点在中心?

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

4

2 回答 2

5

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

image = new Kinetic.Image({
            x : 500,
            y : 135,
            image : imageObj,
            width : 99,
            height : 99,
            offset: [50, 50]
        });
于 2012-07-19T08:24:02.000 回答
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();
}

上面的代码帮助我旋转图像

于 2013-03-27T09:20:35.867 回答