Class CanvasRenderingContext2D

java.lang.Object
org.htmlunit.corejs.javascript.ScriptableObject
org.htmlunit.javascript.HtmlUnitScriptable
org.htmlunit.javascript.host.canvas.CanvasRenderingContext2D
All Implemented Interfaces:
Serializable, Cloneable, org.htmlunit.corejs.javascript.ConstProperties, org.htmlunit.corejs.javascript.debug.DebuggableObject, org.htmlunit.corejs.javascript.Scriptable, org.htmlunit.corejs.javascript.SymbolScriptable

public class CanvasRenderingContext2D extends HtmlUnitScriptable
A JavaScript object for CanvasRenderingContext2D.
See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from class org.htmlunit.corejs.javascript.ScriptableObject

    org.htmlunit.corejs.javascript.ScriptableObject.KeyComparator
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
     
    private static final org.apache.commons.logging.Log
     
     

    Fields inherited from class org.htmlunit.corejs.javascript.ScriptableObject

    CONST, DONTENUM, EMPTY, PERMANENT, READONLY, UNINITIALIZED_CONST

    Fields inherited from interface org.htmlunit.corejs.javascript.Scriptable

    NOT_FOUND
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor.
    Constructs in association with HTMLCanvasElement.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    arc(double x, double y, double radius, double startAngle, double endAngle, boolean anticlockwise)
    Draws an arc.
    void
    arcTo(double x1, double y1, double x2, double y2, double radius)
    Draws an arc.
    void
    Begins the subpaths.
    void
    bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y)
    Draws a cubic Bézier curve.
    void
    clearRect(double x, double y, double w, double h)
    Clears the specified rectangular area.
    static void
    clip(org.htmlunit.corejs.javascript.Context context, org.htmlunit.corejs.javascript.Scriptable scope, org.htmlunit.corejs.javascript.Scriptable thisObj, Object[] args, org.htmlunit.corejs.javascript.Function function)
    Creates a new clipping region.
    void
    Closes the current subpath.
    static ImageData
    createImageData(org.htmlunit.corejs.javascript.Context context, org.htmlunit.corejs.javascript.Scriptable scope, org.htmlunit.corejs.javascript.Scriptable thisObj, Object[] args, org.htmlunit.corejs.javascript.Function function)
    Returns the ImageData object.
    createLinearGradient(double x0, double y0, double r0, double x1, Object y1, Object r1)
    Creates linear gradient.
    void
    Creates a pattern.
    createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1)
    Creates a gradient.
    void
    drawImage(Object image, int sx, int sy, Object sWidth, Object sHeight, Object dx, Object dy, Object dWidth, Object dHeight)
    Draws images onto the canvas.
    void
    ellipse(double x, double y, double radiusX, double radiusY, double rotation, double startAngle, double endAngle, boolean anticlockwise)
    Paints the specified ellipse.
    void
    Fills the shape.
    void
    fillRect(int x, int y, int w, int h)
    Paints the specified rectangular area.
    void
    fillText(String text, double x, double y)
    Fills a given text at the given (x, y) position.
    Returns the associated HTMLCanvasElement.
    Returns the fillStyle property.
    double
    Specifies the alpha (transparency) value that is applied to shapes and images before they are drawn onto the canvas..
    getImageData(int sx, int sy, int sw, int sh)
    Returns the ImageData object.
    void
    Dummy placeholder.
    void
    Dummy placeholder.
    double
    Returns the lineWidth property.
     
    Returns the strokeStyle property.
    void
    Dummy placeholder.
    void
    JavaScript constructor.
    void
    lineTo(double x, double y)
    Connect the last point to the given point.
    Calculate TextMetrics for the given text.
    void
    moveTo(double x, double y)
    Creates a new subpath.
    void
    putImageData(ImageData imageData, int dx, int dy, Object dirtyX, Object dirtyY, Object dirtyWidth, Object dirtyHeight)
    Paints data from the given ImageData object onto the canvas.
    void
    quadraticCurveTo(double controlPointX, double controlPointY, double endPointX, double endPointY)
    Draws a quadratic Bézier curve.
    void
    rect(double x, double y, double w, double h)
    Renders a rectangle.
    void
    Pops state stack and restore state.
    void
    rotate(double angle)
    Adds a rotation to the transformation matrix.
    void
    Pushes state on state stack.
    void
    Changes the transformation matrix to apply a scaling transformation with the given characteristics.
    void
    setFillStyle(String fillStyle)
    Sets the fillStyle property.
    void
    setGlobalAlpha(double globalAlpha)
    Sets the globalAlpha property.
    void
    Dummy placeholder.
    void
    setLineWidth(Object lineWidth)
    Sets the lineWidth property.
    void
    setStrokeStyle(String strokeStyle)
    Sets the strokeStyle property.
    void
    setTransform(double m11, double m12, double m21, double m22, double dx, double dy)
    Resets (overrides) the current transformation to the identity matrix, and then invokes a transformation described by the arguments of this method.
    void
    Calculates the strokes of all the subpaths of the current path.
    void
    strokeRect(int x, int y, int w, int h)
    Strokes the specified rectangular area.
    void
    Dummy placeholder.
    Returns the Data URL.
    void
    transform(double m11, double m12, double m21, double m22, double dx, double dy)
    Multiplies the current transformation with the matrix described by the arguments of this method.
    void
    translate(int x, int y)
    Changes the transformation matrix to apply a translation transformation with the given characteristics.

    Methods inherited from class org.htmlunit.corejs.javascript.ScriptableObject

    applyDescriptorToAttributeBitset, associateValue, avoidObjectDetection, buildDataDescriptor, callMethod, callMethod, checkPropertyChange, checkPropertyDefinition, defineClass, defineClass, defineClass, defineConst, defineConstProperty, defineFunctionProperties, defineOwnProperties, defineOwnProperty, defineOwnProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, delete, delete, delete, deleteProperty, deleteProperty, deleteProperty, ensureScriptable, ensureScriptableObject, ensureSymbolScriptable, get, get, get, getAllIds, getArrayPrototype, getAssociatedValue, getAttributes, getAttributes, getAttributes, getAttributes, getAttributes, getClassPrototype, getDefaultValue, getExternalArrayData, getExternalArrayLength, getFunctionPrototype, getGeneratorFunctionPrototype, getGetterOrSetter, getGetterOrSetter, getIds, getObjectPrototype, getOwnPropertyDescriptor, getParentScope, getProperty, getProperty, getProperty, getPropertyIds, getPrototype, getTopLevelScope, getTopScopeValue, getTypedProperty, getTypedProperty, getTypeOf, has, has, hasProperty, hasProperty, hasProperty, isAccessorDescriptor, isConst, isDataDescriptor, isEmpty, isExtensible, isFalse, isGenericDescriptor, isGetterOrSetter, isSealed, isTrue, preventExtensions, put, put, putConst, putConstProperty, putProperty, putProperty, putProperty, querySlot, redefineProperty, sameValue, sealObject, setAttributes, setAttributes, setAttributes, setAttributes, setAttributes, setCommonDescriptorProperties, setExternalArrayData, setGetterOrSetter, setPrototype, size

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • LOG

      private static final org.apache.commons.logging.Log LOG
    • canvas_

      private HTMLCanvasElement canvas_
    • renderingBackend_

      private RenderingBackend renderingBackend_
  • Constructor Details

    • CanvasRenderingContext2D

      public CanvasRenderingContext2D()
      Default constructor.
    • CanvasRenderingContext2D

      public CanvasRenderingContext2D(HTMLCanvasElement canvas)
      Constructs in association with HTMLCanvasElement.
      Parameters:
      canvas - the HTMLCanvasElement
  • Method Details

    • jsConstructor

      public void jsConstructor()
      JavaScript constructor.
    • getRenderingBackend

      private RenderingBackend getRenderingBackend()
    • getGlobalAlpha

      public double getGlobalAlpha()
      Specifies the alpha (transparency) value that is applied to shapes and images before they are drawn onto the canvas..
      Returns:
      the globalAlpha property
    • setGlobalAlpha

      public void setGlobalAlpha(double globalAlpha)
      Sets the globalAlpha property.
      Parameters:
      globalAlpha - the globalAlpha property
    • getFillStyle

      public Object getFillStyle()
      Returns the fillStyle property.
      Returns:
      the fillStyle property
    • setFillStyle

      public void setFillStyle(String fillStyle)
      Sets the fillStyle property.
      Parameters:
      fillStyle - the fillStyle property
    • getStrokeStyle

      public Object getStrokeStyle()
      Returns the strokeStyle property.
      Returns:
      the strokeStyle property
    • setStrokeStyle

      public void setStrokeStyle(String strokeStyle)
      Sets the strokeStyle property.
      Parameters:
      strokeStyle - the strokeStyle property
    • getLineWidth

      public double getLineWidth()
      Returns the lineWidth property.
      Returns:
      the lineWidth property
    • setLineWidth

      public void setLineWidth(Object lineWidth)
      Sets the lineWidth property.
      Parameters:
      lineWidth - the lineWidth property
    • arc

      public void arc(double x, double y, double radius, double startAngle, double endAngle, boolean anticlockwise)
      Draws an arc.
      Parameters:
      x - the x
      y - the y
      radius - the radius
      startAngle - the start angle
      endAngle - the end angle
      anticlockwise - is anti-clockwise
    • arcTo

      public void arcTo(double x1, double y1, double x2, double y2, double radius)
      Draws an arc.
      Parameters:
      x1 - the x1
      y1 - the y1
      x2 - the x2
      y2 - the y2
      radius - the radius
    • beginPath

      public void beginPath()
      Begins the subpaths.
    • bezierCurveTo

      public void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y)
      Draws a cubic Bézier curve.
      Parameters:
      cp1x - the cp1x
      cp1y - the cp1y
      cp2x - the cp2x
      cp2y - the cp2y
      x - the x
      y - the y
    • clearRect

      public void clearRect(double x, double y, double w, double h)
      Clears the specified rectangular area.
      Parameters:
      x - the x
      y - the y
      w - the width
      h - the height
    • clip

      public static void clip(org.htmlunit.corejs.javascript.Context context, org.htmlunit.corejs.javascript.Scriptable scope, org.htmlunit.corejs.javascript.Scriptable thisObj, Object[] args, org.htmlunit.corejs.javascript.Function function)
      Creates a new clipping region.
      Parameters:
      context - the JavaScript context
      scope - the scope
      thisObj - the scriptable
      args - the arguments passed into the method
      function - the function
    • closePath

      public void closePath()
      Closes the current subpath.
    • createImageData

      public static ImageData createImageData(org.htmlunit.corejs.javascript.Context context, org.htmlunit.corejs.javascript.Scriptable scope, org.htmlunit.corejs.javascript.Scriptable thisObj, Object[] args, org.htmlunit.corejs.javascript.Function function)
      Returns the ImageData object. this may accept a variable number of arguments.
      Parameters:
      context - the JavaScript context
      scope - the scope
      thisObj - the scriptable
      args - the arguments passed into the method
      function - the function
      Returns:
      the ImageData object
    • createLinearGradient

      public CanvasGradient createLinearGradient(double x0, double y0, double r0, double x1, Object y1, Object r1)
      Creates linear gradient.
      Parameters:
      x0 - the x0
      y0 - the y0
      r0 - the r0
      x1 - the x1
      y1 - the y1
      r1 - the r1
      Returns:
      the new CanvasGradient
    • createPattern

      public void createPattern()
      Creates a pattern.
    • createRadialGradient

      public CanvasGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1)
      Creates a gradient.
      Parameters:
      x0 - the x axis of the coordinate of the start circle
      y0 - the y axis of the coordinate of the start circle
      r0 - the radius of the start circle
      x1 - the x axis of the coordinate of the end circle
      y1 - the y axis of the coordinate of the end circle
      r1 - the radius of the end circle
      Returns:
      the new CanvasGradient
    • drawImage

      public void drawImage(Object image, int sx, int sy, Object sWidth, Object sHeight, Object dx, Object dy, Object dWidth, Object dHeight)
      Draws images onto the canvas.
      Parameters:
      image - an element to draw into the context
      sx - the X coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context
      sy - the Y coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context
      sWidth - the width of the sub-rectangle of the source image to draw into the destination context
      sHeight - the height of the sub-rectangle of the source image to draw into the destination context
      dx - the X coordinate in the destination canvas at which to place the top-left corner of the source image
      dy - the Y coordinate in the destination canvas at which to place the top-left corner of the source image
      dWidth - the width to draw the image in the destination canvas. This allows scaling of the drawn image
      dHeight - the height to draw the image in the destination canvas. This allows scaling of the drawn image
    • toDataURL

      public String toDataURL(String type)
      Returns the Data URL.
      Parameters:
      type - an optional type
      Returns:
      the dataURL
    • ellipse

      public void ellipse(double x, double y, double radiusX, double radiusY, double rotation, double startAngle, double endAngle, boolean anticlockwise)
      Paints the specified ellipse.
      Parameters:
      x - the x
      y - the y
      radiusX - the radiusX
      radiusY - the radiusY
      rotation - the rotation
      startAngle - the startAngle
      endAngle - the endAngle
      anticlockwise - the anticlockwise
    • fill

      public void fill()
      Fills the shape.
    • fillRect

      public void fillRect(int x, int y, int w, int h)
      Paints the specified rectangular area.
      Parameters:
      x - the x
      y - the y
      w - the width
      h - the height
    • fillText

      public void fillText(String text, double x, double y)
      Fills a given text at the given (x, y) position.
      Parameters:
      text - the text
      x - the x
      y - the y
    • getImageData

      public ImageData getImageData(int sx, int sy, int sw, int sh)
      Returns the ImageData object.
      Parameters:
      sx - x
      sy - y
      sw - width
      sh - height
      Returns:
      the ImageData object
    • getLineDash

      public void getLineDash()
      Dummy placeholder.
    • getLineData

      public void getLineData()
      Dummy placeholder.
    • isPointInPath

      public void isPointInPath()
      Dummy placeholder.
    • lineTo

      public void lineTo(double x, double y)
      Connect the last point to the given point.
      Parameters:
      x - the x
      y - the y
    • measureText

      public TextMetrics measureText(Object text)
      Calculate TextMetrics for the given text.
      Parameters:
      text - the text to measure
      Returns:
      the text metrics
    • moveTo

      public void moveTo(double x, double y)
      Creates a new subpath.
      Parameters:
      x - the x
      y - the y
    • putImageData

      public void putImageData(ImageData imageData, int dx, int dy, Object dirtyX, Object dirtyY, Object dirtyWidth, Object dirtyHeight)
      Paints data from the given ImageData object onto the canvas.
      Parameters:
      imageData - an ImageData object containing the array of pixel values
      dx - horizontal position (x coordinate) at which to place the image data in the destination canvas
      dy - vertical position (y coordinate) at which to place the image data in the destination canvas
      dirtyX - horizontal position (x coordinate) of the top-left corner from which the image data will be extracted. Defaults to 0.
      dirtyY - vertical position (y coordinate) of the top-left corner from which the image data will be extracted. Defaults to 0.
      dirtyWidth - width of the rectangle to be painted. Defaults to the width of the image data.
      dirtyHeight - height of the rectangle to be painted. Defaults to the height of the image data.
    • quadraticCurveTo

      public void quadraticCurveTo(double controlPointX, double controlPointY, double endPointX, double endPointY)
      Draws a quadratic Bézier curve.
      Parameters:
      controlPointX - the x-coordinate of the control point
      controlPointY - the y-coordinate of the control point
      endPointX - the x-coordinate of the end point
      endPointY - the y-coordinate of the end point
    • rect

      public void rect(double x, double y, double w, double h)
      Renders a rectangle.
      Parameters:
      x - the x
      y - the y
      w - the width
      h - the height
    • restore

      public void restore()
      Pops state stack and restore state.
    • rotate

      public void rotate(double angle)
      Adds a rotation to the transformation matrix.
      Parameters:
      angle - the angle
    • save

      public void save()
      Pushes state on state stack.
    • scale

      public void scale(Object x, Object y)
      Changes the transformation matrix to apply a scaling transformation with the given characteristics.
      Parameters:
      x - the scale factor in the horizontal direction
      y - the scale factor in the vertical direction
    • setLineDash

      public void setLineDash()
      Dummy placeholder.
    • setTransform

      public void setTransform(double m11, double m12, double m21, double m22, double dx, double dy)
      Resets (overrides) the current transformation to the identity matrix, and then invokes a transformation described by the arguments of this method. This lets you scale, rotate, translate (move), and skew the context.
      Parameters:
      m11 - Horizontal scaling. A value of 1 results in no scaling
      m12 - Vertical skewing
      m21 - Horizontal skewing
      m22 - Vertical scaling. A value of 1 results in no scaling
      dx - Horizontal translation (moving)
      dy - Vertical translation (moving).
    • stroke

      public void stroke()
      Calculates the strokes of all the subpaths of the current path.
    • strokeRect

      public void strokeRect(int x, int y, int w, int h)
      Strokes the specified rectangular area.
      Parameters:
      x - the x
      y - the y
      w - the width
      h - the height
    • strokeText

      public void strokeText()
      Dummy placeholder.
    • transform

      public void transform(double m11, double m12, double m21, double m22, double dx, double dy)
      Multiplies the current transformation with the matrix described by the arguments of this method. This lets you scale, rotate, translate (move), and skew the context.
      Parameters:
      m11 - Horizontal scaling. A value of 1 results in no scaling
      m12 - Vertical skewing
      m21 - Horizontal skewing
      m22 - Vertical scaling. A value of 1 results in no scaling
      dx - Horizontal translation (moving)
      dy - Vertical translation (moving).
    • translate

      public void translate(int x, int y)
      Changes the transformation matrix to apply a translation transformation with the given characteristics.
      Parameters:
      x - the translation distance in the horizontal direction
      y - the translation distance in the vertical direction
    • getCanvas

      public HTMLCanvasElement getCanvas()
      Returns the associated HTMLCanvasElement.
      Returns:
      the associated HTMLCanvasElement