4.0.0 • Published 4 years ago

@huteming/util-canvas-draw v4.0.0

Weekly downloads
2
License
ISC
Repository
-
Last release
4 years ago

封装 canvas 方法,便于画图导出图片


例子

CanvasDraw 为构造函数。主要是解决了 canvas 在不同分辨率中显示可能模糊的问题。并封装了一些原生方法,方便调用

import { CanvasDraw } from '@huteming/util'
const instance = new CanvasDraw(375, 500)

instance.onerror(console.error)
instance.add(({ context, canvas, ratio, width, height }) => {
    instance.drawText('hello world', 24, 93)
})

const src = instance.done()

构造函数参数

name描述默认参数
width设计稿上画布宽度
height设计稿上画布高度
optionsCanvas配置

实例方法

name描述参数
add执行callback前后分别执行 save, restore 方法Function: callbackDraw({ context, canvas, ratio, width, height })
onerror异常处理.默认为 console.errorError
done导出图片Object: optionsDone
name描述参数
drawArc圆形x, y, r, optionsArc
drawRect矩形x, y, width, height, optionsRect
drawText文本text, x, y, optionsText
drawLine直线startX, startY, endX, endY, optionsLine
drawImage图片image, x, y, width, height

optionsDone

参数描述默认值
type导出图片类型png

optionsCanvas

name描述默认值
designWidth设计稿标准总宽度750

optionsArc

参数描述默认值
startDegrees开始角度0
endDegrees结束角度360
direction方向。false: 顺时针, true: 逆时针false
lineWidth画笔宽度1
color画笔颜色#000
type画笔类型, fill, strokefill

optionsRect

参数描述默认值
r圆角半径0
lineWidth画笔宽度1
color画笔颜色#000
type画笔类型, fill, strokefill

optionsText

参数描述默认值
prefix前缀
suffix后缀
fix过长省略时添加字符串'.... '
maxWidth最长宽度,会在末尾加上 fix 字符串,一般搭配前缀 后缀使用0
style字体的风格 normal, italic, obliquenormal
variant字体变体 normal, small-capsnormal
weight字体的粗细 bold bolder lighter 100 200 300 400 500 600 700 800 900normal
size字号12
lineHeight行高1
align对齐方式 start, end, center, left, rightstart
baseline文本基线, alphabetic, top, hanging, middle, ideographic, bottomtop
baseline文本基线, alphabetic, top, hanging, middle, ideographic, bottomtop
lineWidth画笔宽度1
color画笔颜色#000
type画笔类型, fill, strokefill

optionsLine

参数描述默认值
lineWidth画笔宽度1
color画笔颜色#000