2.1.5 • Published 4 years ago

@huteming/ui-canvas-draw v2.1.5

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

封装 canvas 方法,主要是解决了 canvas 在不同分辨率中显示可能模糊的问题。并封装了一些原生方法,方便调用导出图片

引入

import { CanvasDraw } from '@huteming/ui'

代码演示

基础用法

const instance = new CanvasDraw(750, 1109)

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

const src = instance.done('png')

高斯模糊

const instance = new CanvasDraw(750, 1109)

instance.add(({ context, canvas, ratio, canvasWidth, canvasHeight }) => {
    const x = 52
    const y = 424
    const width = 552
    const height = 177
    const r = '20 20 -20 -20'

    const blurryCanvas = instance.getBlurryArea(100, x, y, width, height)
    // 创建不规则区域然后将模糊图像渲染
    instance.drawRect(x, y, width, height, { r })
    context.clip()
    instance.drawImage(blurryCanvas, x, y, width, height)
    instance.drawRect(x, y, width, height, { r, color: 'rgba(255, 255, 255, 0.62)' })
})

const src = instance.done('png')

删除线

const instance = new CanvasDraw(750, 1109)

instance.add(() => {
    instance.drawText('<through>删除线</through>', x, y, optionsText)
})

const src = instance.done('png')

下划线

const instance = new CanvasDraw(750, 1109)

instance.add(() => {
    instance.drawText('<underline>下划线</underline>', x, y, optionsText)
})

const src = instance.done('png')

API

构造函数参数

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
getBlurryArea创建高斯模糊区域radius, x, y, width, height

optionsDone

参数描述可选值默认值
type导出图片类型jpg | pngpng

optionsCanvas

name描述默认值
designWidth设计稿标准总宽度, 单位: px750

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

参数描述默认值
fix过长省略时添加字符串'.... '
maxWidth最长宽度,会在末尾加上 fix 字符串,一般搭配前缀 后缀使用Infinity
style字体的风格 normal, italic, obliquenormal
variant字体变体 normal, small-capsnormal
weight字体的粗细 bold bolder lighter 100 200 300 400 500 600 700 800 900normal
size字号24
lineHeight行高, 若不存在,则在运行时会重置为 size * 1.50
align对齐方式 start, end, center, left, rightstart
baseline文本基线, alphabetic, top, hanging, middle, ideographic, bottomtop
letterSpacing字体间距0
lineWidth画笔宽度1
wrap换行次数,不包括第一行。true => Infinity; false => 00
shadowColor阴影颜色-
shadowOffsetX阴影0
shadowOffsetY阴影0
shadowBlur阴影0
color画笔颜色#000
type画笔类型, fillText, strokeTextfillText
underline下划线参数{ left: 10, right: 10, bottom: 6, dashed: [], lineWidth: 1 }

optionsLine

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