{"id":9443,"date":"2020-06-03T22:34:20","date_gmt":"2020-06-03T14:34:20","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9443"},"modified":"2020-06-04T11:36:09","modified_gmt":"2020-06-04T03:36:09","slug":"svg-area-calc","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2020\/06\/svg-area-calc\/","title":{"rendered":"SVG\u4efb\u610f\u56fe\u5f62path\u66f2\u7ebf\u8def\u5f84\u7684\u9762\u79ef\u8ba1\u7b97"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9443\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9443<\/a><br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202006-path-area.jpg';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202006\/path-area-cover.jpg\" width=\"421\" height=\"281\" alt=\"\u8def\u5f84\u9762\u79ef\u8ba1\u7b97\u591a\u8fb9\u5f62\u5360\u4f4d\u56fe\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<h3>\u4e00\u3001\u4f7f\u7528\u793a\u610f<\/h3>\n<p>\u8fd9\u91cc\u6709\u4e2a\u7b80\u6613demo\uff0c\u6d4b\u8bd5\u517c\u6f14\u793a\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202006\/path-area.html\" rel=\"noopener\" target=\"_blank\">\u8def\u5f84\u9762\u79ef\u548c\u5143\u7d20\u9762\u79ef\u8ba1\u7b97demo<\/a><\/p>\n<p>\u8f93\u5165\u6846\u5185\u7c98\u8d34\u4efb\u610f\u7684\u8def\u5f84\uff0c\u7136\u540e\u70b9\u51fb\u7b2c\u4e00\u4e2a\u6309\u94ae\uff0c\u5c31\u53ef\u4ee5\u663e\u793a\u8fd9\u4e2a\u8def\u5f84\u6240\u5360\u636e\u7684\u9762\u79ef\u4e86\u3002<\/p>\n<p>\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202006\/2020-06-03_212611.png\" width=\"363\" height=\"512\" alt=\"\u8def\u5f84\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u5982\u679c\u627e\u4e0d\u5230\u56fe\u6807\u8def\u5f84\u6d4b\u8bd5\uff0c\u53ef\u4ee5\u6233\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/sp\/icon\/\" rel=\"noopener\" target=\"_blank\">https:\/\/www.zhangxinxu.com\/sp\/icon\/<\/a><\/p>\n<p>\u4e5f\u53ef\u4ee5\u8ba1\u7b97<code>&lt;circle&gt;<\/code>\uff0c<code>&lt;ellipse&gt;<\/code>\uff0c<code>&lt;rect&gt;<\/code>\uff0c<code>&lt;line&gt;<\/code>\u5143\u7d20\u7684\u9762\u79ef\u3002<\/p>\n<p>\u4f8b\u5982demo\u9875\u9762\u540e\u9762\u5706\u7684\u9762\u79ef\u8ba1\u7b97\u7ed3\u679c\u793a\u610f\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202006\/2020-06-03_213135.png\" width=\"518\" height=\"384\" alt=\"\u5706\u5f62\u7684\u9762\u79ef\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u4e8c\u3001\u5982\u4f55\u4f7f\u7528<\/h3>\n<p>\u76f8\u5173\u7684\u8ba1\u7b97\u65b9\u6cd5\u6211\u5df2\u7ecf\u5c01\u88c5\u6210\u72ec\u7acb\u7684\u65b9\u6cd5\u4e86\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u8981\u60f3\u4f7f\u7528\uff0c\u53ea\u9700\u8981\u7b80\u5355\u4e24\u6b65\uff1a<\/p>\n<ol>\n<li>\u5f15\u7528JS\u6587\u4ef6\u3002\n<pre>&lt;script src=\".\/path-area.js\"&gt;&lt;\/script&gt;<\/pre>\n<p>\u4f60\u53ef\u4ee5<a href=\"https:\/\/www.zhangxinxu.com\/study\/202006\/path-area.js\" rel=\"noopener\" target=\"_blank\">\u70b9\u51fb\u8fd9\u91cc\u4e0b\u8f7dpath-area.js<\/a><\/p>\n<p>MIT\u534f\u8bae\uff0c\u53ef\u968f\u610f\u5546\u7528\uff0c\u4f46\u9700\u8981\u4fdd\u7559\u4f5c\u8005\u4fe1\u606f\u548c\u6587\u6863\u51fa\u5904\u4fe1\u606f\u3002<\/p>\n<\/li>\n<li>\u6267\u884c\u8ba1\u7b97\u65b9\u6cd5\u3002\n<pre>let area = getPathArea(strPathOrNode, points);<\/pre>\n<p>\u5176\u4e2d\uff0c<code>strPathOrNode<\/code>\u662f\u5fc5\u9700\u53c2\u6570\uff0c\u53ef\u4ee5\u662f\u5b57\u7b26\u4e32\uff0c\u6216\u8005\u662fDOM\u5bf9\u8c61\uff0c\u8868\u793a\u9700\u8981\u8ba1\u7b97\u9762\u79ef\u7684\u8def\u5f84\u6216\u5143\u7d20\uff1b<code>points<\/code>\u662f\u53ef\u9009\u53c2\u6570\uff0c\u8868\u793a\u9762\u79ef\u7684\u7cbe\u5ea6\uff0c\u9ed8\u8ba4\u662f100\uff0c\u503c\u8d8a\u5927\u7cbe\u5ea6\u8d8a\u9ad8\uff0c\u901a\u5e38100\u7684\u7cbe\u5ea6\u8db3\u591f\u4e86\uff0c\u9664\u975e\u8def\u5f84\u6216\u5143\u7d20\u7684\u5c3a\u5bf8\u4e0a\u5343\u4e0a\u4e07\u8fd9\u79cd\u3002<\/p>\n<p>\u8fd4\u56de\u503c<code>area<\/code>\u5c31\u662f\u8ba1\u7b97\u7684\u9762\u79ef\u5927\u5c0f\u3002\n<\/li>\n<\/ol>\n<h3>\u4e09\u3001\u5b9e\u73b0\u539f\u7406<\/h3>\n<p>\u66f2\u7ebf\u7684\u9762\u79ef\u4e0d\u592a\u597d\u8ba1\u7b97\uff0c\u8fd9\u91cc\u662f\u628a\u66f2\u7ebf\u8f6c\u6362\u6210\u591a\u8fb9\u5f62\uff0c\u7136\u540e\u518d\u53bb\u8ba1\u7b97\u9762\u79ef\u7684\u3002<\/p>\n<p>\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202006\/2020-06-03_215647.png\" width=\"706\" height=\"276\" alt=\"\u66f2\u7ebf\u53d8\u6210\u591a\u8fb9\u5f62\u9762\u79ef\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u53ef\u89c1\uff0c\u8f6c\u6362\u7684\u591a\u8fb9\u5f62\u7684\u70b9\u8d8a\u591a\uff0c\u6700\u7ec8\u8ba1\u7b97\u7684\u9762\u79ef\u4e5f\u5c31\u6708\u63a5\u8fd1\uff0c\u70b9\u7684\u6570\u91cf\u591a\u5c11\u5c31\u662f\u53c2\u6570<code>points<\/code>\u8bbe\u7f6e\u7684\u3002<\/p>\n<p>\u8def\u5f84\u8f6c\u591a\u8fb9\u5f62\u7684\u65b9\u6cd5\u5982\u4e0b\uff0c\u4f7f\u7528\u4e86SVG\u539f\u751f\u7684<code>getTotalLength()<\/code>\u65b9\u6cd5\u548c<code>getPointAtLength()<\/code>\u65b9\u6cd5\uff1a<\/p>\n<pre>function pathToPolygon (elePath, num) {\r\n    num = num || 100;\r\n\r\n    var len = elePath.getTotalLength();\r\n    var points = [];\r\n\r\n    for (var i=0; i &lt; num; i++) {\r\n        var pt = elePath.getPointAtLength(i * len \/ (num - 1));\r\n        points.push([pt.x, pt.y]);\r\n    }\r\n\r\n    return points;\r\n};<\/pre>\n<hr>\n<p>\u7136\u800c\uff0c\u975e<code>&lt;path&gt;<\/code>\u5143\u7d20\u662f\u6ca1\u6709\u8def\u5f84\u503c\u7684\uff0c\u4f8b\u5982<code>&lt;circle&gt;<\/code>\uff0c<code>&lt;ellipse&gt;<\/code>\uff0c<code>&lt;rect&gt;<\/code>\uff0c<code>&lt;line&gt;<\/code>\u7b49\u5185\u7f6e\u5143\u7d20\u7684\u6548\u679c\u90fd\u662f\u7531\u5404\u81ea\u8bed\u6cd5\u8bbe\u7f6e\u7684\uff0c\u600e\u4e48\u529e\u5462\uff1f<\/p>\n<p>\u6240\u4ee5\uff0c\u9700\u8981\u4e00\u4e2a\u4efb\u610fSVG\u5143\u7d20\u8f6c\u6362\u6210<code>&lt;path&gt;<\/code>\u5143\u7d20\u8def\u5f84\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u8fd9\u91cc\u5c31\u53c2\u8003\u4e86convertPath\u8fd9\u4e2a\u9879\u76ee\uff1a<a href=\"https:\/\/github.com\/convertSvg\/convertPath\" rel=\"noopener\" target=\"_blank\">https:\/\/github.com\/convertSvg\/convertPath<\/a><\/p>\n<p>\u4e0d\u8fc7\uff0c\u4e0d\u77e5\u9053\u662f\u539f\u9879\u76ee\u4f7f\u7528\u7684\u4eba\u4e0d\u591a\u8fd8\u662f\u4ec0\u4e48\u5176\u4ed6\u539f\u56e0\uff0c\u5c45\u7136\u6709\u51e0\u5904\u5f88\u660e\u663e\u7684bug\uff0c\u7136\u540epath-area.js\u5df2\u7ecf\u505a\u4e86\u4fee\u590d\uff0c\u5927\u5bb6\u53ef\u4ee5\u653e\u5fc3\u4f7f\u7528\u3002<\/p>\n<h3>\u56db\u3001\u603b\u7ed3\u548c\u5176\u4ed6\u8bf4\u660e<\/h3>\n<p>\u672c\u6587\u6240\u63d0\u4f9b\u7684\u9762\u79ef\u8ba1\u7b97\u65b9\u6cd5\uff0c\u5e76\u4e0d\u662f\u5b8c\u5168\u7cbe\u786e\u7684\u9762\u79ef\u8ba1\u7b97\uff0c\u4f46\u662f\uff0c\u5df2\u7ecf\u975e\u5e38\u63a5\u8fd1\u4e86\uff0c\u6839\u636e\u6211\u81ea\u5df1\u6d4b\u8bd5\uff0c\u901a\u5e38\u7684SVG\u56fe\u5f62\u9762\u79ef\u8bef\u5dee\u5728\u51e0\u50cf\u7d20\u4ee5\u5185\u3002<\/p>\n<p>\u8db3\u77e3\u3002<\/p>\n<p>\u7136\u540e\u6709\u4e9b\u56fe\u5f62\uff0c\u4f8b\u5982\u76f4\u7ebf\u5916\u52a0\u7c97\u7c97\u7684\u63cf\u8fb9\uff0c\u8fd9\u4e2a\u662f\u6ca1\u6709\u9762\u79ef\u7684\u3002<\/p>\n<p>\u7136\u540e\uff0c\u672c\u6587\u7684\u9762\u79ef\u8ba1\u7b97\u4e0d\u652f\u6301\u591a\u4e2a\u5143\u7d20\u7684\u9762\u79ef\u91cd\u5408\u8ba1\u7b97\u3002\u8bf7\u5148\u4f7f\u7528\u5176\u4ed6\u5de5\u5177\u8fdb\u884c\u5143\u7d20\u6216\u8def\u5f84\u7684\u5408\u5e76\uff0c\u4f8b\u5982<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2016\/02\/svg-compress-tool-svgo-experience\/\">SVGO\u538b\u7f29\u5de5\u5177<\/a>\u3002<\/p>\n<p>\u597d\uff0c\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u5185\u5bb9\uff0c\u4e3b\u8981\u5c31\u662f\u514d\u8d39\u7ed9\u5927\u5bb6\u63d0\u4f9b\u4e00\u4e2a\u5feb\u901f\u8ba1\u7b97SVG\u56fe\u5f62\u9762\u79ef\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u5982\u679c\u4f60\u89c9\u5f97\u672c\u6587\u5185\u5bb9\u89e3\u51b3\u4e86\u4f60\u7684\u5927\u5fd9\uff0c\u6b22\u8fce\u4f7f\u7528\u5fae\u4fe1\u7ed9\u6211<span class=\"hidden\">\uff08\u5f20\u946b\u65ed\uff09<\/span>\u6253\u8d4f\uff0c2\u5143\u5373\u53ef\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202006\/pay-me-2.png\" width=\"360\" height=\"383\" alt=\"\u6253\u8d4f\u5f20\u946b\u65ed2\u5143\uff0c\u8c22\u8c22\u4f60\" class=\"aligncenter size-medium\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/emtion\/emoji\/1f618.svg\" width=\"20\" height=\"20\" align=\"absmiddle\"><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u6536\u85cf\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u4e14\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9443\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9443<\/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\/202006\/path-area-cover.jpg\" width=\"160\" height=\"107\" alt=\"\u8def\u5f84\u9762\u79ef\u8ba1\u7b97\u591a\u8fb9\u5f62\u5360\u4f4d\u56fe\" class=\"alignright  size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u6211\u5199\u4e86\u4e00\u6bb5JS\uff0c\u7528\u6765\u8ba1\u7b97SVG\u4efb\u610f\u56fe\u5f62\u5143\u7d20\uff0c\u6216\u8005path\u5b57\u7b26\u4e32\u8def\u5f84\u5bf9\u5e94\u7684\u9762\u79ef\u5927\u5c0f\uff0c\u76f4\u63a5\u590d\u5236\u7c98\u8d34\u5c31\u53ef\u4ee5\u7528\uff0c\u5e0c\u671b\u53ef\u4ee5\u89e3\u51b3\u4f60\u7684\u5f00\u53d1\u9700\u6c42\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[895],"tags":[860,922,986,642,859,919],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9443"}],"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=9443"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9443\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=9443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=9443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=9443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}