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
.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
.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
.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
.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
.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.
See Also
.rotation
Set the rotation of your entity. Rotation takes degrees in a clockwise direction. It is important to note there is no limit on the rotation value. Setting a rotation mod 360 will give the same rotation without reaching huge numbers.
See Also
.alpha
Transparency of an entity. Must be a decimal value between 0.0 being fully transparent to 1.0 being fully opaque.
.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
._globalZ
When two entities overlap, the one with the larger _globalZ will be on top of the other.
See Also
.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, hvalues as properties
Determines if this entity intersects a rectangle.
.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, hvalues as properties
Determines if this current entity is within another rectangle.
.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, hvalues as properties
Determines if the rectangle is within the current entity.
.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).
.mbr
public Object .mbr()
Returns the minimum bounding rectangle. If there is no rotation on the entity it will return the rect.
.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
.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.
.shift
public void .shift(Number x, Number y)
- x
- Amount to shift the
xaxis - y
- Amount to shift the
yaxis
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};
._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.
.attach
public this .attach(Entity obj[, .., Entity objN])
- obj
- Entity(s) to attach
Attaches an entities position and rotation to current entity. When the current entity moves, the attached entity will move by the same amount. Attached entities stored in _children array, the parent object is stored in _parent on the child entities.
As many objects as wanted can be attached and a hierarchy of objects is possible by attaching.
.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.
.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
.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")
.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")
._attr
Setter method for all 2D properties including x, y, w, h, alpha, rotation and visible.

@craftyjs
#crafty@freenode.net
google groups
louis@craftyjs.com
github