public class

Context2d

extends JavaScriptObject
implements Context
java.lang.Object
   ↳ com.google.gwt.core.client.JavaScriptObject
     ↳ com.google.gwt.canvas.dom.client.Context2d

Class Overview

Rendering interface used to draw on a CanvasElement.

Experimental API: This API is still under development and is subject to change.

Summary

Nested Classes
enum Context2d.Composite Enum for composite style. 
enum Context2d.LineCap Enum for line-cap style. 
enum Context2d.LineJoin Enum for line-join style. 
enum Context2d.Repetition Enum for the repetition values. 
enum Context2d.TextAlign Enum for text align style. 
enum Context2d.TextBaseline Enum for text baseline style. 
Constants
String CONTEXT_ID Specifies the context id property used in creating a Context.
Protected Constructors
Context2d()
Public Methods
final void arc(double x, double y, double radius, double startAngle, double endAngle)
Draws an arc.
final void arc(double x, double y, double radius, double startAngle, double endAngle, boolean anticlockwise)
Draws an arc.
final void arcTo(double x1, double y1, double x2, double y2, double radius)
Adds an arc to the current subpath, connecting it to the current point with a line segment.
final void beginPath()
Begins a new path.
final void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y)
Draws a cubic Bézier curve from the current point to the point (x, y), with control points (cp1x, cp1y) and (cp2x, cp2y).
final void clearRect(double x, double y, double w, double h)
Clears a rectangle.
final void clip()
Creates a new clipping region from the current path.
final void closePath()
Closes the current path.
final ImageData createImageData(ImageData imagedata)
Creates an image data object of the same size as the given object.
final ImageData createImageData(int w, int h)
Creates an image data object of the given size.
final CanvasGradient createLinearGradient(double x0, double y0, double x1, double y1)
Creates a linear gradient.
final CanvasPattern createPattern(CanvasElement image, String repetition)
Creates a pattern from another canvas.
final CanvasPattern createPattern(ImageElement image, String repetition)
Creates a pattern from an image.
final CanvasPattern createPattern(CanvasElement image, Context2d.Repetition repetition)
Creates a pattern from another canvas.
final CanvasPattern createPattern(ImageElement image, Context2d.Repetition repetition)
Creates a pattern from an image.
final CanvasGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1)
Creates a radial gradient.
final void drawImage(ImageElement image, double sx, double sy, double sw, double sh, double dx, double dy, double dw, double dh)
Draws a scaled subset of an image.
final void drawImage(CanvasElement image, double dx, double dy, double dw, double dh)
Draws a scaled image.
final void drawImage(ImageElement image, double dx, double dy, double dw, double dh)
Draws a scaled image.
final void drawImage(CanvasElement image, double dx, double dy)
Draws an image.
final void drawImage(ImageElement image, double dx, double dy)
Draws an image.
final void drawImage(CanvasElement image, double sx, double sy, double sw, double sh, double dx, double dy, double dw, double dh)
Draws a scaled subset of an image.
final void fill()
Fills the current path.
final void fillRect(double x, double y, double w, double h)
Fills a rectangle.
final void fillText(String text, double x, double y)
Draws filled text.
final void fillText(String text, double x, double y, double maxWidth)
Draws filled text squeezed into the given max width.
final CanvasElement getCanvas()
Gets this context's canvas.
final FillStrokeStyle getFillStyle()
Returns the context's fillStyle.
final String getFont()
Gets this context's font.
final double getGlobalAlpha()
Gets the global alpha value.
final String getGlobalCompositeOperation()
Gets the global composite operation.
final ImageData getImageData(double sx, double sy, double sw, double sh)
Returns an image data object for the screen area denoted by sx, sy, sw and sh.
final String getLineCap()
Gets the current line-cap style.
final String getLineJoin()
Gets the current line-join style.
final double getLineWidth()
Gets the current line-width.
final double getMiterLimit()
Gets the current miter-limit.
final double getShadowBlur()
Gets the current shadow-blur.
final String getShadowColor()
Gets the current shadow color.
final double getShadowOffsetX()
Gets the current x-shadow-offset.
final double getShadowOffsetY()
Gets the current y-shadow-offset.
final FillStrokeStyle getStrokeStyle()
Returns the context's strokeStyle.
final String getTextAlign()
Gets the current text align.
final String getTextBaseline()
Gets the current text baseline.
final boolean isPointInPath(double x, double y)
Returns true if the given point is in the current path.
final void lineTo(double x, double y)
Adds a line from the current point to the point (x, y) to the current path.
final TextMetrics measureText(String text)
Returns the metrics for the given text.
final void moveTo(double x, double y)
Terminates the current path and sets the current path position to the point (x, y).
final void putImageData(ImageData imagedata, double x, double y)
Draws the given image data at the given screen position.
final void quadraticCurveTo(double cpx, double cpy, double x, double y)
Draws a quadratic Bézier curve from the current point to the point (x, y), with control point (cpx, cpy).
final void rect(double x, double y, double w, double h)
Creates a new rectangular path.
final void restore()
Restores the context's state.
final void rotate(double angle)
Applies rotation to the current transform.
final void save()
Saves the context's state.
final void scale(double x, double y)
Applies scale to the current transform.
final void setFillStyle(String fillStyleColor)
Convenience method to set the context's fillStyle to a CssColor, specified in String form.
final void setFillStyle(FillStrokeStyle fillStyle)
Sets the context's fillStyle.
final void setFont(String f)
Sets the font.
final void setGlobalAlpha(double alpha)
Sets the global alpha value.
final void setGlobalCompositeOperation(String globalCompositeOperation)
Sets the global composite operation.
final void setGlobalCompositeOperation(Context2d.Composite composite)
Sets the global composite operation.
final void setLineCap(String lineCap)
Sets the line-cap style.
final void setLineCap(Context2d.LineCap lineCap)
Sets the line-cap style.
final void setLineJoin(String lineJoin)
Sets the line-join style.
final void setLineJoin(Context2d.LineJoin lineJoin)
Sets the line-join style.
final void setLineWidth(double lineWidth)
Sets the line-width.
final void setMiterLimit(double miterLimit)
Sets the miter-limit.
final void setShadowBlur(double shadowBlur)
Sets the shadow-blur.
final void setShadowColor(String shadowColor)
Sets the shadow-color.
final void setShadowOffsetX(double shadowOffsetX)
Sets the x-shadow-offset.
final void setShadowOffsetY(double shadowOffsetY)
Sets the y-shadow-offset.
final void setStrokeStyle(FillStrokeStyle strokeStyle)
Sets the context's stroke style.
final void setStrokeStyle(String strokeStyleColor)
Convenience method to set the context's strokeStyle to a CssColor.
final void setTextAlign(String align)
Sets the text alignment.
final void setTextAlign(Context2d.TextAlign align)
Sets the text alignment.
final void setTextBaseline(String baseline)
Sets the text baseline.
final void setTextBaseline(Context2d.TextBaseline baseline)
Sets the text baseline.
final void setTransform(double m11, double m12, double m21, double m22, double dx, double dy)
Sets the 2D transformation matrix.
final void stroke()
Draws the current path with the current stroke style.
final void strokeRect(double x, double y, double w, double h)
Draws the outline of a rectangle with the current stroke style.
final void strokeText(String text, double x, double y, double maxWidth)
Draws the text outline, squeezing the text into the given max width by compressing the font.
final void strokeText(String text, double x, double y)
Draws the text outline.
final void transform(double m11, double m12, double m21, double m22, double dx, double dy)
Multiplies the current transform by the given matrix.
final void translate(double x, double y)
Applies a translation to the current transform.
[Expand]
Inherited Methods
From class com.google.gwt.core.client.JavaScriptObject
From class java.lang.Object

