微信小程序canvas绘制圆角矩形

这篇博客介绍了如何使用HTML5的Canvas API来绘制一个不规则矩形,包括设置透明填充、边框颜色以及如何防止内容溢出矩形区域。通过arc方法创建圆角并连接路径,然后使用stroke方法绘制边框,clip方法确保内容不会超出矩形边界。此技术适用于前端图形界面的复杂布局和美化。
canvas.save()   // 保存之前的绘图
				
canvas.beginPath()   // 开始绘制
				
canvas.setFillStyle('transparent')   // 填充边缘

// arc对应参数含义
// canvas.arc('圆心x轴坐标', '圆心Y轴坐标', '圆的半径', '起始弧度', '终止弧度', '弧度是否逆时针方向:boolean')
//left - 矩形x轴位置  |    top - 矩形Y轴位置    |    width - 矩形宽度    |    height - 矩形高度

canvas.arc(left + width - radius, top + height - radius, radius, 0, Math.PI * 0.5)    // 右下角
				
canvas.lineTo(left + radius, top + height)    // 下边线
				
canvas.arc(left + radius, top + height - radius, radius, Math.PI * 0.5, Math.PI)    // 左下角
				
canvas.lineTo(left, top + radius)    // 左边线
				
canvas.arc(left + radius, top + radius, radius, Math.PI, Math.PI*1.5)    // 左上角
				
canvas.lineTo(left + width - radius, top)     // 上边线
				
canvas.arc(left + width - radius, top + radius, radius, Math.PI * 1.5, Math.PI * 2)    // 右上角
				
canvas.lineTo(left + width, top + height - radius)    // 右边线
				
canvas.closePath()    // 连接之前绘制的线段
				
canvas.setStrokeStyle('rgba(134, 157, 156, 0.5)')    // 设置边框颜色   支持rgba 和 十六进制  格式
				
canvas.stroke()    // 绘制矩形
				
// 如果需要在矩形内放入数据,并且数据有可能超出矩形,建议添加下面方法 防止内容溢出
canvas.clip()  //  剪切边框内的内容

canvas.restore() // 恢复之前的绘图```

### 实现微信小程序 Canvas 绘制圆角矩形框的方法 在微信小程序中,Canvas 没有直接提供绘制圆角矩形的 API,因此需要通过组合绘制直线和圆弧来实现。以下是实现圆角矩形框的详细方法。 #### 方法原理 圆角矩形绘制思路是基于 Canvas 提供的 `arc` 和 `lineTo` 函数,通过组合四个直角和四段圆弧来实现。具体步骤如下: 1. **确定矩形的四个角**:每个角需要绘制一个 90° 的圆弧。 2. **绘制边与连接圆弧**:使用 `lineTo` 绘制矩形的四条边,并通过 `arc` 绘制四个角的圆弧。 3. **闭合路径**:最后闭合路径并填充或描边。 #### 示例代码 以下是一个完整的绘制圆角矩形的方法: ```javascript /** * 绘制圆角矩形框 * @param {Object} ctx - canvas 的绘图上下文 * @param {Number} x - 矩形左上角 x 坐标 * @param {Number} y - 矩形左上角 y 坐标 * @param {Number} w - 矩形宽度 * @param {Number} h - 矩形高度 * @param {Number} r - 圆角半径 * @param {String} [strokeColor = '#000'] - 描边颜色 */ function drawRoundedRect(ctx, x, y, w, h, r, strokeColor = '#000') { // 限制圆角半径,防止超出矩形尺寸 if (w < 2 * r) r = w / 2; if (h < 2 * r) r = h / 2; ctx.beginPath(); ctx.setStrokeStyle(strokeColor); // 左上角 ctx.moveTo(x + r, y); ctx.arcTo(x, y, x, y + r, r); // 右上角 ctx.lineTo(x + w, y + r); ctx.arcTo(x + w, y, x + w, y + h, r); // 右下角 ctx.lineTo(x + w - r, y + h); ctx.arcTo(x + w, y + h, x, y + h, r); // 左下角 ctx.lineTo(x + r, y + h); ctx.arcTo(x, y + h, x, y, r); // 闭合路径并描边 ctx.closePath(); ctx.stroke(); } ``` #### 使用示例 在页面的 `onLoad` 生命周期中调用绘制函数: ```javascript onLoad() { const ctx = wx.createCanvasContext('myCanvas'); drawRoundedRect(ctx, 50, 50, 200, 100, 20, '#FF0000'); ctx.draw(); } ``` #### 注意事项 - **兼容性问题**:部分 Android 设备上使用 `arcTo` 时可能出现问题,建议使用 `arc` 和 `lineTo` 的组合来替代 `arcTo` [^5]。 - **性能优化**:在频繁重绘的场景中,建议对绘图上下文进行缓存,避免重复创建和销毁。 - **自定义样式**:可以通过 `setStrokeStyle`、`setLineWidth` 等方法调整边框颜色、宽度等样式。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值