|
| 1 | +# Charming Path |
| 2 | + |
| 3 | +**Charming Path** builds SVG [`d`](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d) path strings from numbers—handy for `<path>`, [`getPointAtLength`](https://developer.mozilla.org/en-US/docs/Web/API/SVGGeometryElement/getPointAtLength), or APIs such as `layoutTextInPath` that expect a path description. Shapes are axis-aligned in user space (no rotation in the path itself). |
| 4 | + |
| 5 | +```js eval code=false |
| 6 | +(() => { |
| 7 | + const d = cm.pathCircle(100, 100, 80); |
| 8 | + return cm.svg`<svg ${{width: 200, height: 200}}> |
| 9 | + <path ${{fill: "none", stroke: "black", stroke_width: 2, d}} /> |
| 10 | + </svg>`; |
| 11 | +})(); |
| 12 | +``` |
| 13 | + |
| 14 | +```js |
| 15 | +const d = cm.pathCircle(100, 100, 80); |
| 16 | + |
| 17 | +cm.svg`<svg ${{width: 200, height: 200}}> |
| 18 | + <path ${{fill: "none", stroke: "black", stroke_width: 2, d}} /> |
| 19 | +</svg>`; |
| 20 | +``` |
| 21 | + |
| 22 | +## _cm_.pathLine(_x1_, _y1_, _x2_, _y2_) {#cm-path-line} |
| 23 | + |
| 24 | +Returns an **open** path: `M` to (_x1_, _y1_), `L` to (_x2_, _y2_). |
| 25 | + |
| 26 | +```js eval code=false |
| 27 | +(() => { |
| 28 | + const d = cm.pathLine(10, 90, 90, 10); |
| 29 | + return cm.svg`<svg ${{width: 100, height: 100, viewBox: "0 0 100 100"}}> |
| 30 | + <path ${{fill: "none", stroke: "steelblue", stroke_width: 2, d}} /> |
| 31 | + </svg>`; |
| 32 | +})(); |
| 33 | +``` |
| 34 | + |
| 35 | +```js |
| 36 | +const d = cm.pathLine(10, 90, 90, 10); |
| 37 | +``` |
| 38 | + |
| 39 | +## _cm_.pathCircle(_cx_, _cy_, _r_) {#cm-path-circle} |
| 40 | + |
| 41 | +Returns a **closed** circle centered at (_cx_, _cy_) with radius _r_, using two arc segments. |
| 42 | + |
| 43 | +```js eval code=false |
| 44 | +(() => { |
| 45 | + const d = cm.pathCircle(50, 50, 40); |
| 46 | + return cm.svg`<svg ${{width: 100, height: 100, viewBox: "0 0 100 100"}}> |
| 47 | + <path ${{fill: "none", stroke: "teal", stroke_width: 2, d}} /> |
| 48 | + </svg>`; |
| 49 | +})(); |
| 50 | +``` |
| 51 | + |
| 52 | +```js |
| 53 | +const d = cm.pathCircle(100, 100, 50); |
| 54 | +``` |
| 55 | + |
| 56 | +## _cm_.pathRect(_x_, _y_, _width_, _height_) {#cm-path-rect} |
| 57 | + |
| 58 | +Returns a **closed** rectangle with top-left (_x_, _y_) and size _width_ × _height_. |
| 59 | + |
| 60 | +```js eval code=false |
| 61 | +(() => { |
| 62 | + const d = cm.pathRect(15, 25, 70, 50); |
| 63 | + return cm.svg`<svg ${{width: 100, height: 100, viewBox: "0 0 100 100"}}> |
| 64 | + <path ${{fill: "none", stroke: "purple", stroke_width: 2, d}} /> |
| 65 | + </svg>`; |
| 66 | +})(); |
| 67 | +``` |
| 68 | + |
| 69 | +```js |
| 70 | +const d = cm.pathRect(0, 0, 100, 50); |
| 71 | +``` |
| 72 | + |
| 73 | +## _cm_.pathEllipse(_cx_, _cy_, _rx_, _ry_) {#cm-path-ellipse} |
| 74 | + |
| 75 | +Returns a **closed** axis-aligned ellipse centered at (_cx_, _cy_) with radii _rx_ and _ry_, using two elliptical arcs. |
| 76 | + |
| 77 | +```js eval code=false |
| 78 | +(() => { |
| 79 | + const d = cm.pathEllipse(50, 50, 42, 28); |
| 80 | + return cm.svg`<svg ${{width: 100, height: 100, viewBox: "0 0 100 100"}}> |
| 81 | + <path ${{fill: "none", stroke: "coral", stroke_width: 2, d}} /> |
| 82 | + </svg>`; |
| 83 | +})(); |
| 84 | +``` |
| 85 | + |
| 86 | +```js |
| 87 | +const d = cm.pathEllipse(100, 100, 60, 40); |
| 88 | +``` |
| 89 | + |
| 90 | +## _cm_.pathPolygon(_points_) {#cm-path-polygon} |
| 91 | + |
| 92 | +Returns a **closed** path through _points_, an array of `[x, y]` pairs. The first point is moved to with `M`; later points use `L`; the path ends with `Z`. |
| 93 | + |
| 94 | +```js eval code=false |
| 95 | +(() => { |
| 96 | + const d = cm.pathPolygon([ |
| 97 | + [50, 10], |
| 98 | + [90, 90], |
| 99 | + [10, 90], |
| 100 | + ]); |
| 101 | + return cm.svg`<svg ${{width: 100, height: 100, viewBox: "0 0 100 100"}}> |
| 102 | + <path ${{fill: "none", stroke: "darkgreen", stroke_width: 2, d}} /> |
| 103 | + </svg>`; |
| 104 | +})(); |
| 105 | +``` |
| 106 | + |
| 107 | +```js |
| 108 | +const d = cm.pathPolygon([ |
| 109 | + [0, 0], |
| 110 | + [100, 0], |
| 111 | + [50, 80], |
| 112 | +]); |
| 113 | +``` |
0 commit comments