Constants

public static final String CONTEXT_ID

Specifies the context id property used in creating a Context.

Constant Value: "2d"

Protected Constructors

protected Context2d ()

Public Methods

public final void arc (double x, double y, double radius, double startAngle, double endAngle)

Draws an arc. If a current subpath exists, a line segment is added from the current point to the starting point of the arc. If endAngle - startAngle is equal to or greater than 2 * Math.Pi, the arc is the whole circumference of the circle.

Parameters
x the x coordinate of the center of the arc
y the y coordinate of the center of the arc
radius the radius of the arc
startAngle the start angle, measured in radians clockwise from the positive x-axis
endAngle the end angle, measured in radians clockwise from the positive x-axis

public final void arc (double x, double y, double radius, double startAngle, double endAngle, boolean anticlockwise)

Draws an arc. If a current subpath exists, a line segment is added from the current point to the starting point of the arc. If anticlockwise is false and endAngle - startAngle is equal to or greater than 2 * Math.PI, or if anticlockwise is true and startAngle - endAngle is equal to or greater than 2 * Math.PI, then the arc is the whole circumference of the circle.

Parameters
x the x coordinate of the center of the arc
y the y coordinate of the center of the arc
radius the radius of the arc
startAngle the start angle, measured in radians clockwise from the positive x-axis
endAngle the end angle, measured in radians clockwise from the positive x-axis
anticlockwise if true, the arc is drawn in an anticlockwise direction

