天天看點

HTML5 界面元素 Canvas 參考手冊

HTML5 界面元素 Canvas 參考手冊

<a target="_blank" href="http://blog.csdn.net/opengl_es">轉載請保留此句:太陽火神的美麗人生 -  本部落格專注于 靈活開發及移動和物聯裝置研究:iOS、Android、Html5、Arduino、pcDuino,否則,出自本部落格的文章拒絕轉載或再轉載,謝謝合作。</a>

<a target="_blank" href="http://www.w3schools.com/tags/ref_canvas.asp">HTML5 界面元素 Canvas 參考手冊</a>

<a target="_blank" href="http://www.w3schools.com/tags/ref_canvas.asp">HTML Canvas Reference</a>

描述

Description

The HTML5 &lt;canvas&gt; tag is used to draw graphics, on the fly, via scripting (usually JavaScript).

However, the &lt;canvas&gt; element has no drawing abilities of its own (it is only a container for graphics) - you must use a script to actually draw the graphics.

The getContext() method returns an object that provides methods and properties for drawing on the canvas.

This reference will cover the properties and methods of the getContext("2d") object, which can be used to draw text, lines, boxes, circles, and more - on the canvas.

浏覽器支援

Browser Support

Internet Explorer 9, Firefox, Opera, Chrome, and Safari support &lt;canvas&gt; and its properties and methods.

Note: Internet Explorer 8 and earlier versions, do not support the &lt;canvas&gt; element.

顔色、樣式和陰影

Colors, Styles, and Shadows

Property

<a target="_blank" href="http://www.w3schools.com/tags/canvas_fillstyle.asp">fillStyle</a>

Sets or returns the color, gradient, or pattern used to fill the drawing

<a target="_blank" href="http://www.w3schools.com/tags/canvas_strokestyle.asp">strokeStyle</a>

Sets or returns the color, gradient, or pattern used for strokes

<a target="_blank" href="http://www.w3schools.com/tags/canvas_shadowcolor.asp">shadowColor</a>

Sets or returns the color to use for shadows

<a target="_blank" href="http://www.w3schools.com/tags/canvas_shadowblur.asp">shadowBlur</a>

Sets or returns the blur level for shadows

<a target="_blank" href="http://www.w3schools.com/tags/canvas_shadowoffsetx.asp">shadowOffsetX</a>

Sets or returns the horizontal distance of the shadow from the shape

<a target="_blank" href="http://www.w3schools.com/tags/canvas_shadowoffsety.asp">shadowOffsetY</a>

Sets or returns the vertical distance of the shadow from the shape

Method

<a target="_blank" href="http://www.w3schools.com/tags/canvas_createlineargradient.asp">createLinearGradient()</a>

Creates a linear gradient (to use on canvas content)

<a target="_blank" href="http://www.w3schools.com/tags/canvas_createpattern.asp">createPattern()</a>

Repeats a specified element in the specified direction

<a target="_blank" href="http://www.w3schools.com/tags/canvas_createradialgradient.asp">createRadialGradient()</a>

Creates a radial/circular gradient (to use on canvas content)

<a target="_blank" href="http://www.w3schools.com/tags/canvas_addcolorstop.asp">addColorStop()</a>

Specifies the colors and stop positions in a gradient object

線條樣式

Line Styles

<a target="_blank" href="http://www.w3schools.com/tags/canvas_linecap.asp">lineCap</a>

Sets or returns the style of the end caps for a line

<a target="_blank" href="http://www.w3schools.com/tags/canvas_linejoin.asp">lineJoin</a>

Sets or returns the type of corner created, when two lines meet

<a target="_blank" href="http://www.w3schools.com/tags/canvas_linewidth.asp">lineWidth</a>

Sets or returns the current line width

<a target="_blank" href="http://www.w3schools.com/tags/canvas_miterlimit.asp">miterLimit</a>

Sets or returns the maximum miter length

矩形

Rectangles

<a target="_blank" href="http://www.w3schools.com/tags/canvas_rect.asp">rect()</a>

Creates a rectangle

<a target="_blank" href="http://www.w3schools.com/tags/canvas_fillrect.asp">fillRect()</a>

Draws a "filled" rectangle

<a target="_blank" href="http://www.w3schools.com/tags/canvas_strokerect.asp">strokeRect()</a>

Draws a rectangle (no fill)

<a target="_blank" href="http://www.w3schools.com/tags/canvas_clearrect.asp">clearRect()</a>

Clears the specified pixels within a given rectangle

路徑

Paths

<a target="_blank" href="http://www.w3schools.com/tags/canvas_fill.asp">fill()</a>

Fills the current drawing (path)

<a target="_blank" href="http://www.w3schools.com/tags/canvas_stroke.asp">stroke()</a>

Actually draws the path you have defined

<a target="_blank" href="http://www.w3schools.com/tags/canvas_beginpath.asp">beginPath()</a>

