craftyjs.github.com

2D

Component for any entity that has a position on the stage.

Events

Move [Old position: { _x:Number, _y:Number, _w:Number, _h:Number }]
when the entity has moved
Change [Old position: { _x:Number, _y:Number, _w:Number, _h:Number }]
when the entity has moved
Rotate [Data: { cos:Number, sin:Number, deg:Number, rad:Number, o: {x:Number, y:Number}, matrix: {M11, M12, M21, M22} }]
when the entity is rotated
Back to top

.x

The x position on the stage. When modified, will automatically be redrawn. Is actually a getter/setter so when using this value for calculations and not modifying it, use the ._x property.

See Also

Back to top

.y

The y position on the stage. When modified, will automatically be redrawn. Is actually a getter/setter so when using this value for calculations and not modifying it, use the ._y property.

See Also

Back to top

.w

The width of the entity. When modified, will automatically be redrawn. Is actually a getter/setter so when using this value for calculations and not modifying it, use the ._w property.

Changing this value is not recommended as canvas has terrible resize quality and DOM will just clip the image.

See Also

Back to top

.h

The height of the entity. When modified, will automatically be redrawn. Is actually a getter/setter so when using this value for calculations and not modifying it, use the ._h property.

Changing this value is not recommended as canvas has terrible resize quality and DOM will just clip the image.

See Also

Back to top

.z

The z index on the stage. When modified, will automatically be redrawn. Is actually a getter/setter so when using this value for calculations and not modifying it, use the ._z property.

A higher z value will be closer to the front of the stage. A smaller z value will be closer to the back. A global Z index is produced based on its z value as well as the GID (which entity was created first). Therefore entities will naturally maintain order depending on when it was created if same z value.

z is required to be an integer, e.g. z=11.2 is not allowed.

See Also

Back to top

.rotation

The rotation state of the entity, in clockwise degrees. this.rotation = 0 sets it to its original orientation; this.rotation = 10 sets it to 10 degrees clockwise from its original orientation; this.rotation = -10 sets it to 10 degrees counterclockwise from its original orientation, etc.

When modified, will automatically be redrawn. Is actually a getter/setter so when using this value for calculations and not modifying it, use the ._rotation property.

this.rotation = 0 does the same thing as this.rotation = 360 or 720 or -360 or 36000 etc. So you can keep increasing or decreasing the angle for continuous rotation. (Numerical errors do not occur until you get to millions of degrees.)

The default is to rotate the entity around its (initial) top-left corner; use .origin() to change that.

See Also

Back to top

.alpha

Transparency of an entity. Must be a decimal value between 0.0 being fully transparent to 1.0 being fully opaque.

Back to top

.visible

If the entity is visible or not. Accepts a true or false value. Can be used for optimization by setting an entities visibility to false when not needed to be drawn.

The entity will still exist and can be collided with but just won't be drawn.

See Also

Back to top

._globalZ

When two entities overlap, the one with the larger _globalZ will be on top of the other.

See Also

Back to top

.area

public Number .area(void)

Calculates the area of the entity

Back to top

.intersect

public Boolean .intersect(Number x, Number y, Number w, Number h)

x
X position of the rect
y
Y position of the rect
w
Width of the rect
h
Height of the rect

public Boolean .intersect(Object rect)

rect
An object that must have the x, y, w, h values as properties

Determines if this entity intersects a rectangle.

Back to top

.within

public Boolean .within(Number x, Number y, Number w, Number h)

x
X position of the rect
y
Y position of the rect
w
Width of the rect
h
Height of the rect

public Boolean .within(Object rect)

rect
An object that must have the x, y, w, h values as properties

Determines if this current entity is within another rectangle.

Back to top

.contains

public Boolean .contains(Number x, Number y, Number w, Number h)

x
X position of the rect
y
Y position of the rect
w
Width of the rect
h
Height of the rect

public Boolean .contains(Object rect)

rect
An object that must have the x, y, w, h values as properties

Determines if the rectangle is within the current entity.

Back to top

.pos

public Object .pos(void)

Returns the x, y, w, h properties as a rect object (a rect object is just an object with the keys _x, _y, _w, _h).

The keys have an underscore prefix. This is due to the x, y, w, h properties being merely setters and getters that wrap the properties with an underscore (_x, _y, _w, _h).

Back to top

.mbr

public Object .mbr()

Returns the minimum bounding rectangle. If there is no rotation on the entity it will return the rect.

Back to top

.isAt

public Boolean .isAt(Number x, Number y)

x
X position of the point
y
Y position of the point

Determines whether a point is contained by the entity. Unlike other methods, an object can't be passed. The arguments require the x and y value

Back to top

.move

public this .move(String dir, Number by)

dir
Direction to move (n,s,e,w,ne,nw,se,sw)
by
Amount to move in the specified direction

Quick method to move the entity in a direction (n, s, e, w, ne, nw, se, sw) by an amount of pixels.

Back to top

.shift

public void .shift(Number x, Number y)

x
Amount to shift the x axis
y
Amount to shift the y axis

Shifts the circle by the specified amount.

Example

var poly = new Crafty.circle(0, 0, 10);
circle.shift(5,5);
//{x: 5, y: 5, radius: 10};
Back to top

._cascade

public void ._cascade(e)

e
Amount to move X

Shift move or rotate the entity by an amount. Use negative values for an opposite direction.

Back to top

.attach

public this .attach(Entity obj[, .., Entity objN])

obj
Child entity(s) to attach

Sets one or more entities to be children, with the current entity (this) as the parent. When the parent moves or rotates, its children move or rotate by the same amount. (But not vice-versa: If you move a child, it will not move the parent.) When the parent is destroyed, its children are destroyed.

For any entity, this._children is the array of its children entity objects (if any), and this._parent is its parent entity object (if any).

As many objects as wanted can be attached, and a hierarchy of objects is possible by attaching.

Back to top

.detach

public this .detach([Entity obj])

obj
The entity to detach. Left blank will remove all attached entities

Stop an entity from following the current entity. Passing no arguments will stop every entity attached.

Back to top

.origin

public this .origin(Number x, Number y)

x
Pixel value of origin offset on the X axis
y
Pixel value of origin offset on the Y axis

public this .origin(String offset)

offset
Combination of center, top, bottom, middle, left and right

Set the origin point of an entity for it to rotate around.

Example

this.origin("top left")
this.origin("center")
this.origin("bottom right")
this.origin("middle right")

See Also

Back to top

.flip

Events

Change
when the entity has flipped

public this .flip(String dir)

dir
Flip direction

Flip entity on passed direction

Example

this.flip("X")
Back to top

.unflip

Events

Change
when the entity has unflipped

public this .unflip(String dir)

dir
Unflip direction

Unflip entity on passed direction (if it's flipped)

Example

this.unflip("X")
Back to top

._attr

Setter method for all 2D properties including x, y, w, h, alpha, rotation and visible.