关键词搜索

源码搜索 ×
×

canvas笔记-使用arc与lineTo画圆角矩形及绘制2048棋盘

发布2020-06-02浏览1443次

详情内容

首先是使用arc圆所对应的参数为(默认为顺时针):

这里话矩形的话,对应的圆心顶点坐标为:

程序运行截图如下:

源码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <canvas id="canvas" style="border: 1px solid #aaa; display: block; margin: 50px auto;">
  9. 当前浏览器不支持canvas
  10. </canvas>
  11. <script>
  12. window.onload = function(){
  13. let canvas = document.getElementById("canvas");
  14. canvas.width = 800;
  15. canvas.height = 800;
  16. let context = canvas.getContext("https://cdn.jxasp.com:9143/image/2d");
  17. drawRoundRect(context, 100, 100, 600, 500, 50);
  18. }
  19. function drawRoundRect(cxt, x, y, width, height, radius){
  20. cxt.save();
  21. cxt.translate(x, y);
  22. pathRoundRect(cxt, width, height, radius);
  23. cxt.strokeStyle = "black";
  24. cxt.stroke();
  25. cxt.restore();
  26. }
  27. function pathRoundRect(cxt, width, height, radius){
  28. cxt.beginPath();
  29. cxt.arc(width - radius, height - radius, radius, 0, Math.PI / 2);
  30. cxt.lineTo(radius, height);
  31. cxt.arc(radius, height - radius, radius, Math.PI / 2, Math.PI);
  32. cxt.lineTo(0, radius);
  33. cxt.arc(radius, radius, radius, Math.PI, Math.PI * 3 / 2);
  34. cxt.lineTo(width - radius, 0);
  35. cxt.arc(width - radius, radius, radius, Math.PI * 3 / 2, Math.PI * 2);
  36. cxt.closePath();
  37. }
  38. </script>
  39. </body>
  40. </html>

这里先使用translate把坐标原点移动到指定的位置。

cxt.arc(width - radius, height - radius, radius, 0, Math.PI / 2);

画最右下角的弧。

cxt.lineTo(radius, height);

然后从最右下角的弧画到最左下角。

cxt.arc(radius, height - radius, radius, Math.PI / 2, Math.PI);

画左下角的圆弧。

cxt.lineTo(0, radius);

然后从最右下角的弧画到最左下角。

cxt.arc(radius, height - radius, radius, Math.PI / 2, Math.PI);

画左下角的圆弧。

cxt.lineTo(0, radius);

从最左下角的圆弧画到最左上角。

cxt.arc(radius, radius, radius, Math.PI, Math.PI * 3 / 2);

画最左上角的圆弧。

cxt.lineTo(width - radius, 0);

从最左上角圆弧画到最右端。

cxt.arc(width - radius, radius, radius, Math.PI * 3 / 2, Math.PI * 2);

画右上角圆弧。

cxt.closePath();

封口,也就是画最右边的那条边。

 

 

下面是画一个2048的棋盘

程序运行截图如下;

源码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <canvas id="canvas" style="border: 1px solid #aaa; display: block; margin: 50px auto;">
  9. 当前浏览器不支持canvas
  10. </canvas>
  11. <script>
  12. window.onload = function(){
  13. let canvas = document.getElementById("canvas");
  14. canvas.width = 800;
  15. canvas.height = 800;
  16. let context = canvas.getContext("https://cdn.jxasp.com:9143/image/2d");
  17. fillRoundRect(context, 150, 150, 500, 500, 10, "#bbada0");
  18. for(let i = 0; i < 4; i++){
  19. for(let j = 0; j < 4; j++){
  20. fillRoundRect(context, 170 + i * 120, 170 + j * 120, 100, 100, 6, "#ccc0b3");
  21. }
  22. }
  23. }
  24. function fillRoundRect(cxt, x, y, width, height, radius, /*optional*/ fillColor){
  25. if(2 * radius > width || 2 * radius > height){
  26. return;
  27. }
  28. cxt.save();
  29. cxt.translate(x, y);
  30. pathRoundRect(cxt, width, height, radius);
  31. cxt.fillStyle = fillColor || "black";
  32. cxt.fill();
  33. cxt.restore();
  34. }
  35. function strokeRoundRect(cxt, x, y, width, height, radius, /*optional*/lineWidth, /*optional*/strokeColor){
  36. if(2 * radius > width || 2 * radius > height){
  37. return;
  38. }
  39. cxt.save();
  40. cxt.translate(x, y);
  41. pathRoundRect(cxt, width, height, radius);
  42. cxt.lineWidth = lineWidth || 1;
  43. cxt.stroke();
  44. cxt.restore();
  45. }
  46. function pathRoundRect(cxt, width, height, radius){
  47. cxt.beginPath();
  48. cxt.arc(width - radius, height - radius, radius, 0, Math.PI / 2);
  49. cxt.lineTo(radius, height);
  50. cxt.arc(radius, height - radius, radius, Math.PI / 2, Math.PI);
  51. cxt.lineTo(0, radius);
  52. cxt.arc(radius, radius, radius, Math.PI, Math.PI * 3 / 2);
  53. cxt.lineTo(width - radius, 0);
  54. cxt.arc(width - radius, radius, radius, Math.PI * 3 / 2, Math.PI * 2);
  55. cxt.closePath();
  56. }
  57. </script>
  58. </body>
  59. </html>

这里就是先画了个大矩形,然后再大矩形下面画下矩形。

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载