public final void arcTo (double x1, double y1, double x2, double y2, double radius)

Adds an arc to the current subpath, connecting it to the current point with a line segment.

Parameters
x1 the x coordinate of the starting point of the arc
y1 the y coordinate of the starting point of the arc
x2 the x coordinate of the ending point of the arc
y2 the y coordinate of the ending point of the arc
radius the radius of a circle containing the arc

public final void beginPath ()

Begins a new path.

public final void bezierCurveTo (double cp1x, double cp1y, double cp2x, double cp2y, double x, double y)

Draws a cubic Bézier curve from the current point to the point (x, y), with control points (cp1x, cp1y) and (cp2x, cp2y).

Parameters
cp1x the x coordinate of the first control point
cp1y the y coordinate of the first control point
cp2x the x coordinate of the second control point
cp2y the y coordinate of the second control point
x the x coordinate of the end point
y the y coordinate of the end point

public final void clearRect (double x, double y, double w, double h)

Clears a rectangle.

Parameters
x the x coordinate of the rectangle's upper-left corner
y the y coordinate of the rectangle's upper-left corner
w the width of the rectangle
h the height of the rectangle

public final void clip ()

Creates a new clipping region from the current path.

public final void closePath ()

Closes the current path.

public final ImageData createImageData (ImageData imagedata)

Creates an image data object of the same size as the given object.

Parameters
imagedata an ImageData object
Returns

public final ImageData createImageData (int w, int h)

Creates an image data object of the given size.

Parameters
w the width of the image
h the height of the image
Returns

public final CanvasGradient createLinearGradient (double x0, double y0, double x1, double y1)

Creates a linear gradient.

Parameters
x0 the x coordinate of the starting point of the gradient
y0 the y coordinate of the starting point of the gradient
x1 the x coordinate of the ending point of the gradient
y1 the y coordinate of the ending point of the gradient
Returns

public final CanvasPattern createPattern (CanvasElement image, String repetition)

Creates a pattern from another canvas.

Parameters
image an CanvasElement object
repetition the repetition type as a String
Returns

public final CanvasPattern createPattern (ImageElement image, String repetition)

Creates a pattern from an image.

Parameters
image an ImageElement object
repetition the repetition type as a String
Returns

public final CanvasPattern createPattern (CanvasElement image, Context2d.Repetition repetition)

Creates a pattern from another canvas.

Parameters
image an CanvasElement object
repetition a Context2d.Repetition object
Returns

public final CanvasPattern createPattern (ImageElement image, Context2d.Repetition repetition)

Creates a pattern from an image.

Parameters
image an ImageElement object
repetition a Context2d.Repetition object
Returns

public final CanvasGradient createRadialGradient (double x0, double y0, double r0, double x1, double y1, double r1)

