圆角矩形是由四段线条和四个1/4圆弧组成,拆解如下。
因为我们要写的是函数而不是一个固定的圆角矩形,所以这里列出的是函数需要的参数。分析好之后,直接敲出代码。
- <!DOCTYPE html> <html lang="zh">
- <head> <meta charset="UTF-8">
- <title>圆角矩形</title> <style>
- body { background: url("./images/bg3.jpg") repeat; } #canvas { border: 1px solid #aaaaaa; display: block; margin: 50px auto; }
- </style> </head>
- <body> <div id="canvas-warp">
- <canvas id="canvas"> 你的浏览器居然不支持Canvas?!赶快换一个吧!!
- </canvas> </div>
- <script>
- window.onload = function(){ var canvas = document.getElementById("canvas");
- canvas.width = 800; canvas.height = 600;
- var context = canvas.getContext("2d"); context.fillStyle = "#FFF";
- context.fillRect(0,0,800,600);
- drawRoundRect(context, 200, 100, 400, 400, 50); context.strokeStyle = "#0078AA";
- context.stroke(); }
- function drawRoundRect(cxt, x, y, width, height, radius){
- cxt.beginPath(); cxt.arc(x + radius, y + radius, radius, Math.PI, Math.PI * 3 / 2);
- cxt.lineTo(width - radius + x, y); cxt.arc(width - radius + x, radius + y, radius, Math.PI * 3 / 2, Math.PI * 2);
- cxt.lineTo(width + x, height + y - radius); cxt.arc(width - radius + x, height - radius + y, radius, 0, Math.PI * 1 / 2);
- cxt.lineTo(radius + x, height +y); cxt.arc(radius + x, height - radius + y, radius, Math.PI * 1 / 2, Math.PI);
- cxt.closePath(); }
- </script> </body>
- </html>
相关文章
大家在看
-

福昕阅读器如何复制文字
2023-03-02
0万阅读
-

Dubbo 系列JDK SPI 原理解析
2023-02-24
0万阅读
-

欲为王者至尊 888元志美4倍速DVD刻录机评测
2023-02-22
6万阅读
-

拼音标注能手--中华拼读王
2023-02-17
10万阅读
-

360随身WiFi怎么设置隐藏信号 360随身wifi隐藏信号
2023-02-12
9万阅读
-

怎么用酷盘分享文件给好友
2023-02-11
12万阅读
-

企业应如何防范内存抓取恶意软件
2023-02-10
6万阅读
-

如何知道电脑是否含有病毒
2023-02-09
8万阅读
-

美图秀秀让你的国庆旅游照片更出彩
2023-01-16
9万阅读
-

u启动怎么用 U启动使用教程
2023-01-14
4万阅读
-

福昕阅读器如何复制文字
2023-03-02
0万阅读
-

Dubbo 系列JDK SPI 原理解析
2023-02-24
0万阅读
-

欲为王者至尊 888元志美4倍速DVD刻录机评测
2023-02-22
6万阅读
-

拼音标注能手--中华拼读王
2023-02-17
10万阅读
-

360随身WiFi怎么设置隐藏信号 360随身wifi隐藏信号
2023-02-12
9万阅读
-

怎么用酷盘分享文件给好友
2023-02-11
12万阅读
-

企业应如何防范内存抓取恶意软件
2023-02-10
6万阅读
-

如何知道电脑是否含有病毒
2023-02-09
8万阅读
-

美图秀秀让你的国庆旅游照片更出彩
2023-01-16
9万阅读
-

u启动怎么用 U启动使用教程
2023-01-14
4万阅读
