{"id":11914,"date":"2025-10-27T15:43:08","date_gmt":"2025-10-27T07:43:08","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11914"},"modified":"2026-01-20T15:12:21","modified_gmt":"2026-01-20T07:12:21","slug":"js-canvas-createconicgradient","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2025\/10\/js-canvas-createconicgradient\/","title":{"rendered":"Canvas\u4e5f\u652f\u6301\u9525\u5f62\u6e10\u53d8\u4e86createConicGradient\u65b9\u6cd5"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11914\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11914<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u4f46\u9700\u8981\u4fdd\u7559\u539f\u4f5c\u8005\u3001\u51fa\u5904\u4ee5\u53ca\u6587\u4e2d\u94fe\u63a5\uff0cAI\u6293\u53d6\u4fdd\u7559\u539f\u6587\u5730\u5740\uff0c\u4efb\u4f55\u7f51\u7ad9\u5747\u53ef\u6458\u8981\u805a\u5408\uff0c\u5546\u7528\u8bf7\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202510\/2025-10-27_153911.jpeg\" width=\"351\" height=\"239\" alt=\"\u5c01\u9762\u56fe\" class=\"alignright clip size-medium\" \/><\/p>\n<h3>\u4e00\u3001\u5343\u547c\u4e07\u5524\u59cb\u51fa\u6765<\/h3>\n<p>\u597d\u6d88\u606f\uff0cCanvas\u4e5f\u652f\u6301\u9525\u5f62\u6e10\u53d8\u4e86\uff0c\u65b9\u6cd5\u540d\u79f0\u662f<code>createConicGradient()<\/code>\u65b9\u6cd5\u3002<\/p>\n<p>\u517c\u5bb9\u6027\u53c2\u89c1\u4e0b\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202510\/2025-10-27_142330.png\" width=\"577\" height=\"348\" alt=\"canvas\u9525\u5f62\u6e10\u53d8\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u6240\u6709\u73b0\u4ee3\u6d4f\u89c8\u5668\u90fd\u5df2\u7ecf\u652f\u6301\uff0c\u4e14\u5df2\u7ecf\u652f\u6301\u4e862\u5e74\u4e86\u3002<\/p>\n<h4>\u9700\u8981\u4f7f\u7528Canvas\u9525\u5f62\u6e10\u53d8\u7684\u573a\u666f<\/h4>\n<p>\u6700\u8fd1\u9047\u5230\u4e86\u4e2a\u9700\u6c42\uff0c\u5fc5\u987b\u4f7f\u7528Canvas\u5b9e\u73b0\u9525\u5f62\u6e10\u53d8\uff0c\u800c\u4e0d\u80fd\u662fCSS\u3002<\/p>\n<p>\u90a3\u5c31\u662f\u4e0b\u56fe\u6240\u793a\u7684\u997c\u56fe\u6548\u679c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202510\/2025-10-27_142618.png\" width=\"384\" height=\"253\" alt=\"\u997c\u56fe\u9700\u6c42\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u5982\u679c\u5355\u8bba\u5b9e\u73b0\uff0c\u65e0\u8bba\u662fCSS\u8fd8\u662fCanvas\u90fd\u53ef\u4ee5\uff0cCSS\u66f4\u4f73\uff0c\u56e0\u4e3a\u6210\u672c\u66f4\u4f4e\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u6ce8\u610f\u770b\u56fe\u7247\u7684\u5de6\u4e0a\u89d2\uff0c\u8fd8\u6709\u4e00\u4e2a\u4e0b\u8f7d\u4e3a\u56fe\u7247\u7684\u529f\u80fd\u3002<\/p>\n<p>html2canvas\u662f\u4e0d\u652f\u6301CSS\u9525\u5f62\u6e10\u53d8\u7684\uff0c\u4fdd\u5b58\u7684\u56fe\u7247\u662f\u7a7a\u767d\u3002<\/p>\n<p>\u6240\u4ee5\u9700\u8981Canvas\u5b9e\u73b0\uff0c\u8f6c\u6362\u4e3a\u56fe\u7247\uff0c\u8fd9\u6837\u5c31\u80fd\u622a\u56fe\u4fdd\u5b58\u4e86\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202510\/2025-10-27_143354.png\" width=\"421\" height=\"259\" alt=\"canvas\u5b9e\u73b0\u9525\u5f62\u6e10\u53d8\u6548\u679c\u56fe\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u4e5f\u662f\u5c31\u7528\u5230\u4e86\u8fd9\u4e2a<code>createConicGradient()<\/code>\u65b9\u6cd5\u6765\u7ed8\u5236\u997c\u56fe\u3002<\/p>\n<h3>\u4e8c\u3001createConicGradient\u8bed\u6cd5<\/h3>\n<p>\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>createConicGradient(startAngle, x, y)<\/pre>\n<p>\u8868\u793a\u521b\u5efa\u4e00\u4e2a\u9525\u5f62\u6e10\u53d8\u5f00\u59cb\u7684\u4f4d\u7f6e\u3002<\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li><code>startAngle<\/code>\u662f\u9525\u5f62\u7684\u5706\u5f27\u8d77\u59cb\u89d2\u5ea6\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u548cCSS\u4e0d\u540c\uff0c\u5728Canvas\u4e2d\uff0c\u89d2\u5ea60\u8868\u793a\u7684\u662f\u4e09\u70b9\u949f\u65b9\u5411\uff0c\u6709\u5c31\u662f\u6c34\u5e73\u671d\u53f3\uff0c\u4f46\u662fCSS\u662f12\u70b9\u949f\u65b9\u5411\uff0c\u5782\u76f4\u671d\u4e0a\u3002<\/li>\n<li><code>x, y<\/code>\u8868\u793a\u9525\u5f62\u6e10\u53d8\u7684\u4e2d\u5fc3\u70b9\u5750\u6807\u3002<\/li>\n<\/ul>\n<h4>\u6848\u4f8b<\/h4>\n<p>\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u6848\u4f8b\u6765\u4e86\u89e3\u4e0b<code>createConicGradient()<\/code>\u65b9\u6cd5\u662f\u5982\u4f55\u4f7f\u7528\u7684\u3002<\/p>\n<pre>&lt;canvas&gt;&lt;\/canvas&gt;<\/pre>\n<p>\u7ed8\u5236\u4ee3\u7801\u4e3a\uff1a<\/p>\n<pre>const canvas = document.querySelector(\"canvas\");\r\nconst ctx = canvas.getContext(\"2d\");\r\n\r\n\/\/ \u5c3a\u5bf8\r\nconst size = 300;\r\ncanvas.width = size;\r\ncanvas.height = size;\r\n\r\n\/\/ \u521b\u5efa\u9525\u5f62\u6e10\u53d8\r\nconst gradient = ctx.createConicGradient(-0.5 * Math.PI, size \/ 2, size \/ 2);\r\n\r\n\/\/ \u8272\u5e26\u989c\u8272\r\nconst arrColor = ['#10239E', '#2F54EB','#597EF7', '#85A5FF', '#D6E4FF'];\r\n\/\/ \u5404\u81ea\u5360\u636e\u7684\u767e\u5206\u6bd4\r\nconst arrPercent = [0.07, 0.13, 0.2, 0.27, 0.33];\r\n\r\n\/\/ \u6dfb\u52a0\u4e2d\u65ad\u70b9\r\nlet sumPercent = 0;\r\narrPercent.forEach((percent, index) =&gt; {\r\n  gradient.addColorStop(sumPercent, arrColor[index]);\r\n  sumPercent += percent;\r\n  gradient.addColorStop(sumPercent, arrColor[index]);\r\n});\r\n\r\n\/\/ \u8bbe\u7f6e\u6e10\u53d8\u4e3a\u586b\u5145\u6837\u5f0f\r\nctx.fillStyle = gradient;\r\n\/\/ \u7ed8\u5236\u5706\u5f62\r\nctx.ellipse(size \/ 2, size \/ 2, size \/ 2 - 30, size \/ 2 - 30, 0, 0, 2 * Math.PI);\r\n\/\/ \u586b\u5145\r\nctx.fill();<\/pre>\n<p>\u5728\u6d4f\u89c8\u5668\u4e2d\u7684\u5b9e\u65f6\u7ed8\u5236\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n<p><canvas class=\"canvas\"><\/canvas><\/p>\n<style>canvas{width: 150px; height: 150px;}<\/style>\n<p><script>\"use strict\";!function(){var t=document.querySelector(\"canvas\"),e=t.getContext(\"2d\");t.width=300,t.height=300;var o=e.createConicGradient(-.5*Math.PI,150,150),a=[\"#10239E\",\"#2F54EB\",\"#597EF7\",\"#85A5FF\",\"#D6E4FF\"],i=[.07,.13,.2,.27,.33],l=0;i.forEach(function(t,e){o.addColorStop(l,a[e]),l+=t,o.addColorStop(l,a[e])}),e.fillStyle=o,e.ellipse(150,150,120,120,0,0,2*Math.PI),e.fill()}();<\/script><\/p>\n<p>\u5b8c\u7f8e\uff01<\/p>\n<p>\u5728\u8fc7\u53bb\uff0c\u672c\u6587\u6240\u5c55\u793a\u7684\u997c\u56fe\u6548\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>arc()<\/code>\u6247\u5f62\u7ed8\u5236\u65b9\u6cd5\u6a21\u62df\uff0c\u975e\u672c\u6587\u91cd\u70b9\uff0c\u4e0d\u505a\u5c55\u5f00\u3002<\/p>\n<h3>\u4e09\u3001\u7ed3\u8bed<\/h3>\n<p>\u5982\u679c\u4f60\u8ba9AI\u5b9e\u73b0\u9525\u5f62\u6e10\u53d8\uff0c\u5927\u6982\u7387\u4f1a\u4f7f\u7528CSS <code>conic-gradient()<\/code>\u9525\u5f62\u6e10\u53d8\uff0c\u4f46\u662f\u4f60\u8ba9\u4ed6\u89e3\u51b3html2canvas\u65e0\u6cd5\u5bfc\u51fa\u56fe\u7247\u7684\u95ee\u9898\uff0cAI\u5c31\u4f1a\u626f\u4e1c\u626f\u897f\uff0c\u4e0d\u77e5\u9053\u8be5\u5982\u4f55\u662f\u597d\u4e86\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u65b0\u65f6\u4ee3\uff0c\u6211\u4eec\u5b66\u4e60\u524d\u7aef\uff0c\u53ef\u80fd\u5177\u4f53\u7684\u6280\u672f\u7ec6\u8282\u4e0d\u9700\u8981\u638c\u63e1\uff0c\u4f46\u662f\uff0c\u7ed3\u6784\u3001\u65b9\u5411\u3001\u7b56\u7565\u3001\u7ec6\u8282\u8fd8\u662f\u9700\u8981\u638c\u63e1\u7684\u3002<\/p>\n<p>\u6bd4\u65b9\u8bf4\u672c\u6587\u7684\u6848\u4f8b\uff0c\u4f60\u9700\u8981\u77e5\u9053\u4f7f\u7528Canvas\u6765\u7ed8\u5236\u6e10\u53d8\uff0c\u6307\u5bfcAI\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u4ed6\u5c31\u80fd\u641e\u5b9a\uff0c\u5426\u5219\uff0cAI\u5c31\u4f1a\u8ff7\u8def\u3002<\/p>\n<p>\u5176\u4e2d\u8fd9\u6837\u7684\u4f8b\u5b50\u5f88\u591a\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4e4b\u524d\u6709\u540c\u4e8b\u60f3\u8981\u5728\u624b\u673a\u7aef\u5b9e\u73b0\u5212\u8bcd\u4ea4\u4e92\u6548\u679c\uff0c\u95eeAI\uff0c\u7ed3\u679cAI\u5f04\u4e86\u4e00\u5927\u5806\u4ee3\u7801\uff0c\u4f7f\u7528\u9f20\u6807\u4e8b\u4ef6\u5b9e\u73b0\u7684\uff0c\u5f88\u5570\u55e6\u3002\u4e8e\u662f\u540c\u4e8b\u5c31\u95ee\u6211\uff0c\u6211\u5c31\u8bf4\u8bd5\u8bd5<code>selectionchange<\/code>\u4e8b\u4ef6\uff0c\u8fd9\u4e48\u4e00\u70b9\u62e8\uff0cAI\u7acb\u9a6c\u5c31\u7cca\u6d82\u72b6\u6001\u53d8\u6210\u4e86\u5927\u5e08\u3002<\/p>\n<p>\u8fd8\u6709\u6848\u4f8b\uff0c\u53ef\u7f16\u8f91\u8f93\u5165\u6846\u7c7b\u4f3c\u4e8eAT\u6210\u7ec4\u7684\u4ea4\u4e92\uff0cAI\u7684\u5b9e\u73b0\u4e5f\u662f\uff0c\u6d0b\u6d0b\u6d12\u6d12\uff0c\u4ee3\u7801\u591a\u5f97\u4e0d\u5f97\u4e86\uff0c\u611f\u89c9\u628a\u6574\u4e2a\u7f16\u8f91\u5668\u90fd\u5f04\u8fdb\u6765\u4e86\u3002<\/p>\n<p>\u5b9e\u9645\uff0c\u8d70DOM\u76d1\u63a7\u5355\u5411\u6570\u636e\u6d41\uff0c\u5c31\u767e\u6765\u884c\u4ee3\u7801\u7684\u4e8b\u60c5\u3002<\/p>\n<p>\u6240\u4ee5\u8bf4\uff0c\u5b66\u4e60\u65b0\u7279\u6027\u8fd8\u662f\u6709\u7528\u7684\u3002<\/p>\n<p>\u6211\u4e5f\u4e0d\u4f1a\u62c5\u5fc3\u81ea\u5df1\u88abAI\u53d6\u4ee3\uff0c\u53ea\u8981\u6301\u7eed\u5b66\u4e60\uff0c\u4e0d\u6392\u65a5\u65b0\u4e8b\u7269\uff0c\u6211\u53ea\u4f1a\u6bd4\u4e4b\u524d\u66f4\u52a0\u5177\u6709\u7ade\u4e89\u529b\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u53c8\u626f\u591a\u4e86\uff0c\u6700\u540e\uff0c\u6211\u5bb6\u4f8d\u59be\u6155\u6c9b\u7075\u538b\u9635\uff0c\u9053\u53cb\u8bf7\u8f6c\u53d1\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202510\/2025-10-27_152423.jpeg\" width=\"140\" height=\"117\" alt=\"\u6155\u6c9b\u7075 \u5e38\u670d \u7ea2\u8272\" class=\"alignnone size-medium\" \/><\/p>\n<p>\ud83d\ude09\ud83d\ude0a\ud83d\ude07<br \/>\n\ud83e\udd70\ud83d\ude0d\ud83d\ude18<\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11914\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11914<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202510\/2025-10-27_153911.jpeg\" width=\"160\" height=\"109\" alt=\"\u5c01\u9762\u56fe\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u9664\u4e86CSS\u3001HTML\u548cJS\u4e4b\u5916\uff0cCanvas\u8fd9\u95e8\u8bed\u8a00\u5176\u5b9e\u4e5f\u5728\u53d1\u5c55\uff0c\u8fd9\u4e0d\uff0c\u4e5f\u652f\u6301\u9525\u5f62\u6e10\u53d8\u4e86\uff0c\u6700\u8fd1\u67d0\u4e2a\u9700\u6c42\u6b63\u597d\u7528\u5230\u4e86\uff0c\u89e3\u51b3\u4e86CSS\u9525\u5f62\u6e10\u53d8\u65e0\u6cd5\u524d\u7aef\u622a\u56fe\u7684\u96be\u9898\uff0c\u4e0d\u59a8\u8fdb\u6765\u770b\u770b\u662f\u600e\u4e48\u56de\u4e8b\u5427\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[896],"tags":[53,1179,2089,1629,189,1608],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11914"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=11914"}],"version-history":[{"count":7,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11914\/revisions"}],"predecessor-version":[{"id":12063,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11914\/revisions\/12063"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=11914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=11914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=11914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}