1 Star 0 Fork 0

放肆 / canvas 常用函数封装

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

canvas绘制

​如果不给 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()
    }
  • 方法列表(具体入参描述参考 canvas.js 的函数头注释 )
方法名 描述
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

空文件

简介

canvas 常用函数封装 例如设定宽高绘制类似 css 中 background-size:cover 熟悉,下载canvas绘制后的图片 展开 收起
JavaScript 等 2 种语言
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/DaidaiDeXiaoShuai/canvas_draw.git
git@gitee.com:DaidaiDeXiaoShuai/canvas_draw.git
DaidaiDeXiaoShuai
canvas_draw
canvas 常用函数封装
master

搜索帮助