{"id":27324,"date":"2024-11-24T13:32:39","date_gmt":"2024-11-24T05:32:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/27324\/"},"modified":"2024-11-24T13:32:39","modified_gmt":"2024-11-24T05:32:39","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8canvas-api%e5%ae%9e%e7%8e%b0%e5%9b%be%e7%89%87%e6%8c%89%e6%9b%b2%e7%ba%bf%e6%8b%89%e4%bc%b8%e5%b9%b6%e6%8e%92%e5%88%97%e5%b8%83%e5%b1%80%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/27324\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Canvas API\u5b9e\u73b0\u56fe\u7247\u6309\u66f2\u7ebf\u62c9\u4f38\u5e76\u6392\u5217\u5e03\u5c40\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173122939616305.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528Canvas API\u5b9e\u73b0\u56fe\u7247\u6309\u66f2\u7ebf\u62c9\u4f38\u5e76\u6392\u5217\u5e03\u5c40\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528Canvas API\u5b9e\u73b0\u56fe\u7247\u6309\u66f2\u7ebf\u62c9\u4f38\u5e76\u6392\u5217\u5e03\u5c40\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u56fe\u7247\u6309\u66f2\u7ebf\u62c9\u4f38\u5e76\u5e03\u5c40<\/strong><\/p>\n<p>\u60a8\u60f3\u4f7f\u7528  \u6216 html \u5bf9\u56fe\u7247\u8fdb\u884c\u6309\u66f2\u7ebf\u62c9\u4f38\u5e76\u6392\u5217\u5e03\u5c40\u3002css \u4e2d\u7684 transform \u5c5e\u6027\u63d0\u4f9b\u4e86 rotatey \u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u56fe\u7247\u65cb\u8f6c\uff0c\u4f46\u65e0\u6cd5\u5b9e\u73b0\u4e1d\u6ed1\u7684\u66f2\u7ebf\u6548\u679c\u3002\u5bf9\u4e8e\u4e2d\u95f4\u884c\uff0c\u60a8\u76ee\u524d\u6ca1\u6709\u601d\u8def\u3002<\/p>\n<p><strong>\u4f7f\u7528 canvas api \u5b9e\u73b0<\/strong><\/p>\n<p>\u60a8\u53ef\u80fd\u8003\u8651\u4f7f\u7528 canvas api \u4e2d\u7684 imagedata \u76f8\u5173\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u64cd\u4f5c\u56fe\u7247\u4e0a\u50cf\u7d20\u70b9\u7684\u989c\u8272\uff0c\u4ece\u800c\u5b9e\u73b0\u60f3\u8981\u7684\u66f2\u7ebf\u62c9\u4f38\u548c\u6392\u5217\u6548\u679c\u3002<\/p>\n<p><strong>\u793a\u4f8b\u6f14\u793a<\/strong><\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528canvas api\u5b9e\u73b0\u8be5\u6548\u679c\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<pre>&lt;canvas id=\"canvas\"&gt;&lt;\/canvas&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>const canvas = document.getElementById(\"canvas\");\nconst ctx = canvas.getContext(\"2d\");\n\n\/\/ \u52a0\u8f7d\u56fe\u7247\nconst image = new Image();\nimage.onload = function() {\n  \/\/ \u521b\u5efaImageData\u5bf9\u8c61\n  const imageData = ctx.createImageData(image.width, image.height);\n\n  \/\/ \u83b7\u53d6\u50cf\u7d20\u6570\u636e\n  ctx.drawImage(image, 0, 0);\n  const data = imageData.data;\n\n  \/\/ \u626d\u66f2\u50cf\u7d20\n  for (let y = 0; y &lt; image.height; y++) {\n    for (let x = 0; x &lt; image.width; x++) {\n      const index = (y * image.width + x) * 4;\n      const dx = x - image.width \/ 2;\n      const dy = y - image.height \/ 2;\n      const radius = Math.sqrt(dx * dx + dy * dy);\n      const angle = Math.atan2(dy, dx);\n      const curve = Math.sin(angle * radius \/ 100);\n      data[index] = data[index + 1] = data[index + 2] = curve * 255;\n    }\n  }\n\n  \/\/ \u66f4\u65b0ImageData\u5bf9\u8c61\n  ctx.putImageData(imageData, 0, 0);\n};\nimage.src = \"\u56fe\u7247\u8def\u5f84\";<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u793a\u4f8b\u4f1a\u521b\u5efa\u4e00\u4e2aimagedata\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528\u5faa\u73af\u4fee\u6539\u50cf\u7d20\u7684\u989c\u8272\u4ee5\u5b9e\u73b0\u626d\u66f2\u6548\u679c\u3002\u8c03\u6574&#8221;radius \/ 100&#8243;\u7684\u503c\u53ef\u4ee5\u6539\u53d8\u66f2\u7ebf\u7684\u5927\u5c0f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528Canvas API\u5b9e\u73b0\u56fe\u7247\u6309\u66f2\u7ebf\u62c9\u4f38\u5e76\u5e03\u5c40\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>\u56fe\u7247\u6309\u66f2\u7ebf\u62c9\u4f38\u5e76\u5e03\u5c40 \u60a8\u60f3\u4f7f\u7528 \u6216 html \u5bf9\u56fe\u7247\u8fdb\u884c\u6309\u66f2\u7ebf\u62c9\u4f38\u5e76\u6392\u5217\u5e03\u5c40\u3002css \u4e2d\u7684 transform \u5c5e\u6027\u63d0\u4f9b\u4e86 rotatey \u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u56fe\u7247\u65cb\u8f6c\uff0c\u4f46\u65e0\u6cd5\u5b9e\u73b0\u4e1d\u6ed1\u7684\u66f2\u7ebf\u6548\u679c\u3002\u5bf9\u4e8e\u4e2d\u95f4\u884c\uff0c\u60a8\u76ee\u524d\u6ca1\u6709\u601d\u8def\u3002 \u4f7f\u7528 canvas api \u5b9e\u73b0 \u60a8\u53ef\u80fd\u8003\u8651\u4f7f\u7528 canvas api \u4e2d\u7684 imagedata \u76f8\u5173\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u64cd\u4f5c\u56fe\u7247\u4e0a\u50cf\u7d20\u70b9\u7684\u989c\u8272\uff0c\u4ece\u800c\u5b9e\u73b0\u60f3\u8981\u7684\u66f2\u7ebf\u62c9\u4f38\u548c\u6392\u5217\u6548\u679c\u3002 \u793a\u4f8b\u6f14\u793a \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528canvas api\u5b9e\u73b0\u8be5\u6548\u679c\u7684\u7b80\u5355\u793a\u4f8b\uff1a &lt;canvas id=&#8221;canvas&#8221;&gt;&lt;\/canvas&gt; \u767b\u5f55\u540e\u590d\u5236 const canvas = document.getElementById(&#8220;canvas&#8221;); const ctx = canvas.getContext(&#8220;2d&#8221;); \/\/ \u52a0\u8f7d\u56fe\u7247 const image = new Image(); image.onload = function() { \/\/ \u521b\u5efaImageData\u5bf9\u8c61 const imageData = ctx.createImageData(image.width, image.height); \/\/ \u83b7\u53d6\u50cf\u7d20\u6570\u636e ctx.drawImage(image, [&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-27324","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27324","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=27324"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27324\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=27324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=27324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=27324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}