教你如何用Canvas绘制整身的哆啦A梦

喜欢ヅ旅行 2023-02-17 13:29 129阅读 0赞

教你如何用Canvas绘制整身的哆啦A梦

上牌你文章我们说到了如何使用canvas绘制哆啦A梦,但当时只是绘制了哆啦A梦的头部,之后有学员留言说可不可以教大家绘制整身的哆啦A梦,想了想,决定安排一下。如图:

在这里插入图片描述

HTML代码:

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

CSS代码:

  1. * {
  2. margin: 0;
  3. }
  4. #my_canvas {
  5. /* display: block; margin: 0 auto; */
  6. background: pink;
  7. }
  8. #my_canvas2 {
  9. background: skyblue;
  10. }

JS代码:

  1. var oCanvas = document.getElementById("my_canvas");
  2. oCanvas.width = 680;
  3. oCanvas.height = 840;
  4. var context = oCanvas.getContext("2d");
  5. // 1.大脑袋
  6. context.beginPath();
  7. context.arc(340, 260, 230, Math.PI / 3, Math.PI / 3 * 2, true);
  8. context.lineWidth = "8";
  9. context.closePath();
  10. context.stroke();
  11. context.fillStyle = "rgb(34,118,207)";
  12. context.fill();
  13. // 2.大脸蛋子
  14. // context.ellipse(x,y,r1,r2,旋转的角度,起始角度,结束角度)
  15. context.beginPath();
  16. context.lineWidth = "8";
  17. context.ellipse(340, 310, 200, 180, 0, Math.PI / 4, Math.PI / 4 * 3, true);
  18. context.stroke();
  19. context.fillStyle = "white";
  20. context.fill();
  21. // 3.大嘴巴子
  22. context.beginPath();
  23. context.lineWidth = "5";
  24. context.moveTo(340, 245);
  25. context.lineTo(340, 405);
  26. context.stroke();
  27. context.fillStyle = "black";
  28. context.fill();
  29. context.beginPath();
  30. context.ellipse(340, 215, 200, 190, 0, Math.PI / 4, Math.PI / 4 * 3, );
  31. context.stroke();
  32. // 4.大眼珠子
  33. // 左眼
  34. context.beginPath();
  35. context.lineWidth = "8";
  36. context.ellipse(288, 160, 50, 60, 0, 0, Math.PI * 2);
  37. context.stroke();
  38. context.fillStyle = "white";
  39. context.fill();
  40. context.beginPath();
  41. context.arc(295, 190, 20, 0, -Math.PI, true);
  42. context.stroke();
  43. // 右眼
  44. context.beginPath();
  45. context.ellipse(392, 160, 50, 60, 0, 0, Math.PI * 2);
  46. context.stroke();
  47. context.fillStyle = "white";
  48. context.fill();
  49. context.beginPath();
  50. context.arc(385, 190, 20, 0, -Math.PI, true);
  51. context.stroke();
  52. // 5.大鼻子
  53. context.beginPath();
  54. context.lineWidth = "8";
  55. context.arc(340, 235, 30, 0, 2 * Math.PI);
  56. context.stroke();
  57. context.fillStyle = "red";
  58. context.fill();
  59. // 6.画胡子
  60. context.lineWidth = "5";
  61. huzi(165, 224, 270, 265);
  62. huzi(153, 280, 262, 280);
  63. huzi(165, 320, 262, 300);
  64. huzi(416, 265, 515, 224);
  65. huzi(425, 280, 530, 280);
  66. huzi(425, 300, 522, 320);
  67. // 画胡子 函数
  68. function huzi(x1, y1, x2, y2) {
  69. context.beginPath();
  70. context.moveTo(x1, y1);
  71. context.lineTo(x2, y2);
  72. context.stroke();
  73. }
  74. // 胳膊
  75. // 左臂
  76. context.save();
  77. context.beginPath();
  78. context.moveTo(190, 439);
  79. context.lineTo(80, 504);
  80. context.lineTo(110, 562);
  81. context.lineTo(180, 531);
  82. context.lineTo(180, 500);
  83. context.lineTo(180, 760);
  84. context.lineTo(320, 760);
  85. // 裆
  86. context.arcTo(320, 750, 340, 720, 20);
  87. context.arcTo(360, 720, 360, 750, 20);
  88. context.lineTo(360, 760);
  89. context.lineTo(500, 760);
  90. context.lineTo(500, 500);
  91. context.lineTo(500, 531);
  92. context.lineTo(570, 562);
  93. context.lineTo(600, 504);
  94. context.lineTo(490, 439);
  95. context.stroke();
  96. context.closePath();
  97. context.fillStyle = "rgb(12,162,231)";
  98. context.fill();
  99. context.stroke();
  100. // 左拳
  101. context.beginPath();
  102. context.lineWidth = "8";
  103. context.arc(70, 545, 45, 0, Math.PI * 2);
  104. context.stroke();
  105. context.fillStyle = "white";
  106. context.fill();
  107. // 右拳
  108. context.beginPath();
  109. context.lineWidth = "8";
  110. context.arc(610, 545, 45, 0, Math.PI * 2);
  111. context.stroke();
  112. context.fillStyle = "white";
  113. context.fill();
  114. // 脚
  115. // 左脚
  116. context.save();
  117. context.beginPath();
  118. context.lineWidth = "8";
  119. context.moveTo(180, 760);
  120. context.lineTo(315, 760);
  121. context.arc(315, 780, 20, -Math.PI / 2, Math.PI / 2);
  122. context.lineTo(180, 800);
  123. context.arc(180, 780, 20, -Math.PI / 2, Math.PI / 2, true);
  124. context.fillStyle = "white";
  125. context.lineCap = "round";
  126. context.stroke();
  127. context.fill();
  128. context.restore();
  129. // 右脚
  130. context.save();
  131. context.beginPath();
  132. context.lineWidth = "8";
  133. context.moveTo(500, 760);
  134. context.lineTo(365, 760);
  135. context.arc(365, 780, 20, -Math.PI / 2, Math.PI / 2, true);
  136. context.lineTo(500, 800);
  137. context.arc(500, 780, 20, -Math.PI / 2, Math.PI / 2);
  138. context.fillStyle = "white";
  139. context.lineCap = "round";
  140. context.stroke();
  141. context.fill();
  142. context.restore();
  143. // 肚子
  144. context.beginPath();
  145. context.lineWidth = "8";
  146. context.arc(340, 550, 130, -Math.PI / 4, -Math.PI / 4 * 3);
  147. context.closePath();
  148. context.stroke();
  149. context.fillStyle = "white";
  150. context.fill();
  151. context.beginPath();
  152. context.arc(340, 550, 100, 0, -Math.PI);
  153. context.closePath();
  154. context.stroke();
  155. context.fillStyle = "white";
  156. context.fill();
  157. // 领带
  158. context.save();
  159. context.beginPath();
  160. context.lineWidth = "8";
  161. context.moveTo(190, 430);
  162. context.lineTo(490, 430);
  163. context.arc(490, 450, 20, -Math.PI / 2, Math.PI / 2);
  164. context.lineTo(190, 470);
  165. context.arc(190, 450, 20, -Math.PI / 2, Math.PI / 2, true);
  166. context.fillStyle = "red";
  167. context.lineCap = "round";
  168. context.stroke();
  169. context.fill();
  170. context.restore();
  171. // 铃铛
  172. context.beginPath();
  173. context.arc(340, 470, 30, 0, Math.PI * 2);
  174. context.stroke();
  175. context.fillStyle = "yellow";
  176. context.fill();
  177. context.beginPath();
  178. context.lineWidth = "5";
  179. context.moveTo(312, 455);
  180. context.lineTo(368, 455);
  181. context.stroke();
  182. context.beginPath();
  183. context.moveTo(310, 462);
  184. context.lineTo(370, 462);
  185. context.stroke();
  186. context.beginPath();
  187. context.arc(340, 475, 5, 0, Math.PI * 2);
  188. context.stroke();
  189. context.fillStyle = "black";
  190. context.fill();
  191. context.beginPath();
  192. context.moveTo(340, 480);
  193. context.lineTo(340, 500);
  194. context.stroke();
  195. var oCanvas2 = document.getElementById("my_canvas2");
  196. oCanvas2.width = 680;
  197. oCanvas2.height = 840;
  198. var context2 = oCanvas2.getContext("2d");
  199. // 1.头
  200. context2.beginPath();
  201. context2.lineWidth = "8";
  202. context2.arc(340, 260, 230, Math.PI / 3, Math.PI / 3 * 2, true);
  203. context2.closePath();
  204. context2.stroke();
  205. context2.fillStyle = "yellow";
  206. context2.fill();
  207. // 2.脸 画椭圆
  208. context2.beginPath();
  209. context2.ellipse(340, 310, 200, 180, 0, Math.PI / 4, Math.PI / 4 * 3, true);
  210. context2.closePath();
  211. context2.stroke();
  212. context2.fillStyle = "white";
  213. context2.fill();
  214. // 3.眼睛
  215. // 左眼
  216. context2.beginPath();
  217. context2.ellipse(288,160,50,60,0,0,Math.PI*2);
  218. context2.stroke();
  219. context2.fillStyle="white";
  220. context2.fill();
  221. context2.beginPath();
  222. context2.arc(295,190,20,0,Math.PI,true);
  223. context2.stroke();
  224. // 右眼
  225. context2.beginPath();
  226. context2.ellipse(392,160,50,60,0,0,Math.PI*2);
  227. context2.stroke();
  228. context2.fillStyle="white";
  229. context2.fill();
  230. context2.beginPath();
  231. context2.arc(385,190,20,0,Math.PI,true);
  232. context2.stroke();
  233. // 4.鼻子
  234. context2.beginPath();
  235. context2.arc(340,235,30,0,Math.PI*2);
  236. context2.stroke();
  237. context2.fillStyle="red";
  238. context2.fill();
  239. // 5.嘴巴
  240. context2.beginPath();
  241. context2.lineWidth="5";
  242. context2.moveTo(340,265);
  243. context2.lineTo(340,405);
  244. context2.stroke();
  245. context2.beginPath();
  246. context2.ellipse(340,215,200,190,0,Math.PI/4,Math.PI/4*3);
  247. context2.stroke();
  248. // 6.胡子
  249. context2.lineWidth="5";
  250. huzi2(165, 224, 270, 265);
  251. huzi2(153, 280, 262, 280);
  252. huzi2(165, 320, 262, 300);
  253. huzi2(416, 265, 515, 224);
  254. huzi2(425, 280, 530, 280);
  255. huzi2(425, 300, 522, 320);
  256. // 画胡子 函数
  257. function huzi2(x1, y1, x2, y2) {
  258. context2.beginPath();
  259. context2.moveTo(x1, y1);
  260. context2.lineTo(x2, y2);
  261. context2.stroke();
  262. }
  263. // 7.身体部分
  264. // 左臂
  265. context2.beginPath();
  266. context2.moveTo(190,439);
  267. context2.lineTo(80,504);
  268. context2.lineTo(110,562);
  269. context2.lineTo(180,531);
  270. context2.lineTo(180, 500);
  271. context2.lineTo(180, 760);
  272. context2.lineTo(320, 760);
  273. // 裆
  274. context2.arcTo(320,750,360,720,20);
  275. context2.arcTo(360,720,360,750,20);
  276. context2.lineTo(360, 760);
  277. context2.lineTo(500, 760);
  278. context2.lineTo(500, 500);
  279. context2.lineTo(500, 531);
  280. context2.lineTo(570, 562);
  281. context2.lineTo(600, 504);
  282. context2.lineTo(490, 439);
  283. context2.closePath();
  284. context2.fillStyle="yellow";
  285. context2.fill();
  286. context2.stroke();
  287. // 7.左手
  288. context2.beginPath();
  289. context2.arc(70,545,45,0,Math.PI*2);
  290. context2.fillStyle="white";
  291. context2.fill();
  292. context2.stroke();
  293. // 右手
  294. context2.beginPath();
  295. context2.arc(610,545,45,0,Math.PI*2);
  296. context2.fillStyle="white";
  297. context2.fill();
  298. context2.stroke();
  299. // 8.左脚
  300. context2.save();
  301. context2.beginPath();
  302. context2.lineWidth="8";
  303. context2.moveTo(180,760);
  304. context2.lineTo(315,760);
  305. context2.arc(315,780,20,-Math.PI/2,Math.PI/2);
  306. context2.lineTo(180,800);
  307. context2.arc(180,780,20,-Math.PI/2,Math.PI/2,true);
  308. context2.stroke();
  309. context2.fillStyle="white";
  310. context2.fill();
  311. context2.restore();
  312. // 右脚
  313. context2.save();
  314. context2.beginPath();
  315. context2.lineWidth="8";
  316. context2.moveTo(500,760);
  317. context2.lineTo(365,760);
  318. context2.arc(365,780,20,-Math.PI/2,Math.PI/2,true);
  319. context2.lineTo(500,800);
  320. context2.arc(500,780,20,-Math.PI/2,Math.PI/2);
  321. context2.stroke();
  322. context2.fillStyle="white";
  323. context2.fill();
  324. context2.restore();
  325. // 10.肚子
  326. context2.beginPath();
  327. context2.arc(340,550,130,-Math.PI/4,-Math.PI/4*3);
  328. context2.closePath();
  329. context2.fillStyle="white";
  330. context2.fill();
  331. context2.stroke();
  332. // 口袋
  333. context2.beginPath();
  334. context2.arc(340,550,100,0,Math.PI);
  335. context2.closePath();
  336. context2.fillStyle="white";
  337. context2.fill();
  338. context2.stroke();
  339. // 11.领带
  340. context2.save();
  341. context2.beginPath();
  342. context2.lineWidth="8";
  343. context2.moveTo(190,430);
  344. context2.lineTo(490,430);
  345. context2.arc(490,450,20,-Math.PI/2,Math.PI/2);
  346. context2.lineTo(190,470);
  347. context2.arc(190,450,20,-Math.PI/2,Math.PI/2,true);
  348. context2.stroke();
  349. context2.fillStyle="red";
  350. context2.fill();
  351. context2.restore();
  352. // 12.铃铛
  353. context2.beginPath();
  354. context2.arc(340,470,30,0,Math.PI*2);
  355. context2.fillStyle="skyblue";
  356. context2.fill();
  357. context2.stroke();
  358. context2.lineWidth = "5";
  359. huzi2(312, 455,368, 455);
  360. huzi2(310, 462,370, 462)
  361. huzi2(340, 480,340, 500)
  362. context2.beginPath();
  363. context2.arc(340, 475, 5, 0, Math.PI * 2);
  364. context2.fillStyle = "black";
  365. context2.fill();
  366. context2.stroke();

总结: 绘制整身的哆啦A梦主要涉及到了对画圆方法的使用,需要控制好角度才能绘制出来头部和口袋。同时还要注意绘制脚丫时是如何让拼接的路径。

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

发表评论

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

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

相关阅读