{"id":3310,"date":"2024-11-10T10:54:00","date_gmt":"2024-11-10T02:54:00","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3310\/"},"modified":"2024-11-10T10:54:00","modified_gmt":"2024-11-10T02:54:00","slug":"js%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e8%bf%9e%e7%ba%bf","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3310\/","title":{"rendered":"js\u5982\u4f55\u5b9e\u73b0\u8fde\u7ebf"},"content":{"rendered":"<blockquote><p>\n  \u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 Canvas API \u5b9e\u73b0\u8fde\u7ebf\u7ed8\u5236\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u521b\u5efa Canvas \u5143\u7d20\u83b7\u53d6 Canvas \u4e0a\u4e0b\u6587\u8bbe\u7f6e\u7ebf\u6761\u5c5e\u6027 (\u989c\u8272\u3001\u5bbd\u5ea6)\u8d77\u59cb\u8def\u5f84\u79fb\u52a8\u5230\u8d77\u70b9\u8fde\u7ebf\u5230\u7ec8\u70b9\u7ed8\u5236\u8def\u5f84\u4f8b\u5982\uff0c\u7ed8\u5236\u4e00\u6761\u4ece (0, 0) \u5230 (100, 100) \u7684\u7ea2\u8272\u8fde\u7ebf\uff1aconst canvas = document.createElement(&#8216;canvas&#8217;); const ctx = canvas.getContext(&#8216;2d&#8217;); ctx.strokeStyle = &#8216;red&#8217;; ct\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110809282943140.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u5b9e\u73b0\u8fde\u7ebf\u63d2\u56fe\" alt=\"js\u5982\u4f55\u5b9e\u73b0\u8fde\u7ebf\u63d2\u56fe\" \/><\/p>\n<p><strong>JS \u5982\u4f55\u5b9e\u73b0\u8fde\u7ebf<\/strong><\/p>\n<p>\u5728 JS \u4e2d\uff0c\u4f7f\u7528 Canvas API \u53ef\u4ee5\u5b9e\u73b0\u8fde\u7ebf\u7684\u7ed8\u5236\u3002<\/p>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa Canvas \u5143\u7d20\uff1a<\/strong><\/p>\n<pre>const canvas = document.createElement('canvas');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<li>\n<p><strong>\u83b7\u53d6 Canvas \u4e0a\u4e0b\u6587\uff1a<\/strong><\/p>\n<pre>const ctx = canvas.getContext('2d');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<li>\n<p><strong>\u8bbe\u7f6e\u7ebf\u6761\u5c5e\u6027\uff1a<\/strong><\/p>\n<pre>ctx.strokeStyle = 'color';\nctx.lineWidth = number;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<li>\n<p><strong>\u8d77\u59cb\u8def\u5f84\uff1a<\/strong><\/p>\n<pre>ctx.beginPath();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<li>\n<p><strong>\u79fb\u52a8\u5230\u8d77\u70b9\uff1a<\/strong><\/p>\n<pre>ctx.moveTo(x, y);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<li>\n<p><strong>\u8fde\u7ebf\u5230\u7ec8\u70b9\uff1a<\/strong><\/p>\n<pre>ctx.lineTo(x, y);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<li>\n<p><strong>\u7ed8\u5236\u8def\u5f84\uff1a<\/strong><\/p>\n<pre>ctx.stroke();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236  <\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u5728 Canvas \u5143\u7d20\u4e0a\u7ed8\u5236\u4e00\u6761\u4ece (0, 0) \u5230 (100, 100) \u7684\u7ea2\u8272\u8fde\u7ebf\uff1a<\/p>\n<pre>const canvas = document.createElement('canvas');\nconst ctx = canvas.getContext('2d');\n\nctx.strokeStyle = 'red';\nctx.lineWidth = 2;\nctx.beginPath();\nctx.moveTo(0, 0);\nctx.lineTo(100, 100);\nctx.stroke();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u5750\u6807\u53c2\u6570\u5355\u4f4d\u662f\u50cf\u7d20\u3002<\/li>\n<li>\u591a\u4e2a\u8fde\u7ebf\u53ef\u4ee5\u8fde\u7eed\u7ed8\u5236\uff0c\u65e0\u9700\u91cd\u590d beginPath()\u3002<\/li>\n<li>\u53ef\u4ee5\u4f7f\u7528 ctx.closePath() \u95ed\u5408\u8def\u5f84\uff0c\u4ee5\u7ed8\u5236\u4e00\u4e2a\u586b\u5145\u7684\u56fe\u5f62\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u5b9e\u73b0\u8fde\u7ebf\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>\u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 Canvas API \u5b9e\u73b0\u8fde\u7ebf\u7ed8\u5236\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u521b\u5efa Canvas \u5143\u7d20\u83b7\u53d6 Canvas \u4e0a\u4e0b\u6587\u8bbe\u7f6e\u7ebf\u6761\u5c5e\u6027 (\u989c\u8272\u3001\u5bbd\u5ea6)\u8d77\u59cb\u8def\u5f84\u79fb\u52a8\u5230\u8d77\u70b9\u8fde\u7ebf\u5230\u7ec8\u70b9\u7ed8\u5236\u8def\u5f84\u4f8b\u5982\uff0c\u7ed8\u5236\u4e00\u6761\u4ece (0, 0) \u5230 (100, 100) \u7684\u7ea2\u8272\u8fde\u7ebf\uff1aconst canvas = document.createElement(&#8216;canvas&#8217;); const ctx = canvas.getContext(&#8216;2d&#8217;); ctx.strokeStyle = &#8216;red&#8217;; ct JS \u5982\u4f55\u5b9e\u73b0\u8fde\u7ebf \u5728 JS \u4e2d\uff0c\u4f7f\u7528 Canvas API \u53ef\u4ee5\u5b9e\u73b0\u8fde\u7ebf\u7684\u7ed8\u5236\u3002 \u6b65\u9aa4\uff1a \u521b\u5efa Canvas \u5143\u7d20\uff1a const canvas = document.createElement(&#8216;canvas&#8217;); \u767b\u5f55\u540e\u590d\u5236 \u83b7\u53d6 Canvas \u4e0a\u4e0b\u6587\uff1a const ctx = canvas.getContext(&#8216;2d&#8217;); \u767b\u5f55\u540e\u590d\u5236 \u8bbe\u7f6e\u7ebf\u6761\u5c5e\u6027\uff1a ctx.strokeStyle [&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-3310","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3310","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=3310"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3310\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}