3

i have a container 276*266 and i want to rotate an object in the center of this container thats for i am using this code :

    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));

but it always rotate on the top left of the container can someone know why what ca i do to rotate it in the middle of my container ?

Coolbrain
  • 41
  • 1
  • 8

1 Answers1

1

This is pretty straightforward:

  1. Set the x and y position of the object to 50% of the container's width and height.
  2. Set the regX and regY of the content to 50% of its width and height.

Note that you can not use .width and .height to get these values. If the content is a Bitmap, Sprite, Text, or Container with those types of content, you can use getBounds() to get the width/height and offset from 0. If the content is Graphics/Shape, you can not get it automatically, and will have to specify the size yourself.

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);

Here is a fiddle with that code: http://jsfiddle.net/lannymcnie/548jsume/

Lanny
  • 11,244
  • 1
  • 22
  • 30
  • For more info on width and height in EaselJS, check out this article: http://blog.createjs.com/update-width-height-in-easeljs/ – Lanny Jul 24 '15 at 15:52