canvas 图片带圆角

完整开源代码可点击查看

以uni-app框架为例:可以把画图封装成一个函数调用。

activityCanvas: function(arrImages, storeName, price, people, count, successFn) {
		let that = this;
		let rain = 2;
		const context = uni.createCanvasContext('activityCanvas');
		context.clearRect(0, 0, 0, 0);
		/**
		 * 只能获取合法域名下的图片信息,本地调试无法获取
		 * 
		 */
		context.fillStyle = '#fff';
		context.fillRect(0, 0, 594, 850);
		uni.getImageInfo({
			src: arrImages[0],
			success: function(res) {
				context.drawImage(arrImages[0], 0, 0, 594, 850);
				// 图片加圆角
				that.handleBorderRect(context, 27*rain, 94*rain, 75*rain, 75*rain, 6*rain);
				context.clip();
				context.drawImage(arrImages[1], 27*rain, 94*rain, 75*rain, 75*rain);
				
			   ... // 这里可绘制自己项目的内容
			   
				context.draw(true, function() {
					uni.canvasToTempFilePath({
						canvasId: 'activityCanvas',
						fileType: 'png',
						destWidth: 594,
						destHeight: 850,
						success: function(res) {
							// uni.hideLoading();
							successFn && successFn(res.tempFilePath);
						}
					})
				});

			},
			fail: function(err) {
				console.log('失败', err)
				uni.hideLoading();
				that.Tips({
					title: '无法获取图片信息'
				});
			}
		})
	},

完整开源代码可点击查看

/**
	 * 图片圆角设置
	 * @param string x x轴位置
	 * @param string y y轴位置
	 * @param string w 图片宽
	 * @param string y 图片高
	 * @param string r 圆角值
	 */
	handleBorderRect(ctx, x, y, w, h, r) {
		ctx.beginPath();
		// 左上角
		ctx.arc(x + r, y + r, r, Math.PI, 1.5 * Math.PI);
		ctx.moveTo(x + r, y);
		ctx.lineTo(x + w - r, y);
		ctx.lineTo(x + w, y + r);
		// 右上角
		ctx.arc(x + w - r, y + r, r, 1.5 * Math.PI, 2 * Math.PI);
		ctx.lineTo(x + w, y + h - r);
		ctx.lineTo(x + w - r, y + h);
		// 右下角
		ctx.arc(x + w - r, y + h - r, r, 0, 0.5 * Math.PI);
		ctx.lineTo(x + r, y + h);
		ctx.lineTo(x, y + h - r);
		// 左下角
		ctx.arc(x + r, y + h - r, r, 0.5 * Math.PI, Math.PI);
		ctx.lineTo(x, y + r);
		ctx.lineTo(x + r, y);

		ctx.fill();
		ctx.closePath();
	},

完整开源代码可点击查看

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过使用 `canvas` 的 `drawImage()` 方法和 `arc()` 方法来绘制一个圆角图片。首先,需要将图片加载到 `canvas` 上,然后使用 `arc()` 方法绘制圆角。具体步骤如下: 1. 创建一个 `canvas` 元素和一个 `image` 元素,并将图片加载到 `image` 元素中。 ```html <canvas id="myCanvas"></canvas> <img id="myImage" src="path/to/image"> ``` 2. 在 JavaScript 中获取 `canvas` 和 `image` 元素,并将图片绘制到 `canvas` 上。 ```javascript const canvas = document.getElementById("myCanvas"); const ctx = canvas.getContext("2d"); const img = document.getElementById("myImage"); ctx.drawImage(img, 0, 0); ``` 3. 使用 `arc()` 方法绘制圆角。该方法需要指定圆心坐标、半径、起始角度和结束角度。 ```javascript const radius = 20; const x = 0; const y = 0; const width = canvas.width; const height = canvas.height; ctx.beginPath(); ctx.moveTo(x + radius, y); ctx.lineTo(x + width - radius, y); ctx.arcTo(x + width, y, x + width, y + radius, radius); ctx.lineTo(x + width, y + height - radius); ctx.arcTo(x + width, y + height, x + width - radius, y + height, radius); ctx.lineTo(x + radius, y + height); ctx.arcTo(x, y + height, x, y + height - radius, radius); ctx.lineTo(x, y + radius); ctx.arcTo(x, y, x + radius, y, radius); ctx.closePath(); ctx.clip(); ``` 4. 最后,将绘制好的圆角图片保存到变量中或者直接在 `canvas` 上显示。 ```javascript const roundedImg = canvas.toDataURL(); ``` 完整代码示例: ```html <canvas id="myCanvas"></canvas> <img id="myImage" src="path/to/image"> <script> const canvas = document.getElementById("myCanvas"); const ctx = canvas.getContext("2d"); const img = document.getElementById("myImage"); ctx.drawImage(img, 0, 0); const radius = 20; const x = 0; const y = 0; const width = canvas.width; const height = canvas.height; ctx.beginPath(); ctx.moveTo(x + radius, y); ctx.lineTo(x + width - radius, y); ctx.arcTo(x + width, y, x + width, y + radius, radius); ctx.lineTo(x + width, y + height - radius); ctx.arcTo(x + width, y + height, x + width - radius, y + height, radius); ctx.lineTo(x + radius, y + height); ctx.arcTo(x, y + height, x, y + height - radius, radius); ctx.lineTo(x, y + radius); ctx.arcTo(x, y, x + radius, y, radius); ctx.closePath(); ctx.clip(); const roundedImg = canvas.toDataURL(); </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值