• canvas基础2 -- 形状


     七巧板

    七巧板本质上就是 分别由几个直线 拼成一个个图形,再将这些图形结合起来

    1. var tangram = [
    2. { p: [{ x: 0, y: 0 }, { x: 800, y: 0 }, { x: 400, y: 400 }], color: "#caff67" },
    3. { p: [{ x: 0, y: 0 }, { x: 400, y: 400 }, { x: 0, y: 800 }], color: "#67beef" },
    4. { p: [{ x: 800, y: 0 }, { x: 800, y: 400 }, { x: 600, y: 600 }, { x: 600, y: 200 }], color: "#ef3d61" },
    5. { p: [{ x: 600, y: 200 }, { x: 600, y: 600 }, { x: 400, y: 400 }], color: "#f9f5la" },
    6. { p: [{ x: 400, y: 400 }, { x: 600, y: 600 }, { x: 400, y: 800 }, { x: 200, y: 600 }], color: "#a594c0" },
    7. { p: [{ x: 200, y: 600 }, { x: 400, y: 800 }, { x: 0, y: 800 }], color: "#fa8ecc" },
    8. { p: [{ x: 800, y: 400 }, { x: 800, y: 800 }, { x: 400, y: 800 }], color: "#f6ca29" }
    9. ]
    10. const canvas = document.getElementById('canvas')
    11. canvas.width = 800
    12. canvas.height = 800
    13. const context = canvas.getContext('2d')
    14. for (let i = 0; i < tangram.length; i++) {
    15. draw(tangram[i], context)
    16. }
    17. function draw(piece, cxt) {
    18. cxt.beginPath()
    19. cxt.moveTo(piece.p[0].x, piece.p[0].y)
    20. for (var i = 1; i < piece.p.length; i++) {
    21. cxt.lineTo(piece.p[i].x, piece.p[i].y)
    22. }
    23. cxt.closePath()
    24. cxt.fillStyle = piece.color
    25. cxt.fill()
    26. cxt.strokeStyle = "black"
    27. cxt.lineWidth = 3
    28. cxt.stroke()
    29. }

    图示:

    箭头

    1. context.beginPath()
    2. context.moveTo(100, 350)
    3. context.lineTo(500, 350)
    4. context.lineTo(500, 200)
    5. context.lineTo(700, 400)
    6. context.lineTo(500, 600)
    7. context.lineTo(500, 450)
    8. context.lineTo(100, 450)
    9. context.closePath()
    10. context.lineWidth = 5
    11. context.strokeStyle = '#058'
    12. context.stroke()

    图示:

    五角星

    如下图所示:五角星外面的五个顶点都在外圆上,里面的五个顶点都在内圆上

    五角星的10个顶点,外圆的每个顶点相差72度,同理,内圆的每个顶点也相差72度

    算出每个顶点的坐标

    最后用线段连接起来

    代码:

    1. const canvas = document.getElementById('canvas')
    2. canvas.width = 800
    3. canvas.height = 800
    4. const context = canvas.getContext('2d')
    5. const translateX = 400 // 让圆心在X轴的中心位置
    6. const bigRadius = 300 // 大圆半径
    7. const smallRadius = 150 // 小圆半径
    8. context.beginPath()
    9. for(let i = 0; i < 5; i++) {
    10. context.lineTo(
    11. Math.cos((18 + i * 72) / 180 * Math.PI) * bigRadius + translateX,
    12. -Math.sin((18 + i * 72) / 180 * Math.PI) * bigRadius + translateX
    13. )
    14. context.lineTo(
    15. Math.cos((54 + i * 72) / 180 * Math.PI) * smallRadius + translateX,
    16. -Math.sin((54 + i * 72) / 180 * Math.PI) * smallRadius + translateX
    17. )
    18. }
    19. context.closePath()
    20. context.lineWidth = 10
    21. context.stroke()

    图示:

     绘制弯月

    代码:

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. </head>
    8. <body>
    9. <canvas id="canvas" style="border:1px solid #ccc;display:block;margin:50px auto;"></canvas>
    10. <script>
    11. const canvas = document.getElementById('canvas')
    12. canvas.width = 800
    13. canvas.height = 800
    14. const context = canvas.getContext('2d')
    15. fillMoon(context, 2, 400, 400, 300, 0)
    16. function fillMoon(cxt, d, x, y, R, rot, fillColor) {
    17. cxt.save()
    18. cxt.translate(x, y)
    19. cxt.rotate(rot * Math.PI / 180)
    20. cxt.scale(R, R)
    21. pathMoon(cxt, d)
    22. cxt.fillStyle = fillColor || '#fb5'
    23. cxt.fill()
    24. cxt.restore()
    25. }
    26. function pathMoon(cxt, d) {
    27. cxt.beginPath()
    28. cxt.arc(0, 0, 1, 0.5*Math.PI, 1.5*Math.PI, true)
    29. cxt.moveTo(0, -1)
    30. cxt.arcTo(d, 0, 0, 1, dis(0, -1, d, 0) / d)
    31. cxt.closePath()
    32. }
    33. function dis(x1, y1, x2, y2) {
    34. return Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))
    35. }
    36. </script>
    37. </body>
    38. </html>

    图示:

    星空

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. </head>
    8. <body>
    9. <canvas id="canvas" style="border:1px solid #ccc;display:block;margin:50px auto;"></canvas>
    10. <script>
    11. const canvas = document.getElementById('canvas')
    12. canvas.width = 1200
    13. canvas.height = 800
    14. const context = canvas.getContext('2d')
    15. const skyStyle = context.createLinearGradient(0, 0, 0, canvas.height)
    16. skyStyle.addColorStop(0.0, 'black')
    17. skyStyle.addColorStop(1.0, '#035')
    18. context.fillStyle = skyStyle
    19. context.fillRect(0, 0, canvas.width, canvas.height)
    20. for (let i = 0; i < 200; i++) {
    21. const R = Math.random() * 5 + 5
    22. const r = R / 2.0
    23. const x = Math.random() * canvas.width
    24. const y = Math.random() * canvas.height * 0.65
    25. const rot = Math.random() * 360
    26. drawStar(context, r, R, x, y, rot)
    27. }
    28. fillMoon(context, 2, 900, 200, 100, 30)
    29. drawLand(context)
    30. function drawLand(cxt) {
    31. cxt.save()
    32. cxt.beginPath()
    33. cxt.moveTo(0, 600)
    34. cxt.bezierCurveTo(540, 400, 660, 800, 1200, 600)
    35. cxt.lineTo(1200, 800)
    36. cxt.lineTo(0, 800)
    37. cxt.closePath()
    38. const landStyle = cxt.createLinearGradient(0, 800, 0, 0)
    39. landStyle.addColorStop(0.0, '#030')
    40. landStyle.addColorStop(1.0, '#580')
    41. cxt.fillStyle = landStyle
    42. cxt.fill()
    43. cxt.restore()
    44. }
    45. function fillMoon(cxt, d, x, y, R, rot, fillColor) {
    46. cxt.save()
    47. cxt.translate(x, y)
    48. cxt.rotate(rot * Math.PI / 180)
    49. cxt.scale(R, R)
    50. pathMoon(cxt, d)
    51. cxt.fillStyle = fillColor || '#fb5'
    52. cxt.fill()
    53. cxt.restore()
    54. }
    55. function pathMoon(cxt, d) {
    56. cxt.beginPath()
    57. cxt.arc(0, 0, 1, 0.5 * Math.PI, 1.5 * Math.PI, true)
    58. cxt.moveTo(0, -1)
    59. //cxt.arcTo(d, 0, 0, 1, dis(0, -1, d, 0) / d)
    60. cxt.quadraticCurveTo(1.2, 0, 0, 1)
    61. cxt.closePath()
    62. }
    63. function dis(x1, y1, x2, y2) {
    64. return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))
    65. }
    66. function drawStar(cxt, r, R, x, y, rot) {
    67. cxt.save()
    68. cxt.translate(x, y)
    69. cxt.rotate(rot / 180 * Math.PI)
    70. cxt.scale(R, R)
    71. starPath(cxt)
    72. cxt.fillStyle = '#fb3'
    73. // cxt.strokeStyle = '#fd5'
    74. // cxt.lineWidth = 3
    75. // cxt.lineJoin = 'round'
    76. cxt.fill()
    77. //cxt.stroke()
    78. cxt.restore()
    79. }
    80. function starPath(cxt) {
    81. cxt.beginPath()
    82. for (let i = 0; i < 5; i++) {
    83. cxt.lineTo(
    84. Math.cos((18 + i * 72) / 180 * Math.PI),
    85. -Math.sin((18 + i * 72) / 180 * Math.PI)
    86. )
    87. cxt.lineTo(
    88. Math.cos((54 + i * 72) / 180 * Math.PI) * 0.5,
    89. -Math.sin((54 + i * 72) / 180 * Math.PI) * 0.5
    90. )
    91. }
    92. cxt.closePath()
    93. }
    94. </script>
    95. </body>
    96. </html>

    图示:

    圆角矩形

    本质上是4条线段和4个弧线,共8个部分组成

    代码:

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. </head>
    8. <body>
    9. <canvas id="canvas" style="border:1px solid #ccc;display:block;margin:50px auto;"></canvas>
    10. <script>
    11. const canvas = document.getElementById('canvas')
    12. canvas.width = 800
    13. canvas.height = 800
    14. const context = canvas.getContext('2d')
    15. drawRoundRect(context, 100, 100, 600, 400, 20)
    16. function drawRoundRect(cxt, x, y, width, height, radius) {
    17. cxt.save()
    18. cxt.translate(x, y)
    19. pathRoundRect(cxt, width, height, radius)
    20. cxt.strokeStyle = 'black'
    21. cxt.stroke()
    22. cxt.restore()
    23. }
    24. function pathRoundRect(cxt, width, height, radius) {
    25. cxt.beginPath()
    26. cxt.arc(width - radius, height - radius, radius, 0, Math.PI / 2)
    27. cxt.lineTo(radius, height)
    28. cxt.arc(radius, height - radius, radius, Math.PI / 2, Math.PI)
    29. cxt.lineTo(0, radius)
    30. cxt.arc(radius, radius, radius, Math.PI, Math.PI * 3 / 2)
    31. cxt.lineTo(width - radius, 0)
    32. cxt.arc(width - radius, radius, radius, Math.PI * 3 / 2, Math.PI * 2)
    33. cxt.closePath()
    34. }
    35. </script>
    36. </body>
    37. </html>

    图示:

    绘制2048棋盘

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

    图示:

    1

  • 相关阅读:
    2024北京护眼产品展/北京眼视光展/北京叶黄素展/中国眼博会
    CentOS 7 通过 yum 安装 MariaDB(Mysql)
    线性筛求欧拉函数前n个和
    SpringBoot整合knife4j3.0.3
    低代码维格云明细视图入门教程
    计算机网络——应用层重点协议【HTTP协议】
    [附源码]计算机毕业设计基于web的羽毛球管理系统
    【华为机试真题Java】用户调度问题
    JavaScript自我学习
    chrome 相关设置
  • 原文地址:https://blog.csdn.net/m0_38066007/article/details/124899873