Package playn.core
Class Canvas
java.lang.Object
playn.core.Canvas
- All Implemented Interfaces:
AutoCloseable
,Closeable
A 2D drawing canvas. Rendering is performed by the CPU into a bitmap.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
Values that may be used withsetCompositeOperation(Composite)
.static interface
Facilitates drawing images and image regions to a canvas.static enum
Values that may be used withsetLineCap(LineCap)
.static enum
Values that may be used withsetLineJoin(LineJoin)
.Nested classes/interfaces inherited from interface react.Closeable
Closeable.Set, Closeable.Util
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionabstract Canvas
clear()
Clears the entire canvas torgba(0, 0, 0, 0)
.abstract Canvas
clearRect
(float x, float y, float width, float height) Clears the specified region torgba (0, 0, 0, 0)
.abstract Canvas
Intersects the current clip with the specified path.abstract Canvas
clipRect
(float x, float y, float width, float height) Intersects the current clip with the supplied rectangle.void
close()
Informs the platform that this canvas, and its backing image will no longer be used.createGradient
(Gradient.Config config) Creates a gradient fill pattern.Creates a path object.draw
(Canvas.Drawable image, float x, float y) Drawsimage
at the specified location(x, y)
.draw
(Canvas.Drawable image, float x, float y, float w, float h) Draws a scaled image at the specified location(x, y)
size(w x h)
.draw
(Canvas.Drawable image, float dx, float dy, float dw, float dh, float sx, float sy, float sw, float sh) Draws a subregion of a image(sw x sh) @ (sx, sy)
at the specified size(dw x dh)
and location(dx, dy)
.abstract Canvas
drawArc
(float cx, float cy, float r, float startAngle, float arcAngle) Draws an arc of a circle centered at cx, cy, with radius r.drawCentered
(Canvas.Drawable image, float x, float y) Drawsimage
centered at the specified location.abstract Canvas
drawLine
(float x0, float y0, float x1, float y1) Draws a line between the two specified points.abstract Canvas
drawPoint
(float x, float y) Draws a single point at the specified location.abstract Canvas
Draws text at the specified location.abstract Canvas
fillCircle
(float x, float y, float radius) Fills a circle at the specified center and radius.abstract Canvas
Fills the specified path.abstract Canvas
fillRect
(float x, float y, float width, float height) Fills the specified rectangle.abstract Canvas
fillRoundRect
(float x, float y, float width, float height, float radius) Fills the specified rounded rectangle.abstract Canvas
fillText
(TextLayout text, float x, float y) Fills the text at the specified location.abstract Canvas
restore()
Restores the canvas's previous state.abstract Canvas
rotate
(float radians) Rotates the current transformation matrix by the specified angle in radians.abstract Canvas
save()
The save and restore methods preserve and restore the state of the canvas, but not specific paths or graphics.abstract Canvas
scale
(float x, float y) Scales the current transformation matrix by the specified amount.abstract Canvas
setAlpha
(float alpha) Set the global alpha value to be used for all painting.abstract Canvas
setCompositeOperation
(Canvas.Composite composite) Sets the Porter-Duff composite operation to be used for all painting.abstract Canvas
setFillColor
(int color) Sets the color to be used for fill operations.abstract Canvas
setFillGradient
(Gradient gradient) Sets the gradient to be used for fill operations.abstract Canvas
setFillPattern
(Pattern pattern) Sets the pattern to be used for fill operations.abstract Canvas
setLineCap
(Canvas.LineCap cap) Sets the line-cap mode for strokes.abstract Canvas
setLineJoin
(Canvas.LineJoin join) Sets the line-join mode for strokes.abstract Canvas
setMiterLimit
(float miter) Sets the miter limit for strokes.abstract Canvas
setStrokeColor
(int color) Sets the color for strokes.abstract Canvas
setStrokeWidth
(float strokeWidth) Sets the width for strokes, in pixels.abstract Image
snapshot()
Returns an immutable snapshot of the image that backs this canvas.abstract Canvas
strokeCircle
(float x, float y, float radius) Strokes a circle at the specified center and radius.abstract Canvas
strokePath
(Path path) Strokes the specified path.abstract Canvas
strokeRect
(float x, float y, float width, float height) Strokes the specified rectangle.abstract Canvas
strokeRoundRect
(float x, float y, float width, float height, float radius) Strokes the specified rounded rectangle.abstract Canvas
strokeText
(TextLayout text, float x, float y) Strokes the text at the specified location.CallstoTexture(Texture.Config)
with the default texture config.toTexture
(Texture.Config config) A helper function for creating a texture from this canvas's image, and then disposing this canvas.abstract Canvas
transform
(float m11, float m12, float m21, float m22, float dx, float dy) Multiplies the current transformation matrix by the given matrix.abstract Canvas
translate
(float x, float y) Translates the current transformation matrix by the given amount.
-
Field Details
-
image
The image that underlies this canvas. -
width
public final float widthThe width of this canvas. -
height
public final float heightThe height of this canvas.
-
-
Method Details
-
snapshot
Returns an immutable snapshot of the image that backs this canvas. Subsequent changes to this canvas will not be reflected in the returned image. If you are going to render a canvas image into another canvas image a lot, using a snapshot can improve performance. -
close
public void close()Informs the platform that this canvas, and its backing image will no longer be used. On some platforms this can free up memory earlier than if we waited for the canvas to be garbage collected.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
clear
Clears the entire canvas torgba(0, 0, 0, 0)
. -
clearRect
Clears the specified region torgba (0, 0, 0, 0)
. -
clip
Intersects the current clip with the specified path. -
clipRect
Intersects the current clip with the supplied rectangle. -
createPath
Creates a path object. -
createGradient
Creates a gradient fill pattern. -
draw
Drawsimage
at the specified location(x, y)
. -
drawCentered
Drawsimage
centered at the specified location. Subtractsimage.width/2
from x andimage.height/2
from y. -
draw
Draws a scaled image at the specified location(x, y)
size(w x h)
. -
draw
public Canvas draw(Canvas.Drawable image, float dx, float dy, float dw, float dh, float sx, float sy, float sw, float sh) Draws a subregion of a image(sw x sh) @ (sx, sy)
at the specified size(dw x dh)
and location(dx, dy)
. TODO (jgw): Document whether out-of-bounds source coordinates clamp, repeat, or do nothing. -
drawLine
Draws a line between the two specified points. -
drawPoint
Draws a single point at the specified location. -
drawArc
Draws an arc of a circle centered at cx, cy, with radius r.The start and arc angles are specified in radians. Angles are interpreted such that 0 radians is at the 3 o'clock position. Positive values indicate a counter-clockwise rotation while negative values indicate a clockwise rotation.
-
drawText
Draws text at the specified location. The text will be drawn in the current fill color. -
fillCircle
Fills a circle at the specified center and radius. -
fillPath
Fills the specified path. -
fillRect
Fills the specified rectangle. -
fillRoundRect
Fills the specified rounded rectangle.- Parameters:
x
- the x coordinate of the upper left of the rounded rectangle.y
- the y coordinate of the upper left of the rounded rectangle.width
- the width of the rounded rectangle.height
- the width of the rounded rectangle.radius
- the radius of the circle to use for the corner.
-
fillText
Fills the text at the specified location. The text will use the current fill color. -
restore
Restores the canvas's previous state.- See Also:
-
rotate
Rotates the current transformation matrix by the specified angle in radians. -
save
The save and restore methods preserve and restore the state of the canvas, but not specific paths or graphics. The following values are saved:- transformation matrix
- clipping path
- stroke color
- stroke width
- line cap
- line join
- miter limit
- fill color or gradient
- composite operation
-
scale
Scales the current transformation matrix by the specified amount. -
setAlpha
Set the global alpha value to be used for all painting.Values outside the range [0,1] will be clamped to the range [0,1].
- Parameters:
alpha
- alpha value in range [0,1] where 0 is transparent and 1 is opaque
-
setCompositeOperation
Sets the Porter-Duff composite operation to be used for all painting. -
setFillColor
Sets the color to be used for fill operations. This replaces any existing fill gradient or pattern. -
setFillGradient
Sets the gradient to be used for fill operations. This replaces any existing fill color or pattern. -
setFillPattern
Sets the pattern to be used for fill operations. This replaces any existing fill color or gradient. -
setLineCap
Sets the line-cap mode for strokes. -
setLineJoin
Sets the line-join mode for strokes. -
setMiterLimit
Sets the miter limit for strokes. -
setStrokeColor
Sets the color for strokes. -
setStrokeWidth
Sets the width for strokes, in pixels. -
strokeCircle
Strokes a circle at the specified center and radius. -
strokePath
Strokes the specified path. -
strokeRect
Strokes the specified rectangle. -
strokeRoundRect
Strokes the specified rounded rectangle.- Parameters:
x
- the x coordinate of the upper left of the rounded rectangle.y
- the y coordinate of the upper left of the rounded rectangle.width
- the width of the rounded rectangle.height
- the width of the rounded rectangle.radius
- the radius of the circle to use for the corner.
-
strokeText
Strokes the text at the specified location. The text will use the current stroke configuration (color, width, etc.). -
toTexture
CallstoTexture(Texture.Config)
with the default texture config. -
toTexture
A helper function for creating a texture from this canvas's image, and then disposing this canvas. This is useful for situations where you create a canvas, draw something in it, turn it into a texture and then never use the canvas again. -
transform
Multiplies the current transformation matrix by the given matrix. -
translate
Translates the current transformation matrix by the given amount.
-