酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

写一段代码包含圆角矩形,用canvas实现

当然可以!以下是一个示例代码,使用Canvas绘制一个带有圆角的矩形: ```html <!DOCTYPE html> <html> <head> <title>Canvas圆角矩形</title> </head> <body> <canvas id="myCanvas" width="500" height="500"></canvas> <script> var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); var x = 50; // 矩形左上角的x坐标 var y = 50; // 矩形左上角的y坐标 var width = 200; // 矩形的宽度 var height = 100; // 矩形的高度 var cornerRadius = 20; // 圆角的半径 ctx.beginPath(); ctx.moveTo(x + cornerRadius, y); ctx.lineTo(x + width - cornerRadius, y); ctx.arcTo(x + width, y, x + width, y + cornerRadius, cornerRadius); ctx.lineTo(x + width, y + height - cornerRadius); ctx.arcTo(x + width, y + height, x + width - cornerRadius, y + height, cornerRadius); ctx.lineTo(x + cornerRadius, y + height); ctx.arcTo(x, y + height, x, y + height - cornerRadius, cornerRadius); ctx.lineTo(x, y + cornerRadius); ctx.arcTo(x, y, x + cornerRadius, y, cornerRadius); ctx.closePath(); ctx.lineWidth = 5; // 线宽 ctx.strokeStyle = "black"; // 边线颜色 ctx.fillStyle = "red"; // 填充颜色 ctx.stroke(); // 绘制边线 ctx.fill(); // 绘制填充 </script> </body> </html> ``` 这段代码使用`ctx.arcTo()`方法绘制圆角,通过调整`cornerRadius`变量的值来改变圆角的大小。可以将上述代码复制粘贴到一个HTML文件中,然后在浏览器中打开,就可以看到绘制的圆角矩形啦! [2023-10-05 00:44:58 | AI写代码神器 | 1387点数解答]

相关提问