微信小程序内开发一个可以画图,保存的白板功能
  1. 创建一个CanvasContext对象,以获取绘图上下文。
  2. 实现绘画事件的处理,包括开始绘画、移动和结束绘画。在事件处理函数中,需要配置动作坐标,以便在画布上绘制图形。
  3. 创建一个清除画布的函数,用于重置坐标的高和宽,以便清除画布上的内容。
  4. 实现保存画布内容的功能。在画布上绘制完成后,调用savePic函数将画布内容保存为图片。可以使用wx.canvasToTempFilePath方法将canvas内容保存为临时路径,然后再保存到相册中。
// 初始化画布const ctx = wx.createCanvasContext('canvas')

// 开始绘画事件处理函数function canvasStart(event) {
	ctx.beginPath()
	ctx.moveTo(event.changedTouches[0].x, event.changedTouches[0].y)
}

// 移动绘画事件处理函数function canvasMove(event) {
	ctx.lineTo(event.changedTouches[0].x, event.changedTouches[0].y)
	ctx.stroke()
}

// 结束绘画事件处理函数function canvasEnd(event) {
	ctx.draw()
}

// 清除画布函数function clearCanvas() {
	ctx.clearRect(0, 0, canvasWidth, canvasHeight)
}

// 保存画布内容函数function savePic() {
	wx.canvasToTempFilePath({
	canvasId: 'canvas',
	success: function (res) {
		const tempFilePath = res.tempFilePath
		wx.saveImageToPhotosAlbum({
		filePath: tempFilePath,
		success: function (res) {
		wx.showToast({ title: '保存成功', icon: 'success' })
	},
	fail: function (res) { console.log(res) }
	})
	},
	fail: function (res) { console.log(res) }
	}, 500)
}
  • 当前日期:
  • 北京时间:
  • 时间戳:
  • 今年的第:18周
  • 我的 IP:3.136.26.17
农历
五行
冲煞
彭祖
方位
吉神
凶神
极简任务管理 help
+ 0 0 0
Task Idea Collect