|
| 1 | +import type { Bounds, ElementsRecord } from "./context/context"; |
| 2 | + |
| 3 | +type ElementBounds = { |
| 4 | + startX: number; |
| 5 | + startY: number; |
| 6 | + endX: number; |
| 7 | + endY: number; |
| 8 | +}; |
| 9 | + |
| 10 | +const M = (x: number, y: number) => `M ${x} ${y}`; |
| 11 | +const L = (x: number, y: number) => `L ${x} ${y}`; |
| 12 | +const arc = (toX: number, toY: number, radius: number) => |
| 13 | + `A ${radius},${radius} 0 0 0 ${toX},${toY}`; |
| 14 | +const z = "z"; |
| 15 | + |
| 16 | +const constructClipPath = (data: ElementsRecord[string], containerSize: Bounds): string => { |
| 17 | + const parentBounds = { |
| 18 | + startX: 0, |
| 19 | + startY: 0, |
| 20 | + endX: containerSize.width, |
| 21 | + endY: containerSize.height, |
| 22 | + }; |
| 23 | + |
| 24 | + switch (data.options?.mode) { |
| 25 | + case "circle": { |
| 26 | + const { |
| 27 | + bounds: { x, y, width, height }, |
| 28 | + options: { padding = 0 }, |
| 29 | + } = data; |
| 30 | + const radius = Math.max(width, height) / 2; |
| 31 | + return constructCircularPath( |
| 32 | + parentBounds, |
| 33 | + { cx: x + width / 2, cy: y + height / 2 }, |
| 34 | + radius + padding |
| 35 | + ); |
| 36 | + } |
| 37 | + case "rectangle": // Fallthrough |
| 38 | + default: { |
| 39 | + const padding = data.options?.padding ?? 0; |
| 40 | + const borderRadius = data.options?.borderRadius ?? 0; |
| 41 | + |
| 42 | + const startX = data.bounds.x - padding; |
| 43 | + const endX = startX + data.bounds.width + padding * 2; |
| 44 | + const startY = data.bounds.y - padding; |
| 45 | + const endY = startY + data.bounds.height + padding * 2; |
| 46 | + return constructRectangularPath( |
| 47 | + parentBounds, |
| 48 | + { startX, startY, endX, endY }, |
| 49 | + borderRadius |
| 50 | + ); |
| 51 | + } |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +const constructRectangularPath = ( |
| 56 | + parentBounds: ElementBounds, |
| 57 | + { startX, startY, endX, endY }: ElementBounds, |
| 58 | + borderRadius: number |
| 59 | +): string => { |
| 60 | + return [ |
| 61 | + M(parentBounds.startX, parentBounds.startY), |
| 62 | + L(parentBounds.startX, parentBounds.endY), |
| 63 | + L(parentBounds.endX, parentBounds.endY), |
| 64 | + L(parentBounds.endX, parentBounds.startY), |
| 65 | + z, |
| 66 | + M(startX, startY + borderRadius), |
| 67 | + L(startX, endY - borderRadius), |
| 68 | + arc(startX + borderRadius, endY, borderRadius), |
| 69 | + L(endX - borderRadius, endY), |
| 70 | + arc(endX, endY - borderRadius, borderRadius), |
| 71 | + L(endX, startY + borderRadius), |
| 72 | + arc(endX - borderRadius, startY, borderRadius), |
| 73 | + L(startX + borderRadius, startY), |
| 74 | + arc(startX, startY + borderRadius, borderRadius), |
| 75 | + ].join(" "); |
| 76 | +}; |
| 77 | + |
| 78 | +const constructCircularPath = ( |
| 79 | + parentBounds: ElementBounds, |
| 80 | + { cx, cy }: { cx: number; cy: number }, |
| 81 | + radius: number |
| 82 | +): string => { |
| 83 | + return [ |
| 84 | + M(parentBounds.startX, parentBounds.startY), |
| 85 | + L(parentBounds.startX, parentBounds.endY), |
| 86 | + L(parentBounds.endX, parentBounds.endY), |
| 87 | + L(parentBounds.endX, parentBounds.startY), |
| 88 | + z, |
| 89 | + M(cx, cy), |
| 90 | + `m ${-radius} 0`, |
| 91 | + `a ${radius},${radius} 0 1,0 ${radius * 2},0`, |
| 92 | + `a ${radius},${radius} 0 1,0 ${-radius * 2},0`, |
| 93 | + ].join(" "); |
| 94 | +}; |
| 95 | + |
| 96 | +export default constructClipPath; |
0 commit comments