Creates a radial gradient.

Parameters
x0 the x coordinate of the center of the start circle of the gradient
y0 the y coordinate of the center of the start circle of the gradient
r0 the radius of the start circle of the gradient
x1 the x coordinate of the center of the end circle of the gradient
y1 the y coordinate of the center of the end circle of the gradient
r1 the radius of the end circle of the gradient
Returns

public final void drawImage (ImageElement image, double sx, double sy, double sw, double sh, double dx, double dy, double dw, double dh)

Draws a scaled subset of an image.

Parameters
image an ImageElement object
sx the x coordinate of the upper-left corner of the source rectangle
sy the y coordinate of the upper-left corner of the source rectangle
sw the width of the source rectangle
sh the width of the source rectangle
dx the x coordinate of the upper-left corner of the destination rectangle
dy the y coordinate of the upper-left corner of the destination rectangle
dw the width of the destination rectangle
dh the height of the destination rectangle

public final void drawImage (CanvasElement image, double dx, double dy, double dw, double dh)

Draws a scaled image.

Parameters
image an CanvasElement object
dx the x coordinate of the upper-left corner of the destination rectangle
dy the y coordinate of the upper-left corner of the destination rectangle
dw the width of the destination rectangle
dh the height of the destination rectangle

public final void drawImage (ImageElement image, double dx, double dy, double dw, double dh)

Draws a scaled image.

Parameters
image an ImageElement object
dx the x coordinate of the upper-left corner of the destination rectangle
dy the y coordinate of the upper-left corner of the destination rectangle
dw the width of the destination rectangle
dh the height of the destination rectangle

public final void drawImage (CanvasElement image, double dx, double dy)

Draws an image.

Parameters
image an CanvasElement object
dx the x coordinate of the upper-left corner of the destination rectangle
dy the y coordinate of the upper-left corner of the destination rectangle

public final void drawImage (ImageElement image, double dx, double dy)

Draws an image.

Parameters
image an ImageElement object
dx the x coordinate of the upper-left corner of the destination rectangle
dy the y coordinate of the upper-left corner of the destination rectangle

public final void drawImage (CanvasElement image, double sx, double sy, double sw, double sh, double dx, double dy, double dw, double dh)

Draws a scaled subset of an image.

Parameters
image an CanvasElement object
sx the x coordinate of the upper-left corner of the source rectangle
sy the y coordinate of the upper-left corner of the source rectangle
sw the width of the source rectangle
sh the width of the source rectangle
dx the x coordinate of the upper-left corner of the destination rectangle
dy the y coordinate of the upper-left corner of the destination rectangle
dw the width of the destination rectangle
dh the height of the destination rectangle

public final void fill ()

Fills the current path.

public final void fillRect (double x, double y, double w, double h)

Fills a rectangle.

Parameters
x the x coordinate of the rectangle's upper-left corner
y the y coordinate of the rectangle's upper-left corner
w the width of the rectangle
h the height of the rectangle

public final void fillText (String text, double x, double y)

Draws filled text.

Parameters
text the text as a String
x the x coordinate of the text position
y the y coordinate of the text position

public final void fillText (String text, double x, double y, double maxWidth)

Draws filled text squeezed into the given max width.

Parameters
text the text as a String
x the x coordinate of the text position
y the y coordinate of the text position
maxWidth the maximum width for the text

public final CanvasElement getCanvas ()

Gets this context's canvas.

Returns

public final FillStrokeStyle getFillStyle ()

Returns the context's fillStyle. In dev mode, the returned object will be wrapped in a JavaScript array.

Returns

public final String getFont ()

Gets this context's font.

Returns
  • the font name as a String
See Also

public final double getGlobalAlpha ()

Gets the global alpha value.

Returns
  • the global alpha value as a double

public final String getGlobalCompositeOperation ()

Gets the global composite operation.

Returns
  • the global composite operation as a String

public final ImageData getImageData (double sx, double sy, double sw, double sh)

Returns an image data object for the screen area denoted by sx, sy, sw and sh.

