uni-app canvas创建画布

canvasTmp: function(arr2, store_name, successFn, errFun) {
		let that = this;
		const ctx = uni.createCanvasContext('myCanvas');
		ctx.clearRect(0, 0, 0, 0);
		/**
		 * 只能获取合法域名下的图片信息,本地调试无法获取
		 * 
		 */
		uni.getImageInfo({			
			src: arr2[0],
			success: function(res) {
				const WIDTH = res.width*0.71;
				const HEIGHT = res.height*1.04;
				// const WIDTH = 320;
				//  const HEIGHT = 450;
				let r = 70;
				let d = r * 2;
				let cx = 85;
				let cy = 110;
				ctx.drawImage(arr2[1], 0, 0, WIDTH, HEIGHT); // 小图
				ctx.save();
				ctx.arc(cx + r, cy + r, r, 0, 2 * Math.PI);
				ctx.drawImage(arr2[0], cx, cy, d, d); // 背景
				ctx.restore();
				const CONTENT_ROW_LENGTH = 40;
				let [contentLeng, contentArray, contentRows] = that.textByteLength(store_name, CONTENT_ROW_LENGTH);
				if (contentRows > 2) {
					contentRows = 2;
					let textArray = contentArray.slice(0, 2);
					textArray[textArray.length - 1] += '......';
					contentArray = textArray;
				}
				ctx.setTextAlign('center');
				ctx.setFontSize(26); // 文字
				ctx.setFillStyle('#000000');
				let contentHh = 26 * 1.3;
				for (let m = 0; m < contentArray.length; m++) {
					ctx.fillText(contentArray[m], 156, 430 + contentHh * m);
				}
				ctx.setTextAlign('left')
				ctx.setFontSize(28);
				ctx.setFillStyle('#FFFFFF');
				ctx.draw(true, function() {
					uni.canvasToTempFilePath({
						canvasId: 'myCanvas',
						fileType: 'png',
						destWidth: WIDTH,
						destHeight: HEIGHT,
						success: function(res) {
							uni.hideLoading();
							successFn && successFn(res.tempFilePath);
						},
						fail: function(err) {
							uni.hideLoading();
							errFun && errFun(err);
						}
					})
				});
			},
			fail: function(err) {
				uni.hideLoading();
				that.Tips({
					title: '无法获取图片信息'
				});
				errFun && errFun(err);
			}
		})
	},

调用 :

async spreadMsg(image ,name, title) {
				let that = this
				/** #ifndef H5   type: 'h5',   #endif  #ifdef MP || APP || APP-PLUS  #endif*/
				let rqData = {
					type: 'routine'
				}
				that.spreadData = [that.bgurl[that.type]]
				that.nickName = name
				that.siteName = title
			
				let codeUrl = ""
				// #ifdef MP || APP-PLUS
				let mpUrl = await that.downloadFilestoreImage(image)
				// #endif
				uni.showLoading({
					title: '海报生成中',
					mask: true
				});
				 // 本来这个是个循环 也就是出现多个海报 去除了
				let arr2
				// #ifdef MP || APP-PLUS	
				arr2 = [mpUrl, await that.downloadFilestoreImage(that.bgurl[that.type])]
				// #endif
				// #ifdef H5
				let img = await that.imgToBase(that.bgurl[that.type])
				arr2 = [await that.imgToBase(image), img]			
				// #endif
				that.$util.canvasTmp(arr2, name, (tempFilePath) => {
					that.$set(that.posterImage, 0, tempFilePath);
				}, (err) => {
					that.$set(that, 'canvasStatus', false);
				});
				uni.hideLoading();
			},

that.$util.PosterCanvasMer(arr2, name, (tempFilePath) => {

that.$set(that.posterImage, 0, tempFilePath);

}, (err) => {

that.$set(that, 'canvasStatus', false);

});

相关推荐
玩电脑的辣条哥2 小时前
Python如何播放本地音乐并在web页面播放
开发语言·前端·python
ew452182 小时前
ElementUI表格表头自定义添加checkbox,点击选中样式不生效
前端·javascript·elementui
suibian52352 小时前
AI时代:前端开发的职业发展路径拓宽
前端·人工智能
画月的亮2 小时前
element-ui 使用过程中遇到的一些问题及解决方法
javascript·vue.js·ui
Moon.92 小时前
el-table的hasChildren不生效?子级没数据还显示箭头号?树形数据无法展开和收缩
前端·vue.js·html
m0_526119402 小时前
点击el-dialog弹框跳到其他页面浏览器的滚动条消失了多了 el-popup-parent--hidden
javascript·vue.js·elementui
垚垚 Securify 前沿站2 小时前
深入了解 AppScan 工具的使用:筑牢 Web 应用安全防线
运维·前端·网络·安全·web安全·系统安全
工业甲酰苯胺5 小时前
Vue3 基础概念与环境搭建
前端·javascript·vue.js
lyj1689975 小时前
el-tree选中数据重组成树
javascript·vue.js·elementui
mosquito_lover16 小时前
怎么把pyqt界面做的像web一样漂亮
前端·python·pyqt