导航
×
   ❮   
HTML CSS JavaScript PHP Go Sass W3C Colors ECMS

JS 参考手册

JS 参考手册(字母排序)

JavaScript

JS Arrays JS Boolean JS Classes JS Dates JS Error JS Global JS Iterators JS JSON JS Maps JS Math JS Numbers JS Objects JS Operators JS Assignment JS Arithmetic JS Comparison JS Logical Operators JS Bitwise Operators JS Misc Operators JS Precedence JS Promises JS RegExp Patterns JS RegExp Reference JS Sets JS Statements JS Strings JS Typed Arrays JS Typed Reference

Window

Window Object Window Console Window History Window Location Window Navigator Window Screen

HTML DOM

HTML Documents HTML Elements HTML Attributes HTML Collection HTML NodeList HTML DOMTokenList HTML Styles
alignContent alignItems alignSelf animation animationDelay animationDirection animationDuration animationFillMode animationIterationCount animationName animationTimingFunction animationPlayState background backgroundAttachment backgroundClip backgroundColor backgroundImage backgroundOrigin backgroundPosition backgroundRepeat backgroundSize backfaceVisibility border borderBottom borderBottomColor borderBottomLeftRadius borderBottomRightRadius borderBottomStyle borderBottomWidth borderCollapse borderColor borderImage borderImageOutset borderImageRepeat borderImageSlice borderImageSource borderImageWidth borderLeft borderLeftColor borderLeftStyle borderLeftWidth borderRadius borderRight borderRightColor borderRightStyle borderRightWidth borderSpacing borderStyle borderTop borderTopColor borderTopLeftRadius borderTopRightRadius borderTopStyle borderTopWidth borderWidth bottom boxShadow boxSizing captionSide caretColor clear clip color columnCount columnFill columnGap columnRule columnRuleColor columnRuleStyle columnRuleWidth columns columnSpan columnWidth counterIncrement counterReset cssFloat cursor direction display emptyCells filter flex flexBasis flexDirection flexFlow flexGrow flexShrink flexWrap font fontFamily fontSize fontStyle fontVariant fontWeight fontSizeAdjust height isolation justifyContent left letterSpacing lineHeight listStyle listStyleImage listStylePosition listStyleType margin marginBottom marginLeft marginRight marginTop maxHeight maxWidth minHeight minWidth objectFit objectPosition opacity order orphans outline outlineColor outlineOffset outlineStyle outlineWidth overflow overflowX overflowY padding paddingBottom paddingLeft paddingRight paddingTop pageBreakAfter pageBreakBefore pageBreakInside perspective perspectiveOrigin position quotes resize right scrollBehavior tableLayout tabSize textAlign textAlignLast textDecoration textDecorationColor textDecorationLine textDecorationStyle textIndent textOverflow textShadow textTransform top transform transformOrigin transformStyle transition transitionProperty transitionDuration transitionTimingFunction transitionDelay unicodeBidi userSelect verticalAlign visibility width wordBreak wordSpacing wordWrap widows zIndex

HTML Events

HTML Events HTML Event Objects HTML Event Properties HTML Event Methods

Web APIs

API Canvas API Console API Fetch API Fullscreen API Geolocation API History API MediaQueryList API Storage API Validation API Web

HTML 对象

<a> <abbr> <address> <area> <article> <aside> <audio> <b> <base> <bdo> <blockquote> <body> <br> <button> <canvas> <caption> <cite> <code> <col> <colgroup> <datalist> <dd> <del> <details> <dfn> <dialog> <div> <dl> <dt> <em> <embed> <fieldset> <figcaption> <figure> <footer> <form> <head> <header> <h1> - <h6> <hr> <html> <i> <iframe> <img> <ins> <input> button <input> checkbox <input> color <input> date <input> datetime <input> datetime-local <input> email <input> file <input> hidden <input> image <input> month <input> number <input> password <input> radio <input> range <input> reset <input> search <input> submit <input> text <input> time <input> url <input> week <kbd> <label> <legend> <li> <link> <map> <mark> <menu> <menuitem> <meta> <meter> <nav> <object> <ol> <optgroup> <option> <output> <p> <param> <pre> <progress> <q> <s> <samp> <script> <section> <select> <small> <source> <span> <strong> <sub> <summary> <sup> <table> <tbody> <td> <tfoot> <th> <thead> <tr> <textarea> <time> <title> <track> <u> <ul> <var> <video>

Canvas API


HTML <canvas> 元素是 HTML 页面中的一个位图区域。

Canvas API 允许 JavaScript 在画布上绘制图形。

Canvas API 可以绘制形状、线条、曲线、方框、文本和图像,并支持颜色、旋转、透明度和其他像素操作。

示例

<canvas id="myCanvas" width="300" height="150"></canvas>
亲自试一试 »

