Export (0) Print
Expand All

CanvasRenderingContext2D object

Provides an object that represents a two-dimensional surface that has an origin (0,0) at the upper-left corner, with x-coordinates increasing as you move right and y-coordinates increasing as you move down.

HTML Canvas 2D Context, Section 1Internet Explorer 9

Syntax


<body>
  <canvas id="myCanvas">No canvas support</canvas>
  <script>
    //  get canvas object
    var canvas=document.getElementById("myCanvas");
    //  get rendering context
    var ctx=canvas.getContext("2d");


Members

The CanvasRenderingContext2D object has these types of members:

Methods

The CanvasRenderingContext2D object has these methods.

MethodDescription
arc

Adds points to a path that represents an arc.

arcTo

Draws an arc of a fixed radius between two tangents that are defined by the current point in a path and two additional points.

beginPath

Resets the current path.

bezierCurveTo

Adds a point to the current subpath by using the specified control points that represent a cubic Bézier curve.

clearRect

Clears the pixels on a CanvasRenderingContext2D object within a given rectangle.

clip

Specifies a new clipping region.

closePath

Closes the current subpath and starts a new subpath that has a start point that is equal to the end of the closed subpath.

createImageData

Returns a CanvasImageData object with the given dimensions in CSS pixels.

createLinearGradient

Creates an object that represents a linear gradient to use in a canvas context.

createPattern

Returns a CanvasPattern object that repeats the specified element in the specified direction.

createRadialGradient

Returns an object that represents a radial or circular gradient to use in a canvas context.

drawImage

Draws a specified image onto a canvas.

fill

Fills subpaths by using the current fill style.

fillRect

Paints a rectangle onto a CanvasRenderingContext2D object by using the current fill style.

fillText

Renders filled text to the canvas by using the current fill style and font.

getImageData

Returns an ICanvasImageData object that represents the pixel data for the specified rectangle on a canvas. Use the data property to get a CanvasPixelArray to manipulate individual pixel data.

getLineDash

Gets the current dash list sequence.

isPointInPath

Determines if the specified point is in the current path.

lineTo

Adds a new point to a subpath and connects that point to the last point in the subpath by using a straight line.

measureText

Returns a CanvasTextMetrics object that contains the width of the specified text.

moveTo

Creates a new subpath by using the specified point.

putImageData

Paints the data from a specified CanvasImageData object onto a canvas.

quadraticCurveTo

Adds a point to the current subpath by using the specified control points that represent a quadratic Bézier curve.

rect

Creates a new closed rectangular subpath.

restore

Returns previously saved CanvasRenderingContext2D path state and attributes.

rotate

Rotates the current context coordinates (that is, a transformation matrix).

save

Saves the state of the current context.

scale

Scales the current context by the specified horizontal (x) and vertical (y) factors.

setLineDash

Sets the current line dash pattern. The argument is a even numbered list of distances to alternately produce a line and a space.

setTransform

Resets the current transformation matrix of the context back to the identity matrix and then multiplies it by the specified matrix.

stroke

Renders the strokes of the current subpath by using the current stroke styles.

strokeRect

Creates an outline of the specified rectangle on a canvas by using the current stroke, line width, and join styles.

strokeText

Renders the specified text at the specified position by using the current font, lineWidth, and strokeStyle property.

transform

Modifies the transformation matrix of the current context.

translate

Specifies values to move the origin point in a canvas.

 

Properties

The CanvasRenderingContext2D object has these properties.

PropertyAccess typeDescription

canvas

Gets a back reference to the canvas object that the current context derives from.

fillStyle

Gets or sets the current style that is used to fill shapes.

font

Read/write

Gets or sets the current font for the context.

globalAlpha

Gets or sets the current alpha or transparency value that is applied to global composite rendering operations.

globalCompositeOperation

Gets or sets a value that indicates how source images are drawn onto a destination image.

lineCap

Gets or sets the current line cap style.

lineDashOffset

Read/write

Sets or gets the phase offset of the current line dash pattern. Can be changed to produce "marching ants" animation like marching ants.

lineJoin

Gets or sets the type of corner that is created when two lines meet.

lineWidth

Gets or sets the current line width, in pixels.

miterLimit

Gets or sets the maximum allowed ratio between half of the lineWidth value and the miter length.

msFillRule

Read/write

Gets or sets the fill rule used to check if a point is inside a shape when filling.

msImageSmoothingEnabled

Read/write

Choose bilinear or nearest-neighbor image smoothing when images are resized.

shadowBlur

Read/write

Gets or sets the current level of blur that is applied to shadows.

shadowColor

Read/write

Gets or sets the color to use for shadows.

shadowOffsetX

Read/write

Gets or sets the horizontal distance of a shadow from a shape.

shadowOffsetY

Read/write

Gets or sets the vertical distance of a shadow from a shape.

strokeStyle

Read/write

Gets or sets the current style that is used for strokes (lines) on shapes.

textAlign

Gets or sets the current anchor point or alignment settings for text in the current context.

textBaseline

Gets or sets the current settings for the font baseline alignment.

 

Standards information

Remarks

The CanvasRenderingContext2D object provides most of the drawing methods and properties that are used with the canvas object. Use getContext method to get a context object for a canvas.

Note  A canvas object only supports one context. Additional calls to getContext this object return the same context.

Note  If you are developing on an intranet and have rendering issues for HTML5, you can add a "meta http-equiv-'X-UA-Compatible' content= " meta command, followed by "IE=edge" to the <head> block of a webpage to force Windows Internet Explorer to use the latest standards. For more information about document compatibility, see Defining Document Compatibility.

Examples

This example creates a canvas on a page and uses CanvasRenderingContext2D and associated methods and attributes to add three rectangles to it. See the sample in action.


<!DOCTYPE html>
<html>
<head>
    <title>Canvas test</title>
</head>
<body>
    <h1>Simple Canvas example</h1>
    <canvas id="myCanvas" width="300" height="300" >
       Canvas is not supported on this browser or in this mode
    </canvas>

    <script>
      var canvas = document.getElementById("myCanvas");
      //  if the canvas element exists 
      if (canvas.getContext) {
        // Draw some rectangles with different color values
        var ctx = canvas.getContext("2d");
        ctx.fillStyle = "red";
        ctx.fillRect(5, 5, 200, 200);
        ctx.fillStyle = "rgba(0,50,200,0.5)";
        ctx.fillRect(55, 55, 200, 200);
        ctx.fillStyle = "rgba(0,255,10,0.5)";
        ctx.fillRect(100, 100, 200, 200);
      }
    </script>

</body>
</html>


See also

canvas

 

 

Show:
© 2014 Microsoft