{"id":1481,"date":"2024-11-10T10:16:14","date_gmt":"2024-11-10T02:16:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1481\/"},"modified":"2024-11-10T10:16:14","modified_gmt":"2024-11-10T02:16:14","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-canvas-%e4%b8%8a%e5%ae%9e%e7%8e%b0%e6%a0%b9%e6%8d%ae%e5%8e%8b%e5%8a%9b%e6%88%96%e6%8e%a5%e8%a7%a6%e9%9d%a2%e7%a7%af%e6%94%b9%e5%8f%98%e7%94%bb%e7%ac%94%e7%b2%97%e7%bb%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1481\/","title":{"rendered":"\u5982\u4f55\u5728 Canvas \u4e0a\u5b9e\u73b0\u6839\u636e\u538b\u529b\u6216\u63a5\u89e6\u9762\u79ef\u6539\u53d8\u753b\u7b14\u7c97\u7ec6\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173044605457700.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728 Canvas \u4e0a\u5b9e\u73b0\u6839\u636e\u538b\u529b\u6216\u63a5\u89e6\u9762\u79ef\u6539\u53d8\u753b\u7b14\u7c97\u7ec6\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728 Canvas \u4e0a\u5b9e\u73b0\u6839\u636e\u538b\u529b\u6216\u63a5\u89e6\u9762\u79ef\u6539\u53d8\u753b\u7b14\u7c97\u7ec6\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 canvas \u4e0a\u5b9e\u73b0\u6839\u636e\u538b\u529b\u6216\u63a5\u89e6\u9762\u79ef\u6539\u53d8\u753b\u7b14\u7c97\u7ec6\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528 Canvas \u5b9e\u73b0\u6839\u636e\u538b\u529b\u6216\u63a5\u89e6\u9762\u79ef\u6539\u53d8\u753b\u7b14\u7c97\u7ec6\uff0c\u53ef\u4ee5\u5229\u7528 TouchEvent \u7684\u5c5e\u6027\u3002<\/p>\n<p>\u5177\u4f53\u5b9e\u73b0\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u83b7\u53d6 TouchEvent \u7684\u89e6\u6478\u533a\u57df\u5c5e\u6027\uff0c\u5982 touches[0].radiusX \u548c touches[0].radiusY\u3002<\/li>\n<li>\u6839\u636e\u89e6\u6478\u533a\u57df\u7684\u534a\u5f84\u503c\u8ba1\u7b97\u753b\u7b14\u7c97\u7ec6\u3002\u503c\u8d8a\u5927\uff0c\u753b\u7b14\u8d8a\u7c97\u3002<\/li>\n<li>\u8bbe\u7f6e Canvas \u7ed8\u56fe\u4e0a\u4e0b\u6587\u4e2d\u7684 lineWidth \u5c5e\u6027\u4e3a\u8ba1\u7b97\u51fa\u7684\u753b\u7b14\u7c97\u7ec6\u503c\u3002<\/li>\n<li>\u5f00\u59cb\u7ed8\u5236\u7ebf\u6bb5\u6216\u5f62\u72b6\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 TouchEvent \u5b9e\u73b0\u538b\u529b\u611f\u5e94\u753b\u7b14\uff1a<\/p>\n<pre>const canvas = document.getElementById('canvas');\nconst ctx = canvas.getContext('2d');\n\ncanvas.addEventListener('touchstart', (e) =&gt; {\n  const touch = e.touches[0];\n  \/\/ \u8ba1\u7b97\u753b\u7b14\u7c97\u7ec6\n  const lineWidth = Math.max(touch.radiusX, touch.radiusY);\n  \/\/ \u8bbe\u7f6e\u753b\u7b14\u7c97\u7ec6\n  ctx.lineWidth = lineWidth;\n  \/\/ \u5f00\u59cb\u7ed8\u5236\n  ctx.beginPath();\n  ctx.moveTo(touch.clientX, touch.clientY);\n});\n\ncanvas.addEventListener('touchmove', (e) =&gt; {\n  const touch = e.touches[0];\n  \/\/ \u66f4\u65b0\u753b\u7b14\u7c97\u7ec6\n  const lineWidth = Math.max(touch.radiusX, touch.radiusY);\n  ctx.lineWidth = lineWidth;\n  \/\/ \u7ee7\u7eed\u7ed8\u5236\n  ctx.lineTo(touch.clientX, touch.clientY);\n  ctx.stroke();\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728 Canvas \u4e0a\u5b9e\u73b0\u6839\u636e\u538b\u529b\u6216\u63a5\u89e6\u9762\u79ef\u6539\u53d8\u753b\u7b14\u7c97\u7ec6\u7684\u6548\u679c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728 Canvas \u4e0a\u5b9e\u73b0\u6839\u636e\u538b\u529b\u6216\u63a5\u89e6\u9762\u79ef\u6539\u53d8\u753b\u7b14\u7c97\u7ec6\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728 canvas \u4e0a\u5b9e\u73b0\u6839\u636e\u538b\u529b\u6216\u63a5\u89e6\u9762\u79ef\u6539\u53d8\u753b\u7b14\u7c97\u7ec6\uff1f \u4f7f\u7528 Canvas \u5b9e\u73b0\u6839\u636e\u538b\u529b\u6216\u63a5\u89e6\u9762\u79ef\u6539\u53d8\u753b\u7b14\u7c97\u7ec6\uff0c\u53ef\u4ee5\u5229\u7528 TouchEvent \u7684\u5c5e\u6027\u3002 \u5177\u4f53\u5b9e\u73b0\u6b65\u9aa4\u5982\u4e0b\uff1a \u83b7\u53d6 TouchEvent \u7684\u89e6\u6478\u533a\u57df\u5c5e\u6027\uff0c\u5982 touches[0].radiusX \u548c touches[0].radiusY\u3002 \u6839\u636e\u89e6\u6478\u533a\u57df\u7684\u534a\u5f84\u503c\u8ba1\u7b97\u753b\u7b14\u7c97\u7ec6\u3002\u503c\u8d8a\u5927\uff0c\u753b\u7b14\u8d8a\u7c97\u3002 \u8bbe\u7f6e Canvas \u7ed8\u56fe\u4e0a\u4e0b\u6587\u4e2d\u7684 lineWidth \u5c5e\u6027\u4e3a\u8ba1\u7b97\u51fa\u7684\u753b\u7b14\u7c97\u7ec6\u503c\u3002 \u5f00\u59cb\u7ed8\u5236\u7ebf\u6bb5\u6216\u5f62\u72b6\u3002 \u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 TouchEvent \u5b9e\u73b0\u538b\u529b\u611f\u5e94\u753b\u7b14\uff1a const canvas = document.getElementById(&#8216;canvas&#8217;); const ctx = canvas.getContext(&#8216;2d&#8217;); canvas.addEventListener(&#8216;touchstart&#8217;, (e) =&gt; { const touch = e.touches[0]; \/\/ \u8ba1\u7b97\u753b\u7b14\u7c97\u7ec6 const lineWidth = Math.max(touch.radiusX, touch.radiusY); \/\/ \u8bbe\u7f6e\u753b\u7b14\u7c97\u7ec6 ctx.lineWidth = lineWidth; \/\/ \u5f00\u59cb\u7ed8\u5236 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-1481","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1481","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=1481"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1481\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}