您可以使用HTML DOM方法getElementById()访问><canvas>元素。

要在画布上绘制图形,您需要创建一个2D context对象。

const myCanvas = document.getElementById("myCanvas");
const ctx = myCanvas.getContext("2d");

注意

HTML <canvas> 元素本身不具备绘图功能。

您必须使用 JavaScript 来绘制图形。

getContext() method returns an object with tools (methods) for drawing.


路径

在画布上绘制的常用方法是:

  1. 开始绘制路径 - beginPath()
  2. 移动到指定点 - moveTo()
  3. 沿路径绘制 - lineTo()
  4. 绘制路径 - stroke()

示例

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();
亲自试一试 »

Canvas API 完整参考

本参考涵盖了 `getContext("2d")` 对象的所有属性和方法,该对象用于在画布上绘制文本、线条、方框、圆形、图片等。

绘图方法

只有 3 种方法可以直接在画布上绘制:

方法 描述
fillRect() 绘制一个填充矩形
strokeRect() 绘制一个无填充矩形
clearRect() 清除矩形内指定的像素

路径方法

方法 描述
beginPath() 开始新路径或重置当前路径
closePath() 从当前点到起点向路径添加一条线
isPointInPath() 如果指定点在当前路径中,则返回 true
moveTo() 将路径移动到画布中的指定点(不绘制)
lineTo() 在路径中添加一条线
fill() 填充当前路径
rect() 在路径中添加一个矩形
stroke() 绘制当前路径
圆和曲线
bezierCurveTo() 向路径添加三次贝塞尔曲线
arc() 向路径添加弧线/曲线(圆或圆的一部分)
arcTo() 在路径的两条切线之间添加弧线/曲线
quadraticCurveTo() 向路径添加二次贝塞尔曲线

文本

方法/属性 描述
direction 设置或返回用于绘制文本的方向
fillText() 在画布上绘制"填充"文本
font 设置或返回文本内容的字体属性
measureText() 返回一个包含指定文本宽度的对象
strokeText() 在画布上绘制文本
textAlign 设置或返回文本内容的对齐方式
textBaseline 设置或返回绘制文本时使用的文本基线

颜色、样式和阴影

方法/属性 描述
addColorStop() 指定渐变对象中的颜色和停止位置
createLinearGradient() 创建线性渐变(用于画布内容)
createPattern() 沿指定方向重复指定元素
createRadialGradient() 创建径向/圆形渐变(用于画布内容)
fillStyle 设置或返回用于填充绘图的颜色、渐变或图案
lineCap 设置或返回线条末端的样式
lineJoin 设置或返回两条线相交时创建的拐角类型
lineWidth 设置或返回当前线条宽度
miterLimit 设置或返回最大斜接长度
shadowBlur 设置或返回阴影的模糊级别
shadowColor 设置或返回阴影使用的颜色
shadowOffsetX 设置或返回阴影与形状之间的水平距离
shadowOffsetY 设置或返回阴影与形状之间的垂直距离
strokeStyle 设置或返回描边的颜色、渐变或图案

变换

方法 描述
scale() 放大或缩小当前图形
rotate() 旋转当前图形
translate() 重新映射画布上的 (0,0) 位置
transform() 替换当前绘图的变换矩阵
setTransform() 将当前变换矩阵重置为单位矩阵。然后运行 ​​transform()

图像绘制

方法 描述
drawImage() 在画布上绘制图像、画布或视频

ImageData 对象/像素操作

方法/属性 描述
createImageData() 创建一个新的空白 ImageData 对象
getImageData() 返回一个 ImageData 对象,该对象复制画布上指定矩形的像素数据
ImageData.data 返回一个包含指定 ImageData 对象图像数据的对象
ImageData.height 返回 ImageData 对象的高度
ImageData.width 返回 ImageData 对象的宽度
putImageData() 将图像数据(来自指定的 ImageData 对象)放回画布上

合成

属性 描述
globalAlpha 设置或返回绘图的当前透明度值
globalCompositeOperation 设置或返回新图像在现有图像上的绘制方式

其他方法

方法 描述
clip() 从原始画布中裁剪任意形状和大小的区域
save() 保存当前绘图上下文及其所有属性的状态
restore() 恢复先前保存的状态和属性
createEvent()
getContext()
toDataURL()

标准属性和事件

Canvas 对象还支持标准的属性事件


相关页面

Canvas 教程:Canvas 教程

HTML 教程:HTML5 Canvas

HTML 参考:HTML<canvas> 标签


浏览器支持

<canvas> 元素是 HTML5 标准(2014 年)。

Canvas API 在所有现代浏览器中均受支持。

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes 9-11

Copyright ©2020-2026 freew3c.com All Rights Reserved 提供的内容仅用于学习和测试,不保证内容的正确性。