Canvas API
- Nakaraang Pahina Style ng HTML
- Susunod na Pahina API Console
Ang elemento <canvas> ay nagtatalaga ng lugar ng bitmap sa pahina ng HTML.
Ang Canvas API ay nagbibigay ng kapangyarihan sa JavaScript na magpahayag ng mga图形 sa layunin.
Ang Canvas API ay nagbibigay ng kakayahan sa JavaScript na magpahayag ng mga hugis, linya, kurva, kahon, teksto at imahe, pati na rin ang kulay, pag-rotasyon, transparensya at iba pang operasyon sa pixel.
Idagdag ang Canvas sa HTML
Maaari kang magdagdag ng elemento canvas sa anumang lugar ng pahina sa HTML gamit ang tag <canvas>:
Instance
<canvas id="myCanvas" width="300" height="150"></canvas>
Paano abutin ang elemento Canvas
Maaari kang gumamit ng 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");
Notes
The HTML <canvas> element itself does not have drawing capabilities.
You must use JavaScript to draw any graphics.
The getContext() 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 located at position 20,20. The rectangle is 150 pixels wide and 100 pixels high:
Instance
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:
Instance
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:
Instance
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()
Instance
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
Atributo | Paglalarawan |
---|---|
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. |
Paraan | Paglalarawan |
---|---|
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
Atributo | Paglalarawan |
---|---|
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
Paraan | Paglalarawan |
---|---|
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
Paraan | Paglalarawan |
---|---|
fill() | Fill the current graphics (path). |
stroke() | Actually draw the path you define. |
beginPath() | Start the 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 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
Paraan | Paglalarawan |
---|---|
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
Atributo | Paglalarawan |
---|---|
font | Set or return the current font attribute of the text content. |
textAlign | Set or return the current alignment style of the text content. |
textBaseline | I-set o ibalik ang kasalukuyang teksto baseline na ginagamit sa pagdraw ng teksto. |
Paraan | Paglalarawan |
---|---|
fillText() | Idraw ang teksto na may ibon sa kanvas. |
strokeText() | Idraw ang teksto sa kanvas (walang ibon). |
measureText() | I-balik ang object na naglalaman ng lapad ng teksto na tinukoy. |
Pagdraw ng imahe
Paraan | Paglalarawan |
---|---|
drawImage() | Idraw ang imahe, kanvas o video sa kanvas. |
Operasyon sa pixel
Atributo | Paglalarawan |
---|---|
width | I-balik ang lapad ng ImageData object. |
height | I-balik ang taas ng ImageData object. |
data | I-balik ang object na naglalaman ng datos ng imahe ng tinukoy na ImageData object. |
Paraan | Paglalarawan |
---|---|
createImageData() | Lumikha ng bagong walang laman na ImageData object. |
getImageData() | I-balik ang ImageData object, na kopya ng pixel data ng tinukoy na rectangulo sa kanvas. |
putImageData() | I-balik ang mga datos ng imahe (mula sa tinukoy na ImageData object) sa kanvas. |
Pagsasama
Atributo | Paglalarawan |
---|---|
globalAlpha | I-set o ibalik ang kasalukuyang alpha o透明度 ng pagpipinta. |
globalCompositeOperation | I-set o ibalik kung paano ialin ang bagong imahe sa kasalukuyang imahe. |
Iba pang
Paraan | Paglalarawan |
---|---|
save() | I-save ang kasalukuyang kalagayan ng konteksto. |
restore() | Bumalik sa dati na kalagayan at mga atributo ng path. |
createEvent() | |
getContext() | |
toDataURL() |
Pamantayang Atributo at Kaganapan
Ang canvas object ay sumusuporta sa mga pamantayangAtributoatKaganapan.
Mga Kaugnay na Pahina
Tuturuan sa HTML:Canvas ng HTML5
Tuturuan sa Imaheng HTML:Tuturuan sa HTML Canvas
Manwal ng HTML:Tag <canvas> ng HTML
- Nakaraang Pahina Style ng HTML
- Susunod na Pahina API Console