javascript - 如何在容器中心旋转我的补间

标签 javascript animation createjs tween

我有一个 276*266 的容器,我想在这个容器的中心旋转一个对象,因为我正在使用这段代码:

    createjs.Tween.get(element, {loop: false})
    .to({regY:element.height/2, regX:element.width/2,x:element.x,y:element.y,rotation: 360}, 1000)
    .to({regY:element.height/2, regX:element.height/2,x:element.x,y:element.y,rotation: 0}, 1000)
    .to({alpha: 0}, 500, createjs.Ease.getPowInOut(2))
    .to({alpha: 1}, 500, createjs.Ease.getPowInOut(2));

但它总是在容器的左上角旋转 有人知道为什么我要怎么做才能在容器中间旋转它吗?

最佳答案

这很简单:

  1. 将对象的 x 和 y 位置设置为容器宽度和高度的 50%。
  2. 将内容的 regX 和 regY 设置为其宽度和高度的 50%。

请注意,您不能使用 .width.height 来获取这些值。如果内容是位图、 Sprite 、文本或具有这些类型内容的容器,您可以使用 getBounds()获取宽度/高度和从0开始的偏移量。如果内容是Graphics/Shape,则无法自动获取,必须自己指定大小。

var container = new createjs.Container();
stage.addChild(container);
var containerWidth = 100;
var containerHeight = 100;

// Draw some "bounds" on the container. This is just for the visual, 
// and does not actually affect the bounds, since it can not be calculated.
var bounds = new createjs.Shape();
bounds.graphics.f("#eee").dr(0,0,containerWidth,containerHeight);
container.addChild(bounds);

// Draw the contents. Note this is drawn with the registration point at the top left to illustrate the regX/regY at work.
var shape = new createjs.Shape();
shape.graphics.f("#f00").drawRect(0,0,50,50);
container.addChild(shape);

// Center the shape
shape.x = containerWidth/2;
shape.y = containerHeight/2;

// Change the registration point to the center
shape.regX = 25; //25 is half of the width on the shape that we specified.
shape.regY = 25;

// Tween the shape
createjs.Tween.get(shape).to({rotation:360}, 3000);

这是一个带有该代码的 fiddle :http://jsfiddle.net/lannymcnie/548jsume/

关于javascript - 如何在容器中心旋转我的补间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31580205/

相关文章:

JavaScript - 是否可以限制 forEach 循环给出的结果?

javascript - 最新的 Chrome 桌面更新阻止 SoundJS 播放声音(与自动播放相关)

javascript - createjs flash cc,单独的 list 预加载

javascript - 在Nodejs中将流解析为对象

javascript - 如何在突变 onSuccess 中运行 useQuery 查询并首先等待结果?

android - 为 Android AnimationDrawable PNG 序列提供资源的最佳做法是什么?

android - 方向更改后反转共享元素转换

jquery滑动/计时问题

javascript - "Uncaught TypeError: f.isVisible is not a function"与 createjs 和 pixi

javascript - 在控制范围内检测层启用/禁用