uni-app使用canvas将两张图片合成一张图片

1、HTML部分

<view class="poster" id="canvas">
	<img id="avatar" />
</view>

2、js部分

import {
		qrcanvas
	} from "qrcanvas"
const drawImg = async () => {
		let shareUrl = 'https://xxxx.com'
		nextTick(async () => {
			let qrc = qrcanvas({
				data: shareUrl,
				size: 180,
			});
			let imgsrc = qrc.toDataURL("image/png");
			qrcodeImg.value = imgsrc;  // 二维码

			var canvas = document.createElement("canvas");
			canvas.width = 640 * window.devicePixelRatio;
			canvas.height = 990 * window.devicePixelRatio;
			var context = canvas.getContext("2d");

			// 禁用图像平滑,避免模糊
			context.imageSmoothingEnabled = false;

			// 背景透明处理
			context.fillStyle = "transparent";
			context.fillRect(0, 0, canvas.width, canvas.height);

			var myImage = new Image();
			myImage.crossOrigin = 'Anonymous';
			myImage.src = sharePic;  // 在线图片

			let imgW = 640 * window.devicePixelRatio;
			let imgH = 990 * window.devicePixelRatio;

			let img2X = 50 * window.devicePixelRatio;
			let img2Y = 760 * window.devicePixelRatio;
			let imgWH = 180 * window.devicePixelRatio;


			myImage.onload = function () {
				context.drawImage(myImage, 0, 0, imgW, imgH);

				context.font = `${32 * window.devicePixelRatio}px Arial bold`;
				context.fillStyle = 'black';
				context.fillText(userName.value, 260 * window.devicePixelRatio, 790 * window.devicePixelRatio);

				var myImage2 = new Image();
				myImage2.src = qrcodeImg.value;
				myImage2.crossOrigin = 'Anonymous';

				myImage2.onload = function () {
					context.drawImage(myImage2, img2X, img2Y, imgWH, imgWH);

					// 生成最终的base64编码图片
					var base64 = canvas.toDataURL("image/png");
					var img = document.getElementById('avatar');
					img.setAttribute('src', base64);
				};
			};
		});
	};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值