{"id":27282,"date":"2024-11-24T08:56:08","date_gmt":"2024-11-24T00:56:08","guid":{"rendered":"https:\/\/fwq.ai\/blog\/27282\/"},"modified":"2024-11-24T08:56:08","modified_gmt":"2024-11-24T00:56:08","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-canvas-%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\/27282\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Canvas \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\/173122345315337.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 Canvas \u5b9e\u73b0\u56fe\u7247\u6309\u66f2\u7ebf\u62c9\u4f38\u5e76\u6392\u5217\u5e03\u5c40\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 Canvas \u5b9e\u73b0\u56fe\u7247\u6309\u66f2\u7ebf\u62c9\u4f38\u5e76\u6392\u5217\u5e03\u5c40\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5b9e\u73b0\u56fe\u7247\u6309\u66f2\u7ebf\u62c9\u4f38\u5e76\u5e03\u5c40\uff1f<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5982\u4f55\u5728 html \u548c  \u4e2d\u5b9e\u73b0\u5982\u8bbe\u8ba1\u56fe\u6240\u793a\u7684\u56fe\u7247\u6309\u66f2\u7ebf\u62c9\u4f38\u5e76\u6392\u5217\u5e03\u5c40\uff1f\u76ee\u524d\u6ca1\u6709\u4efb\u4f55\u601d\u8def\uff0ccss3\uff1atransform \u65e0\u6cd5\u5b9e\u73b0\u4e1d\u6ed1\u66f2\u7ebf\u4e14\u4e2d\u95f4\u884c\u4e0d\u6e05\u6670\u3002\u5bfb\u6c42\u524d\u7aef\u4e13\u5bb6\u63d0\u793a\u6216\u89e3\u7b54\uff0c\u662f\u5426\u53ef\u4ee5\u4f7f\u7528 canvas \u5b9e\u73b0\uff1f<\/p>\n<p><strong>\u89e3\u7b54\uff1a<\/strong><\/p>\n<p>\u8981\u5b9e\u73b0\u56fe\u7247\u6309\u66f2\u7ebf\u62c9\u4f38\u5e76\u6392\u5217\u5e03\u5c40\uff0c\u53ef\u4ee5\u4f7f\u7528 canvas api \u4e2d\u7684 imagedata \u76f8\u5173\u65b9\u6cd5\u6765\u63a7\u5236\u56fe\u7247\u50cf\u7d20\u70b9\u7684\u989c\u8272\u3002<\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>const canvas = document.querySelector('canvas');\nconst ctx = canvas.getContext('2d');\n\n\/\/ \u52a0\u8f7d\u56fe\u7247\nconst image = new Image();\nimage.onload = function() {\n  \/\/ \u83b7\u53d6\u56fe\u7247\u6570\u636e\n  const imageData = ctx.getImageData(0, 0, image.width, image.height);\n\n  \/\/ \u5faa\u73af\u6bcf\u4e2a\u50cf\u7d20\u70b9\n  for (let i = 0; i &lt; imageData.data.length; i += 4) {\n    \/\/ \u8ba1\u7b97\u50cf\u7d20\u70b9\u7684\u5750\u6807\n    const x = i \/ 4 % image.width;\n    const y = Math.floor(i \/ 4 \/ image.width);\n\n    \/\/ \u6839\u636e\u66f2\u7ebf\u51fd\u6570\u8c03\u6574\u50cf\u7d20\u70b9\u7684\u989c\u8272\n    const curveX = x + Math.sin(y \/ 10) * 10;\n    const curveY = y + Math.cos(x \/ 10) * 10;\n\n    \/\/ \u8bbe\u7f6e\u65b0\u7684\u50cf\u7d20\u70b9\u989c\u8272\n    const pixelIndex = curveY * image.width * 4 + curveX * 4;\n    imageData.data[i] = imageData.data[pixelIndex];\n    imageData.data[i + 1] = imageData.data[pixelIndex + 1];\n    imageData.data[i + 2] = imageData.data[pixelIndex + 2];\n    imageData.data[i + 3] = imageData.data[pixelIndex + 3];\n  }\n\n  \/\/ \u5c06\u4fee\u6539\u540e\u7684\u50cf\u7d20\u70b9\u6570\u636e\u653e\u56de\u753b\u5e03\n  ctx.putImageData(imageData, 0, 0);\n};\nimage.src = 'image.jpg';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528 Canvas \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>\u5982\u4f55\u5b9e\u73b0\u56fe\u7247\u6309\u66f2\u7ebf\u62c9\u4f38\u5e76\u5e03\u5c40\uff1f \u95ee\u9898\uff1a \u5982\u4f55\u5728 html \u548c \u4e2d\u5b9e\u73b0\u5982\u8bbe\u8ba1\u56fe\u6240\u793a\u7684\u56fe\u7247\u6309\u66f2\u7ebf\u62c9\u4f38\u5e76\u6392\u5217\u5e03\u5c40\uff1f\u76ee\u524d\u6ca1\u6709\u4efb\u4f55\u601d\u8def\uff0ccss3\uff1atransform \u65e0\u6cd5\u5b9e\u73b0\u4e1d\u6ed1\u66f2\u7ebf\u4e14\u4e2d\u95f4\u884c\u4e0d\u6e05\u6670\u3002\u5bfb\u6c42\u524d\u7aef\u4e13\u5bb6\u63d0\u793a\u6216\u89e3\u7b54\uff0c\u662f\u5426\u53ef\u4ee5\u4f7f\u7528 canvas \u5b9e\u73b0\uff1f \u89e3\u7b54\uff1a \u8981\u5b9e\u73b0\u56fe\u7247\u6309\u66f2\u7ebf\u62c9\u4f38\u5e76\u6392\u5217\u5e03\u5c40\uff0c\u53ef\u4ee5\u4f7f\u7528 canvas api \u4e2d\u7684 imagedata \u76f8\u5173\u65b9\u6cd5\u6765\u63a7\u5236\u56fe\u7247\u50cf\u7d20\u70b9\u7684\u989c\u8272\u3002 \u793a\u4f8b\u4ee3\u7801\uff1a const canvas = document.querySelector(&#8216;canvas&#8217;); const ctx = canvas.getContext(&#8216;2d&#8217;); \/\/ \u52a0\u8f7d\u56fe\u7247 const image = new Image(); image.onload = function() { \/\/ \u83b7\u53d6\u56fe\u7247\u6570\u636e const imageData = ctx.getImageData(0, 0, image.width, image.height); \/\/ \u5faa\u73af\u6bcf\u4e2a\u50cf\u7d20\u70b9 for (let i = 0; i &lt; imageData.data.length; [&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-27282","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27282","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=27282"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27282\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=27282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=27282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=27282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}