La interfaz CanvasRenderingContext2D proporciona el contexto de renderizado 2D para la superficie de dibujo de un elemento {{ HTMLElement("canvas") }}.
Para obtener un objeto de esta interfaz, llama a {{domxref("HTMLCanvasElement.getContext()", "getContext()")}} en un <canvas>, proporcionando "2d" como argumento:
var canvas = document.getElementById('mycanvas');
var ctx = canvas.getContext('2d');
Una vez que tengas el contexto de renderizado 2D para un canvas, podrás dibujar en ella. Por ejemplo:
Establece todos los píxeles del rectangulo definido por los puntos (x, y) y tamaños (ancho, alto) a negro transparente, borrando cualquier contenido previo.
Creates a pattern using the specified image (a {{domxref("CanvasImageSource")}}). It repeats the source in the directions specified by the repetition argument. This method returns a {{domxref("CanvasPattern")}}.
Causes the point of the pen to move back to the start of the current sub-path. It tries to draw a straight line from the current point to the start. If the shape has already been closed or has only one point, this function does nothing.
{{domxref("CanvasRenderingContext2D.moveTo()")}}
Moves the starting point of a new sub-path to the (x, y) coordinates.
{{domxref("CanvasRenderingContext2D.lineTo()")}}
Connects the last point in the subpath to the x, y coordinates with a straight line.
Adds a cubic Bézier curve to the path. It requires three points. The first two points are control points and the third one is the end point. The starting point is the last point in the current path, which can be changed using moveTo() before creating the Bézier curve.
Adds a quadratic Bézier curve to the current path.
{{domxref("CanvasRenderingContext2D.arc()")}}
Adds an arc to the path which is centered at (x, y) position with radius r starting at startAngle and ending at endAngle going in the given direction by anticlockwise (defaulting to clockwise).
{{domxref("CanvasRenderingContext2D.arcTo()")}}
Adds an arc to the path with the given control points and radius, connected to the previous point by a straight line.
Adds an ellipse to the path which is centered at (x, y) position with the radii radiusX and radiusY starting at startAngle and ending at endAngle going in the given direction by anticlockwise (defaulting to clockwise).
{{domxref("CanvasRenderingContext2D.rect()")}}
Creates a path for a rectangle atposition (x, y) with a size that is determined by width and height.
Drawing paths
{{domxref("CanvasRenderingContext2D.fill()")}}
Fills the subpaths with the current fill style.
{{domxref("CanvasRenderingContext2D.stroke()")}}
Strokes the subpaths with the current stroke style.
Scrolls the current path or a given path into the view.
{{domxref("CanvasRenderingContext2D.clip()")}}
Creates a clipping path from the current sub-paths. Everything drawn after clip() is called appears inside the clipping path only. For an example, see Clipping paths in the Canvas tutorial.
Reports whether or not the specified point is inside the area contained by the stroking of a path.
Transformations
Objects in the CanvasRenderingContext2D rendering context have a current transformation matrix and methods to manipulate it. The transformation matrix is applied when creating the current default path, painting text, shapes and {{domxref("Path2D")}} objects. The methods listed below remain for historical and compatibility reasons as {{domxref("SVGMatrix")}} objects are used in most parts of the API nowadays and will be used in the future instead.
Returns an {{domxref("ImageData")}} object representing the underlying pixel data for the area of the canvas denoted by the rectangle which starts at (sx, sy) and has an sw width and sh height.
Paints data from the given {{domxref("ImageData")}} object onto the bitmap. If a dirty rectangle is provided, only the pixels from that rectangle are painted.
Image smoothing mode; if disabled, images will not be smoothed if scaled.
The canvas state
The CanvasRenderingContext2D rendering context contains a variety of drawing style states (attributes for line styles, fill styles, shadow styles, text styles). The following methods help you to work with that state:
{{domxref("CanvasRenderingContext2D.save()")}}
Saves the current drawing style state using a stack so you can revert any change you make to it using restore().
{{domxref("CanvasRenderingContext2D.restore()")}}
Restores the drawing style state to the last element on the 'state stack' saved by save().
{{domxref("CanvasRenderingContext2D.canvas")}}
A read-only back-reference to the {{domxref("HTMLCanvasElement")}}. Might be {{jsxref("null")}} if it is not associated with a {{HTMLElement("canvas")}} element.
Inspired by the same WebGLRenderingContext method it returns an Canvas2DContextAttributes object that contains the attributes "storage" to indicate which storage is used ("persistent" by default) and the attribute "alpha" (true by default) to indicate that transparency is used in the canvas.
An array which specifies the lengths of alternating dashes and gaps {{ gecko_minversion_inline("7.0") }}. Use {{domxref("CanvasRenderingContext2D.getLineDash()")}} and {{domxref("CanvasRenderingContext2D.setLineDash()")}} instead.
Specifies where to start a dash array on a line. {{ gecko_minversion_inline("7.0") }}. Use {{domxref("CanvasRenderingContext2D.lineDashOffset")}} instead.
This method was introduced in Gecko 1.9 and is removed starting with Gecko 7.0. Use {{domxref("CanvasRenderingContext2D.strokeText()")}} or {{domxref("CanvasRenderingContext2D.fillText()")}} instead.
This method was introduced in Gecko 1.9 and is unimplemented starting with Gecko 7.0. Use {{domxref("CanvasRenderingContext2D.measureText()")}} instead.
Starting with Gecko 5.0 {{geckoRelease("5.0")}}, specifying invalid values are now silently ignored for the following methods and properties: translate(), transform(), rotate(), scale(),rect(), clearRect(), fillRect(), strokeRect(), lineTo(), moveTo(), quadraticCurveTo(), arc(), shadowOffsetX, shadowOffsetY, shadowBlur.
See also
{{domxref("HTMLCanvasElement")}}
G
M
T
La función de sonido está limitada a 200 caracteres