{"id":1418,"date":"2024-11-10T08:31:30","date_gmt":"2024-11-10T00:31:30","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1418\/"},"modified":"2024-11-10T08:31:30","modified_gmt":"2024-11-10T00:31:30","slug":"%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87%e5%8e%8b%e5%8a%9b%e6%88%96%e6%8e%a5%e8%a7%a6%e9%9d%a2%e7%a7%af%e5%8a%a8%e6%80%81%e8%b0%83%e6%95%b4-canvas-%e7%ad%be%e5%90%8d%e6%88%96%e7%bb%98%e5%9b%be%e7%b2%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1418\/","title":{"rendered":"\u5982\u4f55\u901a\u8fc7\u538b\u529b\u6216\u63a5\u89e6\u9762\u79ef\u52a8\u6001\u8c03\u6574 Canvas \u7b7e\u540d\u6216\u7ed8\u56fe\u7c97\u7ec6\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173042874467525.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u901a\u8fc7\u538b\u529b\u6216\u63a5\u89e6\u9762\u79ef\u52a8\u6001\u8c03\u6574 Canvas \u7b7e\u540d\u6216\u7ed8\u56fe\u7c97\u7ec6\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u901a\u8fc7\u538b\u529b\u6216\u63a5\u89e6\u9762\u79ef\u52a8\u6001\u8c03\u6574 Canvas \u7b7e\u540d\u6216\u7ed8\u56fe\u7c97\u7ec6\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u6839\u636e\u538b\u529b\u6216\u63a5\u89e6\u9762\u79ef\u52a8\u6001\u8c03\u6574 canvas \u7b7e\u540d\u6216\u7ed8\u56fe\u7c97\u7ec6<\/strong><\/p>\n<p>\u5728\u79fb\u52a8\u8bbe\u5907\u4e0a\u4f7f\u7528 Canvas \u8fdb\u884c\u7b7e\u540d\u6216\u7ed8\u56fe\u65f6\uff0c\u9700\u8981\u6839\u636e\u7528\u6237\u65bd\u52a0\u7684\u538b\u529b\u6216\u63a5\u89e6\u9762\u79ef\u6765\u52a8\u6001\u8c03\u6574\u7ebf\u6761\u7c97\u7ec6\u3002\u4f8b\u5982\uff0c\u5f53\u7528\u6237\u7528\u529b\u6309\u538b\u65f6\uff0c\u7ebf\u6761\u5e94\u53d8\u7c97\uff0c\u800c\u8f7b\u6309\u65f6\u7ebf\u6761\u5e94\u53d8\u7ec6\u3002<\/p>\n<p><strong>\u5b9e\u73b0\u65b9\u6cd5<\/strong><\/p>\n<p>\u8981\u5728 Canvas \u4e2d\u5b9e\u73b0\u6839\u636e\u538b\u529b\u6216\u63a5\u89e6\u9762\u79ef\u6539\u53d8\u7ebf\u6761\u7c97\u7ec6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li> <strong>\u5229\u7528 TouchEvent \u5c5e\u6027\uff1a<\/strong> TouchEvent \u5bf9\u8c61\u5177\u6709 touchRadialPressure \u6216 tangentialPressure \u5c5e\u6027\uff0c\u8868\u793a\u7528\u6237\u65bd\u52a0\u7684\u538b\u529b\u3002\u6839\u636e\u8fd9\u4e9b\u5c5e\u6027\u7684\u503c\uff0c\u53ef\u4ee5\u8c03\u6574\u753b\u7b14\u7c97\u7ec6\u3002<\/li>\n<li> <strong>\u4f7f\u7528 getContext(&#8216;2d&#8217;)\uff1a<\/strong> getContext(&#8216;2d&#8217;) \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a 2D \u7ed8\u56fe\u4e0a\u4e0b\u6587\uff0c\u5176\u4e2d\u5305\u542b\u63a7\u5236\u7ebf\u5bbd\u7684\u65b9\u6cd5\uff0c\u5982 lineWidth\u3002<\/li>\n<li> <strong>\u76d1\u542c touchstart \u548c touchmove \u4e8b\u4ef6\uff1a<\/strong>\u5728 Canvas \u5143\u7d20\u4e0a\u76d1\u542c touchstart \u548c touchmove \u4e8b\u4ef6\uff0c\u5728\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u4e2d\u83b7\u53d6\u538b\u529b\u503c\uff0c\u7136\u540e\u76f8\u5e94\u5730\u66f4\u65b0\u753b\u7b14\u7c97\u7ec6\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528 TouchEvent \u5c5e\u6027\u548c getContext(&#8216;2d&#8217;) \u65b9\u6cd5\u5b9e\u73b0\u538b\u529b\u611f\u5e94\u7684 Canvas \u7ed8\u56fe\uff1a<\/p>\n<pre>const canvas = document.getElementById('canvas');\nconst context = canvas.getContext('2d');\n\ncanvas.addEventListener('touchstart', (e) =&gt; {\n  const pressure = e.touches[0].touchRadialPressure;\n  context.lineWidth = pressure * 10;\n});\n\ncanvas.addEventListener('touchmove', (e) =&gt; {\n  const pressure = e.touches[0].touchRadialPressure;\n  context.lineWidth = pressure * 10;\n  context.lineTo(e.touches[0].clientX, e.touches[0].clientY);\n  context.stroke();\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u53c2\u8003\u6587\u6863<\/strong><\/p>\n<ul>\n<li>[TouchEvent](https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/TouchEvent)<\/li>\n<li>[Canvas 2D API](https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Canvas_API\/Tutorial\/Drawing_shapes)<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u901a\u8fc7\u538b\u529b\u6216\u63a5\u89e6\u9762\u79ef\u52a8\u6001\u8c03\u6574 Canvas \u7b7e\u540d\u6216\u7ed8\u56fe\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>\u6839\u636e\u538b\u529b\u6216\u63a5\u89e6\u9762\u79ef\u52a8\u6001\u8c03\u6574 canvas \u7b7e\u540d\u6216\u7ed8\u56fe\u7c97\u7ec6 \u5728\u79fb\u52a8\u8bbe\u5907\u4e0a\u4f7f\u7528 Canvas \u8fdb\u884c\u7b7e\u540d\u6216\u7ed8\u56fe\u65f6\uff0c\u9700\u8981\u6839\u636e\u7528\u6237\u65bd\u52a0\u7684\u538b\u529b\u6216\u63a5\u89e6\u9762\u79ef\u6765\u52a8\u6001\u8c03\u6574\u7ebf\u6761\u7c97\u7ec6\u3002\u4f8b\u5982\uff0c\u5f53\u7528\u6237\u7528\u529b\u6309\u538b\u65f6\uff0c\u7ebf\u6761\u5e94\u53d8\u7c97\uff0c\u800c\u8f7b\u6309\u65f6\u7ebf\u6761\u5e94\u53d8\u7ec6\u3002 \u5b9e\u73b0\u65b9\u6cd5 \u8981\u5728 Canvas \u4e2d\u5b9e\u73b0\u6839\u636e\u538b\u529b\u6216\u63a5\u89e6\u9762\u79ef\u6539\u53d8\u7ebf\u6761\u7c97\u7ec6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a \u5229\u7528 TouchEvent \u5c5e\u6027\uff1a TouchEvent \u5bf9\u8c61\u5177\u6709 touchRadialPressure \u6216 tangentialPressure \u5c5e\u6027\uff0c\u8868\u793a\u7528\u6237\u65bd\u52a0\u7684\u538b\u529b\u3002\u6839\u636e\u8fd9\u4e9b\u5c5e\u6027\u7684\u503c\uff0c\u53ef\u4ee5\u8c03\u6574\u753b\u7b14\u7c97\u7ec6\u3002 \u4f7f\u7528 getContext(&#8216;2d&#8217;)\uff1a getContext(&#8216;2d&#8217;) \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a 2D \u7ed8\u56fe\u4e0a\u4e0b\u6587\uff0c\u5176\u4e2d\u5305\u542b\u63a7\u5236\u7ebf\u5bbd\u7684\u65b9\u6cd5\uff0c\u5982 lineWidth\u3002 \u76d1\u542c touchstart \u548c touchmove \u4e8b\u4ef6\uff1a\u5728 Canvas \u5143\u7d20\u4e0a\u76d1\u542c touchstart \u548c touchmove \u4e8b\u4ef6\uff0c\u5728\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u4e2d\u83b7\u53d6\u538b\u529b\u503c\uff0c\u7136\u540e\u76f8\u5e94\u5730\u66f4\u65b0\u753b\u7b14\u7c97\u7ec6\u3002 \u793a\u4f8b\u4ee3\u7801 \u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528 TouchEvent \u5c5e\u6027\u548c getContext(&#8216;2d&#8217;) \u65b9\u6cd5\u5b9e\u73b0\u538b\u529b\u611f\u5e94\u7684 Canvas \u7ed8\u56fe\uff1a const canvas = document.getElementById(&#8216;canvas&#8217;); const context = canvas.getContext(&#8216;2d&#8217;); canvas.addEventListener(&#8216;touchstart&#8217;, (e) [&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-1418","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1418","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=1418"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1418\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}