【问题标题】:Using the kineticjs how to rotate an image object in place?使用 kineticjs 如何将图像对象旋转到位?
【发布时间】:2013-10-24 16:58:35
【问题描述】:

考虑以下代码:

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

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

【问题讨论】:

    标签: javascript animation kineticjs


    【解决方案1】:

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

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

    【讨论】:

    • 添加偏移量后,setPosition() 行为无法按预期工作,它将图像从偏移量而不是从左上角定位
    【解决方案2】:
    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();
    }
    

    以上代码帮助我旋转图像

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-18
      • 2021-12-23
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多