微信小程序wx.canvasToTempFilePath,draw()报错 ctx.draw is not a function

这篇具有很好参考价值的文章主要介绍了微信小程序wx.canvasToTempFilePath,draw()报错 ctx.draw is not a function。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

微信小程序canvas转图片临时路径,使用wx.canvasToTempFilePath方法,官方文档中写了要在 draw() 回调里调用该方法才能保证图片导出成功。
微信小程序wx.canvasToTempFilePath,draw()报错 ctx.draw is not a function

然而,显示是写在draw()里面会报错draw is not a function,查阅了一下资料,新版 Canvas 2D 接口与 Web 一致,是没有 draw 方法的。https://developers.weixin.qq.com/miniprogram/dev/api/canvas/CanvasContext.html

所以调wx.canvasToTempFilePath时不用写在draw里面,wx.canvasToTempFilePath的canvas参数的值不是canvas id了,而是canvas实例
微信小程序wx.canvasToTempFilePath,draw()报错 ctx.draw is not a function

参考代码:

rotatePic() {
	let that = this
	//获取canvas
	const query = wx.createSelectorQuery()
	query.select('#myCanvas')
	.fields({
		node: true,
		size: true
	})
	.exec(function(res) {
		const canvas = res[0].node
		canvas.width = that.canvasWidth
		canvas.height = that.canvasHeight
		const ctx = canvas.getContext('2d')
		const bg = canvas.createImage()
		bg.src = that.imgUrl
		bg.onload = () => {
			//先保存一下设置
			ctx.save();
			//将画布向右下移动一半宽
			ctx.translate(canvas.width / 2, canvas.height / 2);
			//再旋转角度:逆时针旋转180度
			ctx.rotate(-180 / 180 * Math.PI);
			//最后将画布移回来,摆正之前的位置
			ctx.translate(-canvas.width / 2, -canvas.height / 2);
			//最后画出来
			ctx.drawImage(bg, 0, 0);
			//不要忘记恢复之前的设置
			ctx.restore()
			//canvas转文件的临时路径 (本地路径)
			wx.canvasToTempFilePath({
				canvas,
				fileType: "jpg",
				quality: 1,
				success: (res) => {
					that.imgUrl = res.tempFilePath//这个就是要的路径了
				}
			})
		}
	})
}

参考文章:如何解决draw()报错 ctx.draw is not a function 的问题?文章来源地址https://www.toymoban.com/news/detail-511038.html

到了这里,关于微信小程序wx.canvasToTempFilePath,draw()报错 ctx.draw is not a function的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • 微信小程序报错qqmap-wx-jssdk.js‘ is not defined解决方法

    微信小程序报错qqmap-wx-jssdk.js‘ is not defined解决方法

     今天做了一个需求,需要接入腾讯地图,报错qqmap-wx-jssdk.js\\\' is not defined报了一下午,我苦苦找不到原因,太自信了,因为另外一个地方的腾讯地图引入很轻松,我也是按照官网给的方法写的,怎么都引入不进去,网上的方法都尝试了一遍都不行。刚刚解决了,先说报错原因

    2024年02月04日
    浏览(12)
  • 微信小程序报错“qqmap-wx-jssdk.js‘ is not defined”解决方法

    微信小程序报错“qqmap-wx-jssdk.js‘ is not defined”解决方法

    一、问题描述 在使用微信小程序开发过程中,有时会遇到“qqmap-wx-jssdk.js‘ is not defined”的报错信息。这个错误通常与腾讯地图SDK相关,表明小程序试图引用一个未定义的脚本。 二、解决方案 解决此问题的方法有多种,以下是一些常见的解决方案: 检查引用顺序:确保在使

    2024年04月15日
    浏览(13)
  • uniapp 微信小程序canvasToTempFilePath保存的canvas是空白

    问题描述:绘制完canvas后,使用canvasToTempFilePath保存到手机的图片是空白 问题解决: 在绘制图片ctx.draw时,需要使用 箭头函数 调用wx.canvasToTempFilePath 保存图片

    2024年02月15日
    浏览(8)
  • 【微信小程序】canvasToTempFilePath:fail fail canvas is empty

    【微信小程序】canvasToTempFilePath:fail fail canvas is empty

    在微信小程序开发者工具会报错 canvasToTempFilePath:fail fail canvas is empty ,怎么解决呢 在微信小程序开发者工具会报错内容,如下图所示 最好是先看一下官方文档点此前往,上面写的,以前的是以前,现在的是有了变化 💡 可能是官网打算弃用 canvasId ,也就是说,只传这个的话

    2024年02月11日
    浏览(8)
  • 微信小程序wx.createInnerAudioContext播放play报错errMsg: “operateAudio:fail jsapi has no permission, event=ope

    微信小程序wx.createInnerAudioContext播放play报错解决 微信小程序wx.createInnerAudioContext播放play报错errMsg: “operateAudio:fail jsapi has no permission, event=operateAudio, runningState=background, permissionMsg=permission got, detail=jsapi has no permission” 解决方案: 在创建onError回调函数再次执行播放函数

    2024年02月08日
    浏览(25)
  • 微信小程序报错wx.getLocation need to be declared in the requiredPrivateInfos field in app.json

    微信小程序报错wx.getLocation need to be declared in the requiredPrivateInfos field in app.json

    wx.getLocation need to be declared in the requiredPrivateInfos field in app.json/ext.json(env: Windows,mp,1.06.2301160; lib: 2.30.3) 自 2022 年 7 月 14 日后发布的小程序,使用以下8个地理位置相关接口时,需要声明该字段,否则将无法正常使用。 文件app.json   全局配置 | 微信开放文档 微信开发者平台文档

    2024年02月05日
    浏览(5)
  • 微信小程序报错:WAServiceMainContext.js?t=wechat&s=1684052115130&v=2.31.1:1 wx.chooseAddress need to be

    获取用户收货地址。调起用户编辑收货地址原生界面,并在编辑完成后返回用户选择的地址。 自 2022 年 7 月 14 日后发布的小程序,若使用该接口,需要在 app.json 中进行声明,否则将无法正常使用该接口,2022年7月14日前发布的小程序不受影响。具体规则见公告 暂只针对具备

    2024年02月11日
    浏览(9)
  • ios系统(苹果手机)微信小程序canvas.draw不执行回调

    当canvas绘制图片完成之后,来执行draw的时候, 1.安卓手机不受影响,可以正常执行draw的回调函数 2.苹果手机在ios高版本系统上,则可能出现draw的回调函数不执行了。  比如以下代码: 之所以会出现这样的原因是:ios高版本系统 对于 canvas的元素要求是: canvas元素必须存在,如

    2024年02月03日
    浏览(16)
  • 微信小程序使用canvas报:canvasToTempFilePath:fail executeCanvasMethod failed: Failed to execute ‘drawImage‘

    微信小程序使用canvas报:canvasToTempFilePath:fail executeCanvasMethod failed: Failed to execute ‘drawImage‘

    目录 项目场景: 问题描述 原因分析: 解决方案: 尝试1: 尝试2: 尝试过程: 使用微信小程序的api进行canvas绘制海报或者二维码 技术:uniapp 版本:3.0.0(3.0.1也可以,低版本没有试过) 需求:点击后弹出个人二维码 完整报错:canvasToTempFilePath:fail executeCanvasMethod failed: Fai

    2024年02月08日
    浏览(18)
  • 微信小程序 wx.showModal

    微信小程序--wx.showModal_海轰Pro的博客-CSDN博客

    2024年02月15日
    浏览(11)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包