Parameters
sx the x coordinate of the upper-left corner of the desired area
sy the y coordinate of the upper-left corner of the desired area
sw the width of the desired area
sh the height of the desired area
Returns
  • an ImageData object containing screen pixel data

public final String getLineCap ()

Gets the current line-cap style.

Returns
  • the line cap style as a String

public final String getLineJoin ()

Gets the current line-join style.

Returns
  • the line join style as a String

public final double getLineWidth ()

Gets the current line-width.

Returns
  • the line width as a double

public final double getMiterLimit ()

Gets the current miter-limit.

Returns
  • the miter limit as a double

public final double getShadowBlur ()

Gets the current shadow-blur.

Returns
  • the shadow blur amount as a double

public final String getShadowColor ()

Gets the current shadow color.

Returns
  • the shadow color as a String

public final double getShadowOffsetX ()

Gets the current x-shadow-offset.

Returns
  • the shadow x offset as a double

public final double getShadowOffsetY ()

Gets the current y-shadow-offset.

Returns
  • the shadow y offset as a double

public final FillStrokeStyle getStrokeStyle ()

Returns the context's strokeStyle. In dev mode, the returned object will be wrapped in a JavaScript array.

Returns

public final String getTextAlign ()

Gets the current text align.

Returns
  • the text align as a String

public final String getTextBaseline ()

Gets the current text baseline.

Returns
  • the text baseline as a String

public final boolean isPointInPath (double x, double y)

Returns true if the given point is in the current path.

Parameters
x the x coordinate of the point to test.
y the y coordinate of the point to test.
Returns
  • true if the given point is in the current path.

public final void lineTo (double x, double y)

Adds a line from the current point to the point (x, y) to the current path.

Parameters
x the x coordinate of the line endpoint
y the y coordinate of the line endpoint

public final TextMetrics measureText (String text)

Returns the metrics for the given text.

Parameters
text the text to measure, as a String
Returns

public final void moveTo (double x, double y)

Terminates the current path and sets the current path position to the point (x, y).

Parameters
x the x coordinate of the new position
y the y coordinate of the new position

public final void putImageData (ImageData imagedata, double x, double y)

Draws the given image data at the given screen position.

Parameters
imagedata an ImageData instance to be written to the screen
x the x coordinate of the upper-left corner at which to draw
y the y coordinate of the upper-left corner at which to draw

public final void quadraticCurveTo (double cpx, double cpy, double x, double y)

Draws a quadratic Bézier curve from the current point to the point (x, y), with control point (cpx, cpy).

Parameters
cpx the x coordinate of the control point
cpy the y coordinate of the control point
x the x coordinate of the end point
y the y coordinate of the end point

public final void rect (double x, double y, double w, double h)

Creates a new rectangular path.

Parameters
x the x coordinate of the rectangle's upper-left corner
y the y coordinate of the rectangle's upper-left corner
w the width of the rectangle
h the height of the rectangle

public final void restore ()

Restores the context's state.

public final void rotate (double angle)

Applies rotation to the current transform.

Parameters
angle the clockwise rotation angle, in radians

public final void save ()

Saves the context's state.

public final void scale (double x, double y)

Applies scale to the current transform.

Parameters
x the scale factor along the x-axis
y the scale factor along the y-axis

public final void setFillStyle (String fillStyleColor)

Convenience method to set the context's fillStyle to a CssColor, specified in String form.

Parameters
fillStyleColor the color as a String
See Also

public final void setFillStyle (FillStrokeStyle fillStyle)

Sets the context's fillStyle.

Parameters
fillStyle the fill style to set.

public final void setFont (String f)

Sets the font.

Parameters
f the font name as a String
See Also

public final void setGlobalAlpha (double alpha)

Sets the global alpha value.

Parameters
alpha the global alpha value as a double
See Also

public final void setGlobalCompositeOperation (String globalCompositeOperation)

Sets the global composite operation.

Parameters
globalCompositeOperation the operation as a String

public final void setGlobalCompositeOperation (Context2d.Composite composite)

