代码拉取完成,页面将自动刷新
如果不给 canvas 设置 widht、height 属性时,则默认 width为300、height 为 150,单位都是 px。也可以使用 css 属性来设置宽高,但是如宽高属性和初始比例不一致,他会出现扭曲。所以,建议永远不要使用 css 属性来设置 canvas 的宽高。
<canvas id="myDraw" width="400" height="600"></canvas>
draw();
async function draw() {
//实例化CanvasDraw对象 myDraw为canvas标签的 的id属性
let canvasDraw = new CanvasDraw("myDraw");
// 加载字体
await canvasDraw.loadFace("./小篆拼音体.ttf", "MyCustomFont");
// 绘制矩形
canvasDraw.drawRect("#cccccc", 0, 0, 50, 50);
// 绘制矩形框
canvasDraw.drawRectBorder("#cccccc", 60, 0, 50, 50);
// 绘制文字
canvasDraw.drewText(
"这是一段文字描述",
24,
"#000000",
50,
50,
"MyCustomFont"
);
// 清空区域
canvasDraw.clearRect(10, 10, 20, 20);
// 绘制图片
canvasDraw.drewImage("./640.webp", 100, 100, 200, 400, 30);
// 绘制椭圆
canvasDraw.drawEllipseBorder("#cccccc", 50, 100, 20, 30, 30);
// 下载canvas(如果canvas里绘制的有图片,图片必须不能跨域,否则无法下载)
// canvasDraw.downloadCanvas()
}
方法名 | 描述 |
---|---|
loadFace(fontUrl, fontName) | 加载字体 |
resetDefault() | 重置样式 |
splitStringIntoChunks(str, chunkSize) | 将字符串按照指定长度截取成数组 |
chunkArray(array, chunkSize) | 将一维数组按照指定长度转为二维数组 |
drawVerticalLine(strokeStyle, x, y, height, lineWidth = 1) | 绘制竖线 |
drawHorizontalLine(strokeStyle, x, y, width, lineWidth = 1) | 绘制横线 |
drawLine(strokeStyle, start={x:0,y:0}, end={x:10,y:10}, lineWidth = 1) | 绘制线段 |
drawRect(fillStyle, x, y, width, height) | 绘制矩形 |
drawRectBorder(strokeStyle, x, y, width, height) | 绘制矩形框 |
drawRoundedRect(fillStyle, x, y, width, height, radius = 10) | 绘制圆角矩形 |
drawRoundedRectPath(x, y, width, height, radius = 10) | 创建圆角矩形路径 |
drawRoundedRectBorder(strokeStyle, x, y, width, height, radius = 10) | 圆角矩形框 |
clearRect(x, y, width, height) | 清除矩形区域 |
drawCirclePath(x, y, r) | 绘制一个圆形路径 |
drawCircle(fillStyle, x, y, r) | 绘制圆 |
drawCircleBorder(strokeStyle, x, y, r) | 绘制圆边框 |
drawEllipsePath(x, y, radiusX, radiusY, rotation) | 绘制椭圆路径 |
drawEllipse(fillStyle, x, y, radiusX, radiusY, rotation) | 绘制椭圆 |
drawEllipseBorder(strokeStyle, x, y, radiusX, radiusY, rotation) | 绘制椭圆边框 |
drewText(context, fontSize, color, x, y, family, textAlign = 'left') | 绘制文字 |
drewTextCenter(context, fontSize, color, x, y, family, textAlign = 'left') | 从坐标点往两侧绘制文字 |
drewTextLeft(context, fontSize, color, x, y, family) | 从左侧绘制文字 |
drewTextRight(context, fontSize, color, x, y, family) | 从右侧绘制文字 |
drewImage(src, x, y, width, height, radius) | 绘制圆角图片 表现形式为cover |
downloadCanvas(fileName, mimeType = "image/png", qualityArgument = 0.7) | 下载canvas |
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。