# packages/core/src/lib/geometry/path-warp.ts
This is the source snapshot used to build these API details. [View this revision on GitHub](https://github.com/benlesh/pibbl/blob/272a94aaf62e0bd6ad8726a4c607a76a9ec44ca1/packages/core/src/lib/geometry/path-warp.ts#L8).

[Back to reference](/reference/functions/path-transforms/)

<pre class="api-source"><code><span id="L1"><a href="#L1" aria-label="Line 1">1</a> import { arcPoint } from './path-arc.js';</span>
<span id="L2"><a href="#L2" aria-label="Line 2">2</a> import { PathGeometry } from './path-geometry.js';</span>
<span id="L3"><a href="#L3" aria-label="Line 3">3</a> import { emitSpan, endpoint, product, type WarpPoint, type WarpSpan, type WarpWork } from './path-warp-bezier.js';</span>
<span id="L4"><a href="#L4" aria-label="Line 4">4</a> </span>
<span id="L5"><a href="#L5" aria-label="Line 5">5</a> export interface PathWarpPoint { readonly x: number; readonly y: number }</span>
<span id="L6"><a href="#L6" aria-label="Line 6">6</a> export interface PathWarpRect { readonly x: number; readonly y: number; readonly width: number; readonly height: number }</span>
<span id="L7"><a href="#L7" aria-label="Line 7">7</a> export interface PathWarpCorners {</span>
<span id="L8"><a href="#L8" aria-label="Line 8">8</a>   readonly topLeft: PathWarpPoint;</span>
<span id="L9"><a href="#L9" aria-label="Line 9">9</a>   readonly topRight: PathWarpPoint;</span>
<span id="L10"><a href="#L10" aria-label="Line 10">10</a>   readonly bottomLeft: PathWarpPoint;</span>
<span id="L11"><a href="#L11" aria-label="Line 11">11</a>   readonly bottomRight: PathWarpPoint;</span>
<span id="L12"><a href="#L12" aria-label="Line 12">12</a> }</span>
<span id="L13"><a href="#L13" aria-label="Line 13">13</a> export type PathWarpMode = 'bilinear' | 'perspective';</span>
<span id="L14"><a href="#L14" aria-label="Line 14">14</a> export interface WarpPathOptions {</span>
<span id="L15"><a href="#L15" aria-label="Line 15">15</a>   readonly source: PathWarpRect;</span>
<span id="L16"><a href="#L16" aria-label="Line 16">16</a>   readonly destination: PathWarpCorners;</span>
<span id="L17"><a href="#L17" aria-label="Line 17">17</a>   readonly mode?: PathWarpMode;</span>
<span id="L18"><a href="#L18" aria-label="Line 18">18</a>   readonly tolerance: number;</span>
<span id="L19"><a href="#L19" aria-label="Line 19">19</a>   readonly maxSegments?: number;</span>
<span id="L20"><a href="#L20" aria-label="Line 20">20</a> }</span>
<span id="L21"><a href="#L21" aria-label="Line 21">21</a> </span>
<span id="L22"><a href="#L22" aria-label="Line 22">22</a> function number(value: unknown, name: string): number {</span>
<span id="L23"><a href="#L23" aria-label="Line 23">23</a>   if (typeof value !== 'number') throw new TypeError(`warpPath ${name} must be a number.`);</span>
<span id="L24"><a href="#L24" aria-label="Line 24">24</a>   if (!Number.isFinite(value)) throw new RangeError(`warpPath ${name} must be finite.`);</span>
<span id="L25"><a href="#L25" aria-label="Line 25">25</a>   return value;</span>
<span id="L26"><a href="#L26" aria-label="Line 26">26</a> }</span>
<span id="L27"><a href="#L27" aria-label="Line 27">27</a> function point(value: PathWarpPoint | undefined, name: string): WarpPoint {</span>
<span id="L28"><a href="#L28" aria-label="Line 28">28</a>   return [number(value?.x, `${name}.x`), number(value?.y, `${name}.y`)];</span>
<span id="L29"><a href="#L29" aria-label="Line 29">29</a> }</span>
<span id="L30"><a href="#L30" aria-label="Line 30">30</a> function polynomial(points: readonly WarpPoint[]): WarpSpan {</span>
<span id="L31"><a href="#L31" aria-label="Line 31">31</a>   return { x: points.map(p =&gt; p[0]), y: points.map(p =&gt; p[1]), w: points.map(() =&gt; 1) };</span>
<span id="L32"><a href="#L32" aria-label="Line 32">32</a> }</span>
<span id="L33"><a href="#L33" aria-label="Line 33">33</a> </span>
<span id="L34"><a href="#L34" aria-label="Line 34">34</a> /** Normalize destination coordinates before solving the unit-square homography. */</span>
<span id="L35"><a href="#L35" aria-label="Line 35">35</a> function perspective(corners: readonly WarpPoint[]): (span: WarpSpan) =&gt; WarpSpan {</span>
<span id="L36"><a href="#L36" aria-label="Line 36">36</a>   const scale = Math.max(...corners.flatMap(p =&gt; [Math.abs(p[0]), Math.abs(p[1])]));</span>
<span id="L37"><a href="#L37" aria-label="Line 37">37</a>   if (scale === 0) throw new RangeError('Perspective warp requires a strictly convex quadrilateral.');</span>
<span id="L38"><a href="#L38" aria-label="Line 38">38</a>   const normalized = corners.map(p =&gt; [p[0] / scale, p[1] / scale] as const);</span>
<span id="L39"><a href="#L39" aria-label="Line 39">39</a>   const origin = normalized[0];</span>
<span id="L40"><a href="#L40" aria-label="Line 40">40</a>   const extent = Math.max(...normalized.flatMap(p =&gt; [Math.abs(p[0] - origin[0]), Math.abs(p[1] - origin[1])]));</span>
<span id="L41"><a href="#L41" aria-label="Line 41">41</a>   if (extent === 0) throw new RangeError('Perspective destination is degenerate.');</span>
<span id="L42"><a href="#L42" aria-label="Line 42">42</a>   const q = normalized.map(p =&gt; [(p[0] - origin[0]) / extent, (p[1] - origin[1]) / extent] as const);</span>
<span id="L43"><a href="#L43" aria-label="Line 43">43</a>   const perimeter = [q[0], q[1], q[3], q[2]];</span>
<span id="L44"><a href="#L44" aria-label="Line 44">44</a>   let orientation = 0;</span>
<span id="L45"><a href="#L45" aria-label="Line 45">45</a>   for (let i = 0; i &lt; 4; i++) {</span>
<span id="L46"><a href="#L46" aria-label="Line 46">46</a>     const a = perimeter[i], b = perimeter[(i + 1) % 4], c = perimeter[(i + 2) % 4];</span>
<span id="L47"><a href="#L47" aria-label="Line 47">47</a>     const p = (b[0] - a[0]) * (c[1] - b[1]), r = (b[1] - a[1]) * (c[0] - b[0]);</span>
<span id="L48"><a href="#L48" aria-label="Line 48">48</a>     const cross = p - r;</span>
<span id="L49"><a href="#L49" aria-label="Line 49">49</a>     if (Math.abs(cross) &lt;= 32 * Number.EPSILON * (Math.abs(p) + Math.abs(r)) ||</span>
<span id="L50"><a href="#L50" aria-label="Line 50">50</a>         (orientation !== 0 &amp;&amp; Math.sign(cross) !== orientation)) {</span>
<span id="L51"><a href="#L51" aria-label="Line 51">51</a>       throw new RangeError('Perspective warp requires a strictly convex, nondegenerate quadrilateral.');</span>
<span id="L52"><a href="#L52" aria-label="Line 52">52</a>     }</span>
<span id="L53"><a href="#L53" aria-label="Line 53">53</a>     orientation = Math.sign(cross);</span>
<span id="L54"><a href="#L54" aria-label="Line 54">54</a>   }</span>
<span id="L55"><a href="#L55" aria-label="Line 55">55</a>   const [a, b, c, d] = q;</span>
<span id="L56"><a href="#L56" aria-label="Line 56">56</a>   const dx1 = b[0] - d[0], dx2 = c[0] - d[0], dx3 = a[0] - b[0] - c[0] + d[0];</span>
<span id="L57"><a href="#L57" aria-label="Line 57">57</a>   const dy1 = b[1] - d[1], dy2 = c[1] - d[1], dy3 = a[1] - b[1] - c[1] + d[1];</span>
<span id="L58"><a href="#L58" aria-label="Line 58">58</a>   const determinant = dx1 * dy2 - dx2 * dy1;</span>
<span id="L59"><a href="#L59" aria-label="Line 59">59</a>   if (Math.abs(determinant) &lt;= 32 * Number.EPSILON * (Math.abs(dx1 * dy2) + Math.abs(dx2 * dy1))) {</span>
<span id="L60"><a href="#L60" aria-label="Line 60">60</a>     throw new RangeError('Perspective mapping is numerically singular.');</span>
<span id="L61"><a href="#L61" aria-label="Line 61">61</a>   }</span>
<span id="L62"><a href="#L62" aria-label="Line 62">62</a>   const g = (dx3 * dy2 - dx2 * dy3) / determinant;</span>
<span id="L63"><a href="#L63" aria-label="Line 63">63</a>   const h = (dx1 * dy3 - dx3 * dy1) / determinant;</span>
<span id="L64"><a href="#L64" aria-label="Line 64">64</a>   if ([1 + g, 1 + h, 1 + g + h].some(w =&gt; w &lt;= 64 * Number.EPSILON * (1 + Math.abs(g) + Math.abs(h)))) {</span>
<span id="L65"><a href="#L65" aria-label="Line 65">65</a>     throw new RangeError('Perspective mapping cannot certify a finite source rectangle.');</span>
<span id="L66"><a href="#L66" aria-label="Line 66">66</a>   }</span>
<span id="L67"><a href="#L67" aria-label="Line 67">67</a>   const xx = b[0] - a[0] + g * b[0], xy = c[0] - a[0] + h * c[0];</span>
<span id="L68"><a href="#L68" aria-label="Line 68">68</a>   const yx = b[1] - a[1] + g * b[1], yy = c[1] - a[1] + h * c[1];</span>
<span id="L69"><a href="#L69" aria-label="Line 69">69</a>   return span =&gt; {</span>
<span id="L70"><a href="#L70" aria-label="Line 70">70</a>     const w = span.w.map((weight, i) =&gt; g * span.x[i] + h * span.y[i] + weight);</span>
<span id="L71"><a href="#L71" aria-label="Line 71">71</a>     const x = w.map((weight, i) =&gt; ((xx * span.x[i] + xy * span.y[i] + a[0] * span.w[i]) * extent + origin[0] * weight) * scale);</span>
<span id="L72"><a href="#L72" aria-label="Line 72">72</a>     const y = w.map((weight, i) =&gt; ((yx * span.x[i] + yy * span.y[i] + a[1] * span.w[i]) * extent + origin[1] * weight) * scale);</span>
<span id="L73"><a href="#L73" aria-label="Line 73">73</a>     return { x, y, w };</span>
<span id="L74"><a href="#L74" aria-label="Line 74">74</a>   };</span>
<span id="L75"><a href="#L75" aria-label="Line 75">75</a> }</span>
<span id="L76"><a href="#L76" aria-label="Line 76">76</a> </span>
<span id="L77"><a href="#L77" aria-label="Line 77">77</a> /** Approximate a four-corner warp in destination units, returning independent line geometry. */</span>
<span id="L78"><a href="#L78" aria-label="Line 78">78</a> export function warpPath(path: PathGeometry, options: Readonly&lt;WarpPathOptions&gt;): PathGeometry {</span>
<span id="L79"><a href="#L79" aria-label="Line 79">79</a>   if (!(path instanceof PathGeometry)) throw new TypeError('warpPath requires PathGeometry.');</span>
<span id="L80"><a href="#L80" aria-label="Line 80">80</a>   const source = options?.source, destination = options?.destination;</span>
<span id="L81"><a href="#L81" aria-label="Line 81">81</a>   const sx = number(source?.x, 'source.x'), sy = number(source?.y, 'source.y');</span>
<span id="L82"><a href="#L82" aria-label="Line 82">82</a>   const width = number(source?.width, 'source.width'), height = number(source?.height, 'source.height');</span>
<span id="L83"><a href="#L83" aria-label="Line 83">83</a>   const tolerance = number(options?.tolerance, 'tolerance');</span>
<span id="L84"><a href="#L84" aria-label="Line 84">84</a>   const requestedLimit = options?.maxSegments;</span>
<span id="L85"><a href="#L85" aria-label="Line 85">85</a>   const maxSegments = number(requestedLimit === undefined ? 1_000_000 : requestedLimit, 'maxSegments');</span>
<span id="L86"><a href="#L86" aria-label="Line 86">86</a>   if (width &lt;= 0 || height &lt;= 0 || tolerance &lt;= 0 || !Number.isSafeInteger(maxSegments) || maxSegments &lt;= 0) {</span>
<span id="L87"><a href="#L87" aria-label="Line 87">87</a>     throw new RangeError('warpPath requires positive dimensions, tolerance, and a positive safe-integer maxSegments.');</span>
<span id="L88"><a href="#L88" aria-label="Line 88">88</a>   }</span>
<span id="L89"><a href="#L89" aria-label="Line 89">89</a>   const requestedMode = options?.mode;</span>
<span id="L90"><a href="#L90" aria-label="Line 90">90</a>   const mode = requestedMode === undefined ? 'bilinear' : requestedMode;</span>
<span id="L91"><a href="#L91" aria-label="Line 91">91</a>   if (mode !== 'bilinear' &amp;&amp; mode !== 'perspective') throw new TypeError('Unknown warpPath mode.');</span>
<span id="L92"><a href="#L92" aria-label="Line 92">92</a>   const corners = [point(destination?.topLeft, 'topLeft'), point(destination?.topRight, 'topRight'),</span>
<span id="L93"><a href="#L93" aria-label="Line 93">93</a>     point(destination?.bottomLeft, 'bottomLeft'), point(destination?.bottomRight, 'bottomRight')];</span>
<span id="L94"><a href="#L94" aria-label="Line 94">94</a>   const project = mode === 'perspective' ? perspective(corners) : undefined;</span>
<span id="L95"><a href="#L95" aria-label="Line 95">95</a>   const map = (span: WarpSpan): WarpSpan =&gt; {</span>
<span id="L96"><a href="#L96" aria-label="Line 96">96</a>     const u = span.x.map((x, i) =&gt; (x - sx * span.w[i]) / width);</span>
<span id="L97"><a href="#L97" aria-label="Line 97">97</a>     const v = span.y.map((y, i) =&gt; (y - sy * span.w[i]) / height);</span>
<span id="L98"><a href="#L98" aria-label="Line 98">98</a>     if (project) return project({ x: u, y: v, w: span.w });</span>
<span id="L99"><a href="#L99" aria-label="Line 99">99</a>     const wu = span.w.map((w, i) =&gt; w - u[i]), wv = span.w.map((w, i) =&gt; w - v[i]);</span>
<span id="L100"><a href="#L100" aria-label="Line 100">100</a>     const weights = [product(wu, wv), product(u, wv), product(wu, v), product(u, v)];</span>
<span id="L101"><a href="#L101" aria-label="Line 101">101</a>     const w = product(span.w, span.w);</span>
<span id="L102"><a href="#L102" aria-label="Line 102">102</a>     const x = w.map((_, i) =&gt; weights.reduce((sum, ws, j) =&gt; sum + ws[i] * corners[j][0], 0));</span>
<span id="L103"><a href="#L103" aria-label="Line 103">103</a>     const y = w.map((_, i) =&gt; weights.reduce((sum, ws, j) =&gt; sum + ws[i] * corners[j][1], 0));</span>
<span id="L104"><a href="#L104" aria-label="Line 104">104</a>     return { x, y, w };</span>
<span id="L105"><a href="#L105" aria-label="Line 105">105</a>   };</span>
<span id="L106"><a href="#L106" aria-label="Line 106">106</a>   const output = new PathGeometry();</span>
<span id="L107"><a href="#L107" aria-label="Line 107">107</a>   let count = 0, closed = false;</span>
<span id="L108"><a href="#L108" aria-label="Line 108">108</a>   const reserve = () =&gt; {</span>
<span id="L109"><a href="#L109" aria-label="Line 109">109</a>     if (++count &gt; maxSegments) throw new RangeError('warpPath exceeded maxSegments.');</span>
<span id="L110"><a href="#L110" aria-label="Line 110">110</a>   };</span>
<span id="L111"><a href="#L111" aria-label="Line 111">111</a>   const line = (x: number, y: number) =&gt; { reserve(); output.lineTo(x, y); closed = false; };</span>
<span id="L112"><a href="#L112" aria-label="Line 112">112</a>   // At most 4M subdivision visits per call, even for a pole with no output.</span>
<span id="L113"><a href="#L113" aria-label="Line 113">113</a>   const work: WarpWork = { remaining: Math.min(4_000_000, 2 * maxSegments + 53), pool: [], depths: [], scratch: [] };</span>
<span id="L114"><a href="#L114" aria-label="Line 114">114</a>   const emit = (span: WarpSpan) =&gt; {</span>
<span id="L115"><a href="#L115" aria-label="Line 115">115</a>     if (count &gt;= maxSegments) throw new RangeError('warpPath exceeded maxSegments.');</span>
<span id="L116"><a href="#L116" aria-label="Line 116">116</a>     emitSpan(map(span), tolerance, work, line);</span>
<span id="L117"><a href="#L117" aria-label="Line 117">117</a>   };</span>
<span id="L118"><a href="#L118" aria-label="Line 118">118</a>   let current: WarpPoint = [0, 0], start: WarpPoint = current;</span>
<span id="L119"><a href="#L119" aria-label="Line 119">119</a>   for (const segment of path) {</span>
<span id="L120"><a href="#L120" aria-label="Line 120">120</a>     switch (segment.type) {</span>
<span id="L121"><a href="#L121" aria-label="Line 121">121</a>       case 'move': {</span>
<span id="L122"><a href="#L122" aria-label="Line 122">122</a>         current = start = [segment.x, segment.y];</span>
<span id="L123"><a href="#L123" aria-label="Line 123">123</a>         const p = endpoint(map(polynomial([current])), 0);</span>
<span id="L124"><a href="#L124" aria-label="Line 124">124</a>         reserve(); output.moveTo(p[0], p[1]); closed = false;</span>
<span id="L125"><a href="#L125" aria-label="Line 125">125</a>         break;</span>
<span id="L126"><a href="#L126" aria-label="Line 126">126</a>       }</span>
<span id="L127"><a href="#L127" aria-label="Line 127">127</a>       case 'line': {</span>
<span id="L128"><a href="#L128" aria-label="Line 128">128</a>         const end: WarpPoint = [segment.x, segment.y];</span>
<span id="L129"><a href="#L129" aria-label="Line 129">129</a>         emit(polynomial([current, end])); current = end; break;</span>
<span id="L130"><a href="#L130" aria-label="Line 130">130</a>       }</span>
<span id="L131"><a href="#L131" aria-label="Line 131">131</a>       case 'quadratic': {</span>
<span id="L132"><a href="#L132" aria-label="Line 132">132</a>         const end: WarpPoint = [segment.x, segment.y];</span>
<span id="L133"><a href="#L133" aria-label="Line 133">133</a>         emit(polynomial([current, [segment.cpx, segment.cpy], end])); current = end; break;</span>
<span id="L134"><a href="#L134" aria-label="Line 134">134</a>       }</span>
<span id="L135"><a href="#L135" aria-label="Line 135">135</a>       case 'cubic': {</span>
<span id="L136"><a href="#L136" aria-label="Line 136">136</a>         const end: WarpPoint = [segment.x, segment.y];</span>
<span id="L137"><a href="#L137" aria-label="Line 137">137</a>         emit(polynomial([current, [segment.cp1x, segment.cp1y], [segment.cp2x, segment.cp2y], end]));</span>
<span id="L138"><a href="#L138" aria-label="Line 138">138</a>         current = end; break;</span>
<span id="L139"><a href="#L139" aria-label="Line 139">139</a>       }</span>
<span id="L140"><a href="#L140" aria-label="Line 140">140</a>       case 'arc': {</span>
<span id="L141"><a href="#L141" aria-label="Line 141">141</a>         const first = arcPoint(segment, segment.startAngle);</span>
<span id="L142"><a href="#L142" aria-label="Line 142">142</a>         if (current[0] !== first[0] || current[1] !== first[1]) emit(polynomial([current, first]));</span>
<span id="L143"><a href="#L143" aria-label="Line 143">143</a>         const pieces = Math.max(1, Math.ceil(Math.abs(segment.sweep) / (Math.PI / 2)));</span>
<span id="L144"><a href="#L144" aria-label="Line 144">144</a>         for (let i = 0; i &lt; pieces; i++) {</span>
<span id="L145"><a href="#L145" aria-label="Line 145">145</a>           const a = segment.startAngle + segment.sweep * i / pieces;</span>
<span id="L146"><a href="#L146" aria-label="Line 146">146</a>           const b = segment.startAngle + segment.sweep * (i + 1) / pieces;</span>
<span id="L147"><a href="#L147" aria-label="Line 147">147</a>           const mid = (a + b) / 2, weight = Math.cos((b - a) / 2);</span>
<span id="L148"><a href="#L148" aria-label="Line 148">148</a>           const p = arcPoint(segment, a), q = arcPoint(segment, b);</span>
<span id="L149"><a href="#L149" aria-label="Line 149">149</a>           emit({</span>
<span id="L150"><a href="#L150" aria-label="Line 150">150</a>             x: [p[0], segment.cx * weight + segment.ux * Math.cos(mid) + segment.vx * Math.sin(mid), q[0]],</span>
<span id="L151"><a href="#L151" aria-label="Line 151">151</a>             y: [p[1], segment.cy * weight + segment.uy * Math.cos(mid) + segment.vy * Math.sin(mid), q[1]],</span>
<span id="L152"><a href="#L152" aria-label="Line 152">152</a>             w: [1, weight, 1],</span>
<span id="L153"><a href="#L153" aria-label="Line 153">153</a>           });</span>
<span id="L154"><a href="#L154" aria-label="Line 154">154</a>         }</span>
<span id="L155"><a href="#L155" aria-label="Line 155">155</a>         current = arcPoint(segment, segment.startAngle + segment.sweep);</span>
<span id="L156"><a href="#L156" aria-label="Line 156">156</a>         break;</span>
<span id="L157"><a href="#L157" aria-label="Line 157">157</a>       }</span>
<span id="L158"><a href="#L158" aria-label="Line 158">158</a>       case 'close':</span>
<span id="L159"><a href="#L159" aria-label="Line 159">159</a>         if (current[0] !== start[0] || current[1] !== start[1]) emit(polynomial([current, start]));</span>
<span id="L160"><a href="#L160" aria-label="Line 160">160</a>         if (!closed) { reserve(); output.closePath(); closed = true; }</span>
<span id="L161"><a href="#L161" aria-label="Line 161">161</a>         current = start; break;</span>
<span id="L162"><a href="#L162" aria-label="Line 162">162</a>     }</span>
<span id="L163"><a href="#L163" aria-label="Line 163">163</a>   }</span>
<span id="L164"><a href="#L164" aria-label="Line 164">164</a>   return output;</span>
<span id="L165"><a href="#L165" aria-label="Line 165">165</a> }</span>
<span id="L166"><a href="#L166" aria-label="Line 166">166</a> </span></code></pre>

## Documentation version

Documentation built with @pibbl/core 0.0.2, revision 272a94a. ALPHA — NOT FOR PRODUCTION USE.
