HTML DOM Canvas 对象
Ang elemento <canvas> ay nagtatalaga ng larawang bilang na rehiyon sa pahina ng HTML.
Ang Canvas API ay nagbibigay ng kapangyarihan sa JavaScript na magpahayag ng mga grapik sa kanvas.
Ang Canvas API ay nagbibigay ng kakayahan sa JavaScript na magpahayag ng mga grapik sa kanvas.
Idagdag ang Canvas sa HTML
Maaari mong magdagdag ng elemento canvas sa anumang lugar ng pahina sa HTML gamit ang tag <canvas>:
Example
<canvas id="myCanvas" width="300" height="150"></canvas>
Paano abutin ang elemento Canvas
Maaari mong gamit ang HTML DOM method na getElementById() upang abutin ang elemento <canvas>:
const myCanvas = document.getElementById("myCanvas");
To draw on the canvas, you need to create a 2D context object:
const ctx = myCanvas.getContext("2d");
Note
The HTML <canvas> element itself does not have drawing capabilities.
You must use JavaScript to draw any graphics.
The getContextMenu() method returns an object with drawing tools (methods).
Drawing on the canvas
After creating the 2D context object, you can draw on the canvas.
The following fillRect() method draws a black rectangle at position 20,20. The rectangle is 150 pixels wide and 100 pixels high:
Example
const myCanvas = document.getElementById("myCanvas"); const ctx = myCanvas.getContext("2d"); ctx.fillRect(20, 20, 150, 100);
Using color
The fillStyle property sets the fill color of the drawing object:
Example
const myCanvas = document.getElementById("myCanvas"); const ctx = myCanvas.getContext("2d"); ctx.fillStyle = "red"; ctx.fillRect(20, 20, 150, 100);
You can also use the document.createElement() method to create a new <canvas> element and add it to an existing HTML page:
Example
const myCanvas = document.createElement("canvas"); document.body.appendChild(myCanvas); const ctx = myCanvas.getContext("2d"); ctx.fillStyle = "red"; ctx.fillRect(20, 20, 150, 100);
Path
Common methods for drawing on the canvas are:
- Start a path - beginPath()
- Move to a point - moveTo()
- Draw in the path - lineTo()
- Draw a path - stroke()
Example
const canvas = document.getElementById("myCanvas"); const ctx = canvas.getContext("2d"); ctx.beginPath(); ctx.moveTo(20, 20); ctx.lineTo(20, 100); ctx.lineTo(70, 100); ctx.stroke();
Color, style, and shadow
属性 | 描述 |
---|---|
fillStyle | Set or return the color, gradient, or pattern used for filling the drawing. |
strokeStyle | Set or return the color, gradient, or pattern used for strokes. |
shadowColor | Set or return the color used for the shadow. |
shadowBlur | Set or return the blur level of the shadow. |
shadowOffsetX | Set or return the horizontal distance of the shadow to the shape. |
shadowOffsetY | Set or return the vertical distance of the shadow to the shape. |
方法 | 描述 |
---|---|
createLinearGradient() | Create a linear gradient (for canvas content). |
createPattern() | Repeat the specified element in the specified direction. |
createRadialGradient() | Create a radial/circular gradient (for canvas content). |
addColorStop() | Specify the color and stop position in the gradient object. |
Line style
属性 | 描述 |
---|---|
lineCap | Set or return the line cap style. |
lineJoin | Set or return the type of angle created when two lines intersect. |
lineWidth | Set or return the current line width. |
miterLimit | Set or return the maximum miter length. |
Rectangle
方法 | 描述 |
---|---|
rect() | Create a rectangle. |
fillRect() | Draw a 'filled' rectangle. |
strokeRect() | Draw a rectangle (no fill). |
clearRect() | Clear the specified pixels within the given rectangle. |
Path
方法 | 描述 |
---|---|
fill() | Fill the current graphics (path). |
stroke() | Actually draw the path you define. |
beginPath() | Start a path or reset the current path. |
moveTo() | Move the path to the specified point on the canvas without creating a line. |
closePath() | Create a path from the current point back to the starting point. |
lineTo() | Add a new point and create a line from that point to the last specified point on the canvas. |
clip() | Clip any shape and size of the area from the original canvas. |
quadraticCurveTo() | Create a quadratic Bezier curve. |
bezierCurveTo() | Create a cubic Bezier curve. |
arc() | Create an arc/curve (used to create a circle or part of a circle). |
arcTo() | Create an arc/curve between two tangents. |
isPointInPath() | Returns true if the specified point is in the current path, otherwise returns false. |
Transformation
方法 | 描述 |
---|---|
scale() | Enlarge or shrink the current graphics. |
rotate() | Rotate the current graphics. |
translate() | Remap the (0,0) position on the canvas. |
transform() | Replace the current drawing transformation matrix. |
setTransform() | Reset the current transformation to the unit matrix. Then run transform()。 |
Text
属性 | 描述 |
---|---|
font | Set or return the current font properties of the text content. |
textAlign | Set or return the current alignment style of the text content. |
textBaseline | 设置或返回绘制文本时使用的当前文本基线。 |
方法 | 描述 |
---|---|
fillText() | 在画布上绘制“填充”文本。 |
strokeText() | 在画布上绘制文本(无填充)。 |
measureText() | 返回包含指定文本宽度的对象。 |
图像绘制
方法 | 描述 |
---|---|
drawImage() | 在画布上绘制图像、画布或视频。 |
像素操作
属性 | 描述 |
---|---|
width | 返回 ImageData 对象的宽度。 |
height | 返回 ImageData 对象的高度。 |
data | 返回包含指定 ImageData 对象的图像数据的对象。 |
方法 | 描述 |
---|---|
createImageData() | 创建新的空白 ImageData 对象。 |
getImageData() | 返回 ImageData 对象,该对象复制画布上指定矩形的像素数据。 |
putImageData() | 将图像数据(来自指定的 ImageData 对象)放回画布上。 |
合成
属性 | 描述 |
---|---|
globalAlpha | 设置或返回绘图的当前 alpha 或透明度值。 |
globalCompositeOperation | 设置或返回如何将新图像绘制到现有图像上。 |
其他
方法 | 描述 |
---|---|
save() | 保存当前上下文的状态。 |
restore() | 返回之前保存的路径状态和属性。 |
createEvent() | |
getContext() | |
toDataURL() |