Skip to content

xtf-canvas

组件说明

xtf-canvas 是画布组件,封装了 uni-app 的 canvas 能力,提供绘图、手写签名、撤销/重做、导出图片等功能。支持画笔/橡皮擦切换、笔画历史管理、网格显示和命令式绘图,适用于手写签名、涂鸦画板和自定义图表等场景。


基础用法

1. 最简示例

设置画布尺寸,开启绘图模式即可手写:

vue
<template>
  <xtf-canvas
    ref="canvas"
    width="600"
    height="400"
    drawing
    background-color="#ffffff"
    @ready="onReady"
  />
</template>

<script>
export default {
  methods: {
    onReady(meta) {
      console.log('画布就绪', meta.canvasId)
    }
  }
}
</script>

2. 画笔与橡皮擦

通过 tool 切换画笔/橡皮擦,通过 strokeColorstrokeWidth 控制笔画样式:

vue
<template>
  <view>
    <xtf-button-group :gap="12" style="margin-bottom: 16rpx">
      <xtf-button
        label="画笔"
        size="sm"
        :type="tool === 'pen' ? 'solid' : 'light'"
        @click="tool = 'pen'"
      />
      <xtf-button
        label="橡皮擦"
        size="sm"
        :type="tool === 'eraser' ? 'solid' : 'light'"
        @click="tool = 'eraser'"
      />
    </xtf-button-group>
    <xtf-canvas
      ref="canvas"
      width="600"
      height="400"
      drawing
      :tool="tool"
      stroke-color="#333333"
      :stroke-width="4"
      background-color="#ffffff"
    />
  </view>
</template>

<script>
export default {
  data() {
    return {
      tool: 'pen'
    }
  }
}
</script>

3. 撤销与重做

通过 undo / redo 方法操作历史记录:

vue
<template>
  <view>
    <xtf-button-group :gap="12" style="margin-bottom: 16rpx">
      <xtf-button label="撤销" size="sm" type="light" @click="undo" />
      <xtf-button label="重做" size="sm" type="light" @click="redo" />
      <xtf-button label="清空" size="sm" type="light" theme="danger" @click="clearCanvas" />
      <xtf-button label="导出" size="sm" type="solid" @click="exportImage" />
    </xtf-button-group>
    <xtf-canvas
      ref="canvas"
      width="600"
      height="400"
      drawing
      background-color="#ffffff"
      :history-limit="30"
    />
  </view>
</template>

<script>
export default {
  methods: {
    undo() {
      this.$refs.canvas.undo()
    },
    redo() {
      this.$refs.canvas.redo()
    },
    clearCanvas() {
      this.$refs.canvas.clear()
    },
    exportImage() {
      this.$refs.canvas.exportImage().then((res) => {
        console.log('导出图片路径:', res.tempFilePath)
      })
    }
  }
}
</script>

4. 网格与空状态

通过 showGrid 显示网格辅助线,通过 emptyText 设置空状态提示:

vue
<template>
  <xtf-canvas
    ref="canvas"
    width="600"
    height="400"
    drawing
    show-grid
    grid-size="40"
    grid-color="rgba(148, 163, 184, 0.18)"
    background-color="#ffffff"
    empty-text="在画布上绘制内容"
  />
</template>

全部属性

属性类型默认值作用描述适用范围
canvasIdString''画布 ID,不设则自动生成自定义 ID
widthString | Number600画布宽度(rpx)控制宽度
heightString | Number400画布高度(rpx)控制高度
backgroundColorString''背景色背景色
disableScrollBooleantrue是否禁用画布区域滚动防止滚动
autoCreateContextBooleantrue是否自动创建绘图上下文自动初始化
drawingBooleanfalse是否开启绘图模式手写绘图
toolString'pen'工具:'pen' / 'eraser'切换工具
strokeColorString'#111827'画笔颜色画笔颜色
strokeWidthString | Number6画笔宽度(rpx)画笔粗细
minDistanceString | Number2最小采样距离(rpx)采样精度
pressureBooleanfalse是否启用压感压感支持
historyLimitString | Number50历史记录上限历史限制
strokesArray[]外部笔画数据导入笔画
commandsArray[]命令式绘图指令命令绘图
showGridBooleanfalse是否显示网格网格辅助
gridSizeString | Number40网格大小(rpx)网格间距
gridColorString'rgba(148, 163, 184, 0.18)'网格颜色网格颜色
emptyTextString''空状态提示文字空状态
customClassString''自定义类名样式覆盖
customStyleString | Object''自定义样式动态样式覆盖

事件

事件名称触发时机回调参数参数说明
ready画布上下文初始化完成(meta){ canvasId, context, width, height, strokeCount, canUndo, canRedo }
touchstart / touchmove / touchend / touchcancel原生触摸阶段触发(event)原生触摸事件;组件继续执行内部绘制逻辑
mousedown / mousemove / mouseup / mouseleaveH5 鼠标阶段触发(event)原生鼠标事件;用于桌面端交互监听
draw-start开始绘制一笔时触发(payload)含起始 point 与画布元数据
draw-move绘制过程追加采样点时触发(payload)含当前 point 与画布元数据
draw-end一笔绘制完成时触发(payload)含完成的 stroke 与画布元数据
change笔画或命令变更(payload)reasonstrokescommands 和画布元数据
update:strokes内部笔画数据变更时触发(strokes: Array)可用于同步外部 strokes 数据
update:commands内部命令数据变更时触发(commands: Array)可用于同步外部 commands 数据
undo撤销时触发(strokes)撤销后的笔画
redo重做时触发(strokes)重做后的笔画
clear清空时触发--
export导出时触发(res)导出结果
error出错时触发(error)错误对象

方法

方法名参数返回值说明
getContext-Object获取画布上下文
getCanvasId-String获取画布 ID
undo--撤销上一笔
redo--重做上一笔
clear--清空画布
exportImage(options)Promise导出临时图片;成功时触发 export
getStrokes / setStrokes / clearStrokes数据 / -数据 / -读取、替换、清空笔画
getCommands / setCommands / addCommand / removeCommand / clearCommands命令或 ID数据 / ID / Boolean管理命令绘图
drawRectdrawCircledrawEllipsedrawRoundRectdrawArcdrawLinedrawPointsdrawPolygondrawPathdrawTextdrawIcondrawImage图形参数String添加对应命令并返回命令 ID
redraw(callback)-重绘画布

主题说明

  • 画布本身不使用主题变量,颜色由 backgroundColorstrokeColorgridColor 等属性控制
  • 画笔颜色和宽度可通过属性自定义
  • 网格颜色默认为半透明灰色

如需全局调整主题,请在 xtf-config-provider 或主题配置中覆盖相关 CSS 变量。

MIT Licensed