/* 迷雾裁剪路径:把若干「容器像素圆」拼成一段 clip-path: path() 数据。 * * 为什么自己拼 path():clip-path: circle() 只表达得了一个圆,而专题的显示区是一组圆。 * 每个圆一段子路径、圆弧一律 sweep=0(**同绕向**)—— nonzero 填充规则下多段自然取并集, * 重叠处不会互相挖洞;如果两段绕向相反,重叠区会被抵消成一个洞。 * 用两段半圆弧拼整圆,而不是往 DOM 里插 ,是为了整串塞进一个 path(), * 不用额外维护 SVG 节点与它的坐标系。 * * 单独一个文件(而不是塞进 rond-map.js)是为了能直接跑 node 单测: * rond-map.js 是个大 IIFE,函数取不出来。 */ (function (global) { 'use strict'; // circles: [{x, y, r}],单位都是**地图容器像素**(与 clip-path 的坐标系一致)。 // 返回可直接赋给 style.clipPath 的字符串;没有圆时返回空串(调用方据此不改样式)。 function pathFor(circles) { if (!circles || !circles.length) return ''; var d = ''; for (var i = 0; i < circles.length; i++) { var c = circles[i]; var x = c.x, y = c.y; // 半径下限 1px:缩到极小时 r=0 会让路径退化(浏览器可能整段忽略) var r = Math.max(1, c.r); var rr = r.toFixed(1), w = (2 * r).toFixed(1); d += 'M' + (x - r).toFixed(1) + ' ' + y.toFixed(1) + 'a' + rr + ' ' + rr + ' 0 1 0 ' + w + ' 0' + 'a' + rr + ' ' + rr + ' 0 1 0 ' + (-2 * r).toFixed(1) + ' 0Z'; } return 'path("' + d + '")'; } var api = { pathFor: pathFor }; if (typeof module !== 'undefined' && module.exports) module.exports = api; global.rondFogClip = api; })(typeof window !== 'undefined' ? window : globalThis);