教你如何利用canvas画布绘制哆啦A梦

爱被打了一巴掌 2023-02-17 03:43 134阅读 0赞

教你如何利用canvas画布绘制哆啦A梦

最近一直在练习使用canvas画布标签,今天教大家如何使用canvas画布绘制哆啦A梦。如图:

在这里插入图片描述

HTML代码:

  1. <canvas id="my_canvas"></canvas>

CSS代码:

  1. canvas {
  2. display:block;
  3. margin:0 auto;
  4. background: pink
  5. }

JavaScript代码:

  1. var oCanvas = document.getElementById("my_canvas");
  2. oCanvas.width = 600;
  3. oCanvas.height = 600;
  4. var context = oCanvas.getContext("2d");
  5. // document.onclick = function (ev) {
  6. // console.log(ev.pageX, ev.pageY)
  7. // }
  8. // 1.大脑袋
  9. context.beginPath();
  10. context.arc(300, 300, 250, 0, Math.PI * 2);
  11. context.lineWidth = "5";
  12. context.stroke();
  13. context.fillStyle = "rgb(34,118,207)";
  14. context.fill();
  15. // 2.大脸蛋子
  16. // context.scale(1,0.9);
  17. // context.beginPath();
  18. // context.arc(300,410,200,0,Math.PI*2);
  19. // context.lineWidth="5";
  20. // context.stroke();
  21. // context.ellipse(x,y,r1,r2,旋转的角度,起始角度,结束角度)
  22. context.beginPath();
  23. context.ellipse(300, 380, 200, 170, 0, 0, Math.PI * 2);
  24. context.stroke();
  25. context.fillStyle = "white";
  26. context.fill();
  27. // 3.大嘴巴子
  28. context.beginPath();
  29. context.arc(300, 460, 50, 0, Math.PI * 2);
  30. context.stroke();
  31. context.fillStyle = "black";
  32. context.fill();
  33. context.beginPath();
  34. context.ellipse(300, 470, 48, 40, 0, 0, Math.PI * 2);
  35. context.stroke();
  36. context.fillStyle = "red";
  37. context.fill();
  38. // 4.大眼珠子
  39. context.beginPath();
  40. context.ellipse(248, 230, 50, 60, 0, 0, Math.PI * 2);
  41. context.stroke();
  42. context.fillStyle = "white";
  43. context.fill();
  44. context.beginPath();
  45. context.ellipse(270, 230, 20, 30, 0, 0, Math.PI * 2);
  46. context.stroke();
  47. context.fillStyle = "black";
  48. context.fill();
  49. context.beginPath();
  50. context.ellipse(270, 230, 5, 10, 0, 0, Math.PI * 2);
  51. context.stroke();
  52. context.fillStyle = "white";
  53. context.fill();
  54. context.beginPath();
  55. context.ellipse(352, 230, 50, 60, 0, 0, Math.PI * 2);
  56. context.stroke();
  57. context.fillStyle = "white";
  58. context.fill();
  59. context.beginPath();
  60. context.ellipse(330, 230, 20, 30, 0, 0, Math.PI * 2);
  61. context.stroke();
  62. context.fillStyle = "black";
  63. context.fill();
  64. context.beginPath();
  65. context.ellipse(330, 230, 5, 10, 0, 0, Math.PI * 2);
  66. context.stroke();
  67. context.fillStyle = "white";
  68. context.fill();
  69. // 5.大鼻子
  70. context.beginPath();
  71. context.lineWidth = "3";
  72. context.arc(300, 290, 30, 0, 2 * Math.PI);
  73. context.stroke();
  74. context.fillStyle = "red";
  75. context.fill();
  76. // 6.画胡子
  77. context.lineWidth="5";
  78. huzi(125,294,230,335);
  79. huzi(113,370,222,366);
  80. huzi(125,434,222,398);
  81. huzi(376,335,465,282);
  82. huzi(385,369,490,354);
  83. huzi(385,400,488,420);
  84. // 画胡子的方法
  85. function huzi(x1, y1, x2, y2) {
  86. context.beginPath();
  87. context.moveTo(x1, y1);
  88. context.lineTo(x2, y2);
  89. context.stroke();
  90. }

总结: 绘制哆啦A梦的头部其实很简单,只需要知道人如何绘制圆形和线条即可,同时要知道图层的前后顺序。

视频讲解链接:
https://www.bilibili.com/video/BV1454y1Q7Aq/

发表评论

表情:
评论列表 (有 0 条评论,134人围观)

还没有评论,来说两句吧...

相关阅读