uniapp 使用canvas画布,实现文字换行

1、新建一个工具类文件夹utils(已有忽略)

2、新建文件common.js

3、在common.js,新增内容:

/**
 * canvas文本超出范围时,自动换行
 * @param {*} ctx
 * @param {*} text
 * @param {*} x
 * @param {*} y
 * @param {*} maxWidth
 * @param {*} lineHeight
 */
export function canvasTextAutoLine(ctx, text, x, y, maxWidth, lineHeight) {
  let lineWidth = 0
  let lastSubStrIndex = 0
  for (let i = 0; i < text.length; i++) {
    lineWidth += ctx.measureText(text[i]).width
    if (lineWidth > maxWidth) {
      ctx.fillText(text.substring(lastSubStrIndex, i), x, y)
      y += lineHeight
      lineWidth = 0
      lastSubStrIndex = i
    }
    if (i === text.length - 1) {
      ctx.fillText(text.substring(lastSubStrIndex, i + 1), x, y)
    }
  }
}

4、在视图/组件内使用

<script>
import { canvasTextAutoLine } from '@/utils/common'
export default {
	methods:{
		handleInitCanvas(){
			const ctx = uni.createCanvasContext('canvas', this) // 创建画布
			...
			canvasTextAutoLine(
            ctx,
            '测试换行文字文字文字文字',
            x, // x轴坐标位置(视实际情况而定)
            y, // y轴坐标位置(视实际情况而定)
            500, // 文字显示最大宽度(视实际情况而定)
            30 // 文字行高(视实际情况而定)
          )
			...
		}
	}
}
</script>
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的示例代码,实现使用uniappcanvas组件实现手写签名效果,并提供了保存和清空的功能。 template: ```html <template> <view> <canvas canvas-id="myCanvas" style="width:100%;height:400rpx;background-color: #fff;" bindtouchstart="touchStart" bindtouchmove="touchMove" bindtouchend="touchEnd"></canvas> <view class="btn-group"> <button type="primary" class="btn" @tap="save">保存</button> <button type="default" class="btn" @tap="clear">清空</button> </view> </view> </template> ``` script: ```javascript <script> export default { data() { return { ctx: null, // canvas绘图上下文 lastX: 0, // 上一次触摸的X坐标 lastY: 0, // 上一次触摸的Y坐标 isDrawing: false // 是否正在绘制 } }, mounted() { // 获取canvas绘图上下文 this.ctx = uni.createCanvasContext('myCanvas', this); }, methods: { touchStart(e) { // 记录起始点坐标 this.lastX = e.changedTouches[0].x; this.lastY = e.changedTouches[0].y; // 开始绘制 this.isDrawing = true; }, touchMove(e) { if (this.isDrawing) { // 获取当前坐标 const currentX = e.changedTouches[0].x; const currentY = e.changedTouches[0].y; // 绘制线条 this.ctx.beginPath(); this.ctx.moveTo(this.lastX, this.lastY); this.ctx.lineTo(currentX, currentY); this.ctx.stroke(); // 更新上一次坐标 this.lastX = currentX; this.lastY = currentY; } }, touchEnd() { // 结束绘制 this.isDrawing = false; }, save() { // 保存签名 uni.canvasToTempFilePath({ canvasId: 'myCanvas', success: (res) => { uni.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: () => { uni.showToast({ title: '保存成功' }) } }) } }, this); }, clear() { // 清空画布 this.ctx.clearRect(0, 0, uni.upx2px(750), uni.upx2px(400)); this.ctx.draw(); } } } </script> ``` 需要注意的是,canvas组件的宽度和高度应该使用rpx或者upx作为单位,并且在保存签名时,需要先使用canvasToTempFilePath方法将canvas转换成临时文件路径,再使用saveImageToPhotosAlbum方法保存到相册中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值