Graphics Class
The Graphics class contains methods used to draw primitive shapes such as lines, circles and rectangles to the display, and color and fill them.
Constructor
Graphics
()
Item Index
Methods
- _destroyCachedSprite
- _generateCachedSprite
- _renderCachedSprite
- _renderCanvas
- _renderWebGL
- addChild
- addChildAt
- arc
- beginFill
- bezierCurveTo
- clear
- click
- destroyCachedSprite
- drawCircle
- drawEllipse
- drawPolygon
- drawRect
- drawRoundedRect
- drawShape
- endFill
- generateTexture
- getBounds
- getChildAt
- getChildIndex
- getLocalBounds
- lineStyle
- lineTo
- mousedown
- mouseout
- mouseover
- mouseup
- mouseupoutside
- moveTo
- quadraticCurveTo
- removeChild
- removeChildAt
- removeChildren
- removeStageReference
- rightclick
- rightdown
- rightup
- rightupoutside
- setChildIndex
- setStageReference
- swapChildren
- tap
- toGlobal
- toLocal
- touchend
- touchendoutside
- touchstart
- updateCache
- updateCachedSpriteTexture
- updateLocalBounds
Properties
- _bounds
- _cacheAsBitmap
- _cacheIsDirty
- _cr
- _currentBounds
- _interactive
- _sr
- _webGL
- alpha
- blendMode
- boundsPadding
- buttonMode
- cacheAsBitmap
- cachedSpriteDirty
- children
- currentPath
- defaultCursor
- dirty
- fillAlpha
- filterArea
- filters
- graphicsData
- height
- hitArea
- interactive
- isMask
- lineColor
- lineWidth
- mask
- parent
- pivot
- position
- renderable
- rotation
- scale
- stage
- tint
- visible
- webGLDirty
- width
- worldAlpha
- worldTransform
- worldVisible
- x
- y
Methods
_destroyCachedSprite
()
private
Destroys the cached sprite.
_generateCachedSprite
()
private
Generates the cached sprite when the sprite has cacheAsBitmap = true
_renderCachedSprite
-
renderSession
Internal method.
Parameters:
-
renderSession
ObjectThe render session
_renderCanvas
-
renderSession
Renders the object using the Canvas renderer
Parameters:
-
renderSession
RenderSession
_renderWebGL
-
renderSession
Renders the object using the WebGL renderer
Parameters:
-
renderSession
RenderSession
addChild
-
child
Adds a child to the container.
Parameters:
-
child
DisplayObjectThe DisplayObject to add to the container
Returns:
The child that was added.
addChildAt
-
child
-
index
Adds a child to the container at a specified index. If the index is out of bounds an error will be thrown
Parameters:
-
child
DisplayObjectThe child to add
-
index
NumberThe index to place the child in
Returns:
The child that was added.
arc
-
cx
-
cy
-
radius
-
startAngle
-
endAngle
-
anticlockwise
The arc method creates an arc/curve (used to create circles, or parts of circles).
Parameters:
-
cx
NumberThe x-coordinate of the center of the circle
-
cy
NumberThe y-coordinate of the center of the circle
-
radius
NumberThe radius of the circle
-
startAngle
NumberThe starting angle, in radians (0 is at the 3 o'clock position of the arc's circle)
-
endAngle
NumberThe ending angle, in radians
-
anticlockwise
BooleanOptional. Specifies whether the drawing should be counterclockwise or clockwise. False is default, and indicates clockwise, while true indicates counter-clockwise.
Returns:
beginFill
-
color
-
alpha
Specifies a simple one-color fill that subsequent calls to other Graphics methods (such as lineTo() or drawCircle()) use when drawing.
Parameters:
-
color
Numberthe color of the fill
-
alpha
Numberthe alpha of the fill
Returns:
bezierCurveTo
-
cpX
-
cpY
-
cpX2
-
cpY2
-
toX
-
toY
Calculate the points for a bezier curve and then draws it.
Parameters:
-
cpX
NumberControl point x
-
cpY
NumberControl point y
-
cpX2
NumberSecond Control point x
-
cpY2
NumberSecond Control point y
-
toX
NumberDestination point x
-
toY
NumberDestination point y
Returns:
clear
()
Graphics
Clears the graphics that were drawn to this Graphics object, and resets fill and line style settings.
Returns:
click
-
interactionData
A callback that is used when the users clicks on the displayObject with their mouse's left button
Parameters:
-
interactionData
InteractionData
destroyCachedSprite
()
Destroys a previous cached sprite.
drawCircle
-
x
-
y
-
radius
Draws a circle.
Parameters:
-
x
NumberThe X coordinate of the center of the circle
-
y
NumberThe Y coordinate of the center of the circle
-
radius
NumberThe radius of the circle
Returns:
drawEllipse
-
x
-
y
-
width
-
height
Draws an ellipse.
Parameters:
-
x
NumberThe X coordinate of the center of the ellipse
-
y
NumberThe Y coordinate of the center of the ellipse
-
width
NumberThe half width of the ellipse
-
height
NumberThe half height of the ellipse
Returns:
drawPolygon
-
path
Draws a polygon using the given path.
Parameters:
-
path
ArrayThe path data used to construct the polygon.
Returns:
drawRect
-
x
-
y
-
width
-
height
Parameters:
-
x
NumberThe X coord of the top-left of the rectangle
-
y
NumberThe Y coord of the top-left of the rectangle
-
width
NumberThe width of the rectangle
-
height
NumberThe height of the rectangle
Returns:
drawRoundedRect
-
x
-
y
-
width
-
height
-
radius
Parameters:
-
x
NumberThe X coord of the top-left of the rectangle
-
y
NumberThe Y coord of the top-left of the rectangle
-
width
NumberThe width of the rectangle
-
height
NumberThe height of the rectangle
-
radius
NumberRadius of the rectangle corners
drawShape
-
shape
Draws the given shape to this Graphics object. Can be any of Circle, Rectangle, Ellipse, Line or Polygon.
Returns:
The generated GraphicsData object.
endFill
()
Graphics
Applies a fill to the lines and shapes that were added since the last call to the beginFill() method.
Returns:
generateTexture
-
resolution
-
scaleMode
Useful function that returns a texture of the graphics object that can then be used to create sprites This can be quite useful if your geometry is complicated and needs to be reused multiple times.
Parameters:
-
resolution
NumberThe resolution of the texture being generated
-
scaleMode
NumberShould be one of the PIXI.scaleMode consts
Returns:
a texture of the graphics object
getBounds
()
Rectangle
Retrieves the bounds of the graphic shape as a rectangle object
Returns:
the rectangular bounding area
getChildAt
-
index
Returns the child at the specified index
Parameters:
-
index
NumberThe index to get the child from
Returns:
The child at the given index, if any.
getChildIndex
-
child
Returns the index position of a child DisplayObject instance
Parameters:
-
child
DisplayObjectThe DisplayObject instance to identify
Returns:
The index position of the child display object to identify
getLocalBounds
()
Rectangle
Retrieves the non-global local bounds of the displayObjectContainer as a rectangle. The calculation takes all visible children into consideration.
Returns:
The rectangular bounding area
lineStyle
-
lineWidth
-
color
-
alpha
Specifies the line style used for subsequent calls to Graphics methods such as the lineTo() method or the drawCircle() method.
Parameters:
-
lineWidth
Numberwidth of the line to draw, will update the objects stored style
-
color
Numbercolor of the line to draw, will update the objects stored style
-
alpha
Numberalpha of the line to draw, will update the objects stored style
Returns:
lineTo
-
x
-
y
Draws a line using the current line style from the current drawing position to (x, y); The current drawing position is then set to (x, y).
Parameters:
-
x
Numberthe X coordinate to draw to
-
y
Numberthe Y coordinate to draw to
Returns:
mousedown
-
interactionData
A callback that is used when the user clicks the mouse's left button down over the sprite
Parameters:
-
interactionData
InteractionData
mouseout
-
interactionData
A callback that is used when the users mouse leaves the displayObject
Parameters:
-
interactionData
InteractionData
mouseover
-
interactionData
A callback that is used when the users mouse rolls over the displayObject
Parameters:
-
interactionData
InteractionData
mouseup
-
interactionData
A callback that is used when the user releases the mouse's left button that was over the displayObject for this callback to be fired, the mouse's left button must have been pressed down over the displayObject
Parameters:
-
interactionData
InteractionData
mouseupoutside
-
interactionData
A callback that is used when the user releases the mouse's left button that was over the displayObject but is no longer over the displayObject for this callback to be fired, the mouse's left button must have been pressed down over the displayObject
Parameters:
-
interactionData
InteractionData
moveTo
-
x
-
y
Moves the current drawing position to x, y.
Parameters:
-
x
Numberthe X coordinate to move to
-
y
Numberthe Y coordinate to move to
Returns:
quadraticCurveTo
-
cpX
-
cpY
-
toX
-
toY
Calculate the points for a quadratic bezier curve and then draws it. Based on: https://stackoverflow.com/questions/785097/how-do-i-implement-a-bezier-curve-in-c
Parameters:
-
cpX
NumberControl point x
-
cpY
NumberControl point y
-
toX
NumberDestination point x
-
toY
NumberDestination point y
Returns:
removeChild
-
child
Removes a child from the container.
Parameters:
-
child
DisplayObjectThe DisplayObject to remove
Returns:
The child that was removed.
removeChildAt
-
index
Removes a child from the specified index position.
Parameters:
-
index
NumberThe index to get the child from
Returns:
The child that was removed.
removeChildren
-
beginIndex
-
endIndex
Removes all children from this container that are within the begin and end indexes.
Parameters:
-
beginIndex
NumberThe beginning position. Default value is 0.
-
endIndex
NumberThe ending position. Default value is size of the container.
removeStageReference
()
Removes the current stage reference from the container and all of its children.
rightclick
-
interactionData
A callback that is used when the users clicks on the displayObject with their mouse's right button
Parameters:
-
interactionData
InteractionData
rightdown
-
interactionData
A callback that is used when the user clicks the mouse's right button down over the sprite
Parameters:
-
interactionData
InteractionData
rightup
-
interactionData
A callback that is used when the user releases the mouse's right button that was over the displayObject for this callback to be fired the mouse's right button must have been pressed down over the displayObject
Parameters:
-
interactionData
InteractionData
rightupoutside
-
interactionData
A callback that is used when the user releases the mouse's right button that was over the displayObject but is no longer over the displayObject for this callback to be fired, the mouse's right button must have been pressed down over the displayObject
Parameters:
-
interactionData
InteractionData
setChildIndex
-
child
-
index
Changes the position of an existing child in the display object container
Parameters:
-
child
DisplayObjectThe child DisplayObject instance for which you want to change the index number
-
index
NumberThe resulting index number for the child display object
setStageReference
-
stage
Sets the containers Stage reference. This is the Stage that this object, and all of its children, is connected to.
Parameters:
-
stage
Stagethe stage that the container will have as its current stage reference
swapChildren
-
child
-
child2
Swaps the position of 2 Display Objects within this container.
Parameters:
-
child
DisplayObject -
child2
DisplayObject
tap
-
interactionData
A callback that is used when the users taps on the sprite with their finger basically a touch version of click
Parameters:
-
interactionData
InteractionData
toGlobal
-
position
Calculates the global position of the display object
Parameters:
-
position
PointThe world origin to calculate from
Returns:
A point object representing the position of this object
toLocal
-
position
-
[from]
Calculates the local position of the display object relative to another point
Parameters:
-
position
PointThe world origin to calculate from
-
[from]
DisplayObject optionalThe DisplayObject to calculate the global position from
Returns:
A point object representing the position of this object
touchend
-
interactionData
A callback that is used when the user releases a touch over the displayObject
Parameters:
-
interactionData
InteractionData
touchendoutside
-
interactionData
A callback that is used when the user releases the touch that was over the displayObject for this callback to be fired, The touch must have started over the sprite
Parameters:
-
interactionData
InteractionData
touchstart
-
interactionData
A callback that is used when the user touches over the displayObject
Parameters:
-
interactionData
InteractionData
updateCache
()
Generates and updates the cached sprite for this object.
updateCachedSpriteTexture
()
private
Updates texture size based on canvas size
updateLocalBounds
()
Update the bounds of the object
Properties
_cacheAsBitmap
Boolean
private
Cached internal flag.
_cacheIsDirty
Boolean
private
Cached internal flag.
_cr
Number
private
cached sin rotation and cos rotation
_interactive
Boolean
private
[read-only] Whether or not the object is interactive, do not toggle directly! use the interactive
property
_sr
Number
private
cached sin rotation and cos rotation
_webGL
Array
private
Array containing some WebGL-related properties used by the WebGL renderer.
alpha
Number
The opacity of the object.
blendMode
Number
The blend mode to be applied to the graphic shape. Apply a value of PIXI.blendModes.NORMAL to reset the blend mode.
Default: PIXI.blendModes.NORMAL;
boundsPadding
Number
The bounds' padding used for bounds calculation.
cacheAsBitmap
Boolean
private
When cacheAsBitmap is set to true the graphics object will be rendered as if it was a sprite. This is useful if your graphics element does not change often, as it will speed up the rendering of the object in exchange for taking up texture memory. It is also useful if you need the graphics object to be anti-aliased, because it will be rendered using canvas. This is not recommended if you are constantly redrawing the graphics element.
Default: false
cachedSpriteDirty
Boolean
private
Used to detect if the cached sprite object needs to be updated.
children
Array[read-only] The array of children of this container.
currentPath
Object
private
Current path
defaultCursor
String
This is the cursor that will be used when the mouse is over this object. To enable this the element must have interaction = true and buttonMode = true
dirty
Boolean
private
Used to detect if the graphics object has changed. If this is set to true then the graphics object will be recalculated.
fillAlpha
Number
The alpha value used when filling the Graphics object.
filterArea
Rectangle
The area the filter is applied to like the hitArea this is used as more of an optimisation rather than figuring out the dimensions of the displayObject each frame you can set this rectangle
filters
Array An array of filters
Sets the filters for the displayObject.
- IMPORTANT: This is a webGL only feature and will be ignored by the canvas renderer. To remove filters simply set this property to 'null'
graphicsData
Array
private
Graphics data
height
Number
The height of the displayObjectContainer, setting this will actually modify the scale to achieve the value set
hitArea
Rectangle | Circle | Ellipse | Polygon
This is the defined area that will pick up mouse / touch events. It is null by default. Setting it is a neat way of optimising the hitTest function that the interactionManager will use (as it will not need to hit test all the children)
interactive
Boolean
Indicates if the sprite will have touch and mouse interactivity. It is false by default
Default: false
isMask
Boolean
Whether this shape is being used as a mask.
lineColor
String
The color of any lines drawn.
Default: 0
lineWidth
Number
The width (thickness) of any lines drawn.
mask
Graphics
Sets a mask for the displayObject. A mask is an object that limits the visibility of an object to the shape of the mask applied to it. In PIXI a regular mask must be a PIXI.Graphics object. This allows for much faster masking in canvas as it utilises shape clipping. To remove a mask, set this property to null.
parent
DisplayObjectContainer
[read-only] The display object container that contains this display object.
renderable
Boolean
Can this object be rendered
rotation
Number
The rotation of the object in radians.
stage
Stage
[read-only] The stage the display object is connected to, or undefined if it is not connected to the stage.
tint
Number
The tint applied to the graphic shape. This is a hex value. Apply a value of 0xFFFFFF to reset the tint.
Default: 0xFFFFFF
visible
Boolean
The visibility of the object.
webGLDirty
Boolean
private
Used to detect if the webgl graphics object has changed. If this is set to true then the graphics object will be recalculated.
width
Number
The width of the displayObjectContainer, setting this will actually modify the scale to achieve the value set
worldAlpha
Number
[read-only] The multiplied alpha of the displayObject
worldTransform
Matrix
private
[read-only] Current transform of the object based on world (parent) factors
worldVisible
Boolean
[read-only] Indicates if the sprite is globally visible.
x
Number
The position of the displayObject on the x axis relative to the local coordinates of the parent.
y
Number
The position of the displayObject on the y axis relative to the local coordinates of the parent.