Begins a path, or resets the current path

<a target="_blank" href="http://www.w3schools.com/tags/canvas_moveto.asp">moveTo()</a>

Moves the path to the specified point in the canvas, without creating a line

<a target="_blank" href="http://www.w3schools.com/tags/canvas_closepath.asp">closePath()</a>

Creates a path from the current point back to the starting point

<a target="_blank" href="http://www.w3schools.com/tags/canvas_lineto.asp">lineTo()</a>

Adds a new point and creates a line from that point to the last specified point in the canvas

<a target="_blank" href="http://www.w3schools.com/tags/canvas_clip.asp">clip()</a>

Clips a region of any shape and size from the original canvas

<a target="_blank" href="http://www.w3schools.com/tags/canvas_quadraticcurveto.asp">quadraticCurveTo()</a>

Creates a quadratic Bézier curve

<a target="_blank" href="http://www.w3schools.com/tags/canvas_beziercurveto.asp">bezierCurveTo()</a>

Creates a cubic Bézier curve

<a target="_blank" href="http://www.w3schools.com/tags/canvas_arc.asp">arc()</a>

Creates an arc/curve (used to create circles, or parts of circles)

<a target="_blank" href="http://www.w3schools.com/tags/canvas_arcto.asp">arcTo()</a>

Creates an arc/curve between two tangents

<a target="_blank" href="http://www.w3schools.com/tags/canvas_ispointinpath.asp">isPointInPath()</a>

Returns true if the specified point is in the current path, otherwise false

轉換

Transformations

<a target="_blank" href="http://www.w3schools.com/tags/canvas_scale.asp">scale()</a>

Scales the current drawing bigger or smaller

<a target="_blank" href="http://www.w3schools.com/tags/canvas_rotate.asp">rotate()</a>

Rotates the current drawing

<a target="_blank" href="http://www.w3schools.com/tags/canvas_translate.asp">translate()</a>

Remaps the (0,0) position on the canvas

<a target="_blank" href="http://www.w3schools.com/tags/canvas_transform.asp">transform()</a>

Replaces the current transformation matrix for the drawing

<a target="_blank" href="http://www.w3schools.com/tags/canvas_settransform.asp">setTransform()</a>

文本

Text

<a target="_blank" href="http://www.w3schools.com/tags/canvas_font.asp">font</a>

Sets or returns the current font properties for text content

<a target="_blank" href="http://www.w3schools.com/tags/canvas_textalign.asp">textAlign</a>

Sets or returns the current alignment for text content

<a target="_blank" href="http://www.w3schools.com/tags/canvas_textbaseline.asp">textBaseline</a>

Sets or returns the current text baseline used when drawing text

<a target="_blank" href="http://www.w3schools.com/tags/canvas_filltext.asp">fillText()</a>

Draws "filled" text on the canvas

<a target="_blank" href="http://www.w3schools.com/tags/canvas_stroketext.asp">strokeText()</a>

Draws text on the canvas (no fill)

<a target="_blank" href="http://www.w3schools.com/tags/canvas_measuretext.asp">measureText()</a>

Returns an object that contains the width of the specified text

圖像繪制

Image Drawing

<a target="_blank" href="http://www.w3schools.com/tags/canvas_drawimage.asp">drawImage()</a>

Draws an image, canvas, or video onto the canvas

像素操作

Pixel Manipulation

<a target="_blank" href="http://www.w3schools.com/tags/canvas_imagedata_width.asp">width</a>

Returns the width of an ImageData object

<a target="_blank" href="http://www.w3schools.com/tags/canvas_imagedata_height.asp">height</a>

Returns the height of an ImageData object

<a target="_blank" href="http://www.w3schools.com/tags/canvas_imagedata_data.asp">data</a>

Returns an object that contains image data of a specified ImageData object

<a target="_blank" href="http://www.w3schools.com/tags/canvas_createimagedata.asp">createImageData()</a>

Creates a new, blank ImageData object

<a target="_blank" href="http://www.w3schools.com/tags/canvas_getimagedata.asp">getImageData()</a>

Returns an ImageData object that copies the pixel data for the specified rectangle on a canvas

<a target="_blank" href="http://www.w3schools.com/tags/canvas_putimagedata.asp">putImageData()</a>

Puts the image data (from a specified ImageData object) back onto the canvas

混合

Compositing

<a target="_blank" href="http://www.w3schools.com/tags/canvas_globalalpha.asp">globalAlpha</a>

Sets or returns the current alpha or transparency value of the drawing

<a target="_blank" href="http://www.w3schools.com/tags/canvas_globalcompositeoperation.asp">globalCompositeOperation</a>

Sets or returns how a new image are drawn onto an existing image

其它

Other

save()

Saves the state of the current context

restore()

Returns previously saved path state and attributes

createEvent()

getContext()

toDataURL()

繼續閱讀