Sets the global composite operation.

Parameters
composite a Context2d.Composite value

public final void setLineCap (String lineCap)

Sets the line-cap style.

Parameters
lineCap the line cap style as a String
See Also

public final void setLineCap (Context2d.LineCap lineCap)

Sets the line-cap style.

Parameters
lineCap the line cap style as a Context2d.LineCap value
See Also

public final void setLineJoin (String lineJoin)

Sets the line-join style.

Parameters
lineJoin the ling join style as a String
See Also

public final void setLineJoin (Context2d.LineJoin lineJoin)

Sets the line-join style.

Parameters
lineJoin the line join style as a Context2d.LineJoin value
See Also

public final void setLineWidth (double lineWidth)

Sets the line-width.

Parameters
lineWidth the line width as a double

public final void setMiterLimit (double miterLimit)

Sets the miter-limit.

Parameters
miterLimit the miter limit as a double
See Also

public final void setShadowBlur (double shadowBlur)

Sets the shadow-blur.

Parameters
shadowBlur the amount of blur as a double
See Also

public final void setShadowColor (String shadowColor)

Sets the shadow-color.

Parameters
shadowColor the shadow color as a String
See Also

public final void setShadowOffsetX (double shadowOffsetX)

Sets the x-shadow-offset.

Parameters
shadowOffsetX the x shadow offset

public final void setShadowOffsetY (double shadowOffsetY)

Sets the y-shadow-offset.

Parameters
shadowOffsetY the y shadow offset

public final void setStrokeStyle (FillStrokeStyle strokeStyle)

Sets the context's stroke style.

Parameters
strokeStyle the stroke style to set

public final void setStrokeStyle (String strokeStyleColor)

Convenience method to set the context's strokeStyle to a CssColor.

Parameters
strokeStyleColor the stroke color as a String
See Also

public final void setTextAlign (String align)

Sets the text alignment.

Parameters
align the alignment setting as a String
See Also

public final void setTextAlign (Context2d.TextAlign align)

Sets the text alignment.

Parameters
align the alignment setting as a Context2d.TextAlign value
See Also

public final void setTextBaseline (String baseline)

Sets the text baseline.

Parameters
baseline the baseline setting as a String

public final void setTextBaseline (Context2d.TextBaseline baseline)

Sets the text baseline.

Parameters
baseline a the baseline setting as a Context2d.TextBaseline value

public final void setTransform (double m11, double m12, double m21, double m22, double dx, double dy)

Sets the 2D transformation matrix.

Parameters
m11 the value at position (1, 1) of the matrix
m12 the value at position (1, 2) of the matrix
m21 the value at position (2, 1) of the matrix
m22 the value at position (2, 2) of the matrix
dx the x translation value
dy the y translation value

public final void stroke ()

Draws the current path with the current stroke style.

public final void strokeRect (double x, double y, double w, double h)

Draws the outline of a rectangle with the current stroke style.

Parameters
x the x coordinate of the rectangle's upper-left corner
y the y coordinate of the rectangle's upper-left corner
w the width of the rectangle
h the height of the rectangle

public final void strokeText (String text, double x, double y, double maxWidth)

Draws the text outline, squeezing the text into the given max width by compressing the font.

Parameters
text the text as a String
x the x coordinate of the text position
y the y coordinate of the text position
maxWidth the maximum width for the text

public final void strokeText (String text, double x, double y)

Draws the text outline.

Parameters
text the text as a String
x the x coordinate of the text position
y the y coordinate of the text position

public final void transform (double m11, double m12, double m21, double m22, double dx, double dy)

Multiplies the current transform by the given matrix.

Parameters
m11 the value at position (1, 1) of the matrix
m12 the value at position (1, 2) of the matrix
m21 the value at position (2, 1) of the matrix
m22 the value at position (2, 2) of the matrix
dx the x translation value
dy the y translation value

public final void translate (double x, double y)

Applies a translation to the current transform.

Parameters
x the amount of translation along the x-axis
y the amount of translation along the y-axis