@open-pencil/scene-graph 0.17.0 → 0.18.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (423) hide show
  1. package/dist/behaviours/index.d.ts +3 -2
  2. package/dist/behaviours/index.js +2 -1
  3. package/dist/behaviours/kinds.d.ts.map +1 -1
  4. package/dist/behaviours/kinds.js +15 -8
  5. package/dist/behaviours/kinds.js.map +1 -1
  6. package/dist/behaviours/model.d.ts +2 -2
  7. package/dist/behaviours/model.d.ts.map +1 -1
  8. package/dist/behaviours/model.js.map +1 -1
  9. package/dist/behaviours/schema.d.ts +136 -1
  10. package/dist/behaviours/schema.d.ts.map +1 -1
  11. package/dist/behaviours/schema.js +17 -2
  12. package/dist/behaviours/schema.js.map +1 -1
  13. package/dist/behaviours/spec.d.ts.map +1 -1
  14. package/dist/behaviours/spec.js +4 -0
  15. package/dist/behaviours/spec.js.map +1 -1
  16. package/dist/behaviours/transition.d.ts +23 -0
  17. package/dist/behaviours/transition.d.ts.map +1 -0
  18. package/dist/behaviours/transition.js +36 -0
  19. package/dist/behaviours/transition.js.map +1 -0
  20. package/dist/checkpoint.d.ts.map +1 -1
  21. package/dist/checkpoint.js +2 -0
  22. package/dist/checkpoint.js.map +1 -1
  23. package/dist/color/index.d.ts +12 -9
  24. package/dist/color/index.d.ts.map +1 -1
  25. package/dist/color/index.js +15 -25
  26. package/dist/color/index.js.map +1 -1
  27. package/dist/color/management.d.ts +51 -23
  28. package/dist/color/management.d.ts.map +1 -1
  29. package/dist/color/management.js +154 -149
  30. package/dist/color/management.js.map +1 -1
  31. package/dist/color/okhcl.d.ts +28 -10
  32. package/dist/color/okhcl.d.ts.map +1 -1
  33. package/dist/color/okhcl.js +56 -40
  34. package/dist/color/okhcl.js.map +1 -1
  35. package/dist/color/schema.d.ts +6 -1
  36. package/dist/color/schema.d.ts.map +1 -1
  37. package/dist/color/schema.js +10 -1
  38. package/dist/color/schema.js.map +1 -1
  39. package/dist/constraints.d.ts.map +1 -1
  40. package/dist/constraints.js +2 -1
  41. package/dist/constraints.js.map +1 -1
  42. package/dist/copy.d.ts +7 -1
  43. package/dist/copy.d.ts.map +1 -1
  44. package/dist/copy.js +22 -1
  45. package/dist/copy.js.map +1 -1
  46. package/dist/corners.d.ts +3 -1
  47. package/dist/corners.d.ts.map +1 -1
  48. package/dist/corners.js +3 -1
  49. package/dist/corners.js.map +1 -1
  50. package/dist/css/grid.js +1 -1
  51. package/dist/css/index.d.ts +2 -1
  52. package/dist/css/index.js +3 -2
  53. package/dist/css/motion.d.ts +12 -0
  54. package/dist/css/motion.d.ts.map +1 -0
  55. package/dist/css/motion.js +60 -0
  56. package/dist/css/motion.js.map +1 -0
  57. package/dist/css/shadows.d.ts +4 -3
  58. package/dist/css/shadows.d.ts.map +1 -1
  59. package/dist/css/shadows.js +7 -6
  60. package/dist/css/shadows.js.map +1 -1
  61. package/dist/css/values.d.ts +3 -2
  62. package/dist/css/values.d.ts.map +1 -1
  63. package/dist/css/values.js +4 -4
  64. package/dist/css/values.js.map +1 -1
  65. package/dist/fields/index.d.ts +2 -2
  66. package/dist/fields/index.js +2 -2
  67. package/dist/fields/layout.d.ts +3 -1
  68. package/dist/fields/layout.d.ts.map +1 -1
  69. package/dist/fields/layout.js +9 -1
  70. package/dist/fields/layout.js.map +1 -1
  71. package/dist/figjam/palette.js +1 -1
  72. package/dist/font-style.d.ts.map +1 -1
  73. package/dist/font-style.js +7 -3
  74. package/dist/font-style.js.map +1 -1
  75. package/dist/generated/index.d.ts +6 -2
  76. package/dist/generated/index.d.ts.map +1 -1
  77. package/dist/generated/index.js +14 -4
  78. package/dist/generated/index.js.map +1 -1
  79. package/dist/geometry.d.ts +6 -1
  80. package/dist/geometry.d.ts.map +1 -1
  81. package/dist/geometry.js +10 -2
  82. package/dist/geometry.js.map +1 -1
  83. package/dist/graph.d.ts +29 -4
  84. package/dist/graph.d.ts.map +1 -1
  85. package/dist/graph.js +59 -9
  86. package/dist/graph.js.map +1 -1
  87. package/dist/group-bounds.d.ts +5 -2
  88. package/dist/group-bounds.d.ts.map +1 -1
  89. package/dist/group-bounds.js +38 -8
  90. package/dist/group-bounds.js.map +1 -1
  91. package/dist/hit-test.d.ts.map +1 -1
  92. package/dist/hit-test.js +19 -8
  93. package/dist/hit-test.js.map +1 -1
  94. package/dist/index.d.ts +2 -2
  95. package/dist/index.js +2 -2
  96. package/dist/instances/content-size.js +1 -1
  97. package/dist/instances/fields.d.ts +2 -2
  98. package/dist/instances/fields.d.ts.map +1 -1
  99. package/dist/instances/fields.js +2 -0
  100. package/dist/instances/fields.js.map +1 -1
  101. package/dist/instances/index.d.ts.map +1 -1
  102. package/dist/instances/index.js +50 -11
  103. package/dist/instances/index.js.map +1 -1
  104. package/dist/instances/sync.js +67 -7
  105. package/dist/instances/sync.js.map +1 -1
  106. package/dist/layout/flow.d.ts +16 -0
  107. package/dist/layout/flow.d.ts.map +1 -0
  108. package/dist/layout/flow.js +34 -0
  109. package/dist/layout/flow.js.map +1 -0
  110. package/dist/layout/index.d.ts +4 -3
  111. package/dist/layout/index.js +4 -3
  112. package/dist/layout/keywords.d.ts.map +1 -1
  113. package/dist/layout/keywords.js +7 -1
  114. package/dist/layout/keywords.js.map +1 -1
  115. package/dist/layout/sizing.d.ts +9 -1
  116. package/dist/layout/sizing.d.ts.map +1 -1
  117. package/dist/layout/sizing.js +17 -1
  118. package/dist/layout/sizing.js.map +1 -1
  119. package/dist/layout/spacing.d.ts +7 -1
  120. package/dist/layout/spacing.d.ts.map +1 -1
  121. package/dist/layout/spacing.js +10 -1
  122. package/dist/layout/spacing.js.map +1 -1
  123. package/dist/mesh/geometry.d.ts +83 -0
  124. package/dist/mesh/geometry.d.ts.map +1 -0
  125. package/dist/mesh/geometry.js +441 -0
  126. package/dist/mesh/geometry.js.map +1 -0
  127. package/dist/mesh/index.d.ts +4 -0
  128. package/dist/mesh/index.js +4 -0
  129. package/dist/mesh/model.d.ts +28 -0
  130. package/dist/mesh/model.d.ts.map +1 -0
  131. package/dist/mesh/model.js +40 -0
  132. package/dist/mesh/model.js.map +1 -0
  133. package/dist/mesh/schema.d.ts +191 -0
  134. package/dist/mesh/schema.d.ts.map +1 -0
  135. package/dist/mesh/schema.js +56 -0
  136. package/dist/mesh/schema.js.map +1 -0
  137. package/dist/mutation-impact.d.ts +2 -0
  138. package/dist/mutation-impact.d.ts.map +1 -1
  139. package/dist/mutation-impact.js +18 -7
  140. package/dist/mutation-impact.js.map +1 -1
  141. package/dist/node-defaults.d.ts +3 -1
  142. package/dist/node-defaults.d.ts.map +1 -1
  143. package/dist/node-defaults.js +22 -6
  144. package/dist/node-defaults.js.map +1 -1
  145. package/dist/paints/index.d.ts +5 -3
  146. package/dist/paints/index.js +5 -3
  147. package/dist/paints/pattern.d.ts +5 -1
  148. package/dist/paints/pattern.d.ts.map +1 -1
  149. package/dist/paints/pattern.js +33 -1
  150. package/dist/paints/pattern.js.map +1 -1
  151. package/dist/paints/shows.d.ts +4 -1
  152. package/dist/paints/shows.d.ts.map +1 -1
  153. package/dist/paints/shows.js +8 -1
  154. package/dist/paints/shows.js.map +1 -1
  155. package/dist/paints/solid.d.ts +11 -0
  156. package/dist/paints/solid.d.ts.map +1 -0
  157. package/dist/paints/solid.js +18 -0
  158. package/dist/paints/solid.js.map +1 -0
  159. package/dist/paints/stills.d.ts +74 -0
  160. package/dist/paints/stills.d.ts.map +1 -0
  161. package/dist/paints/stills.js +113 -0
  162. package/dist/paints/stills.js.map +1 -0
  163. package/dist/plugin-data/field.d.ts.map +1 -1
  164. package/dist/plugin-data/field.js +2 -0
  165. package/dist/plugin-data/field.js.map +1 -1
  166. package/dist/plugin-data/fields.d.ts +67 -0
  167. package/dist/plugin-data/fields.d.ts.map +1 -1
  168. package/dist/plugin-data/fields.js +12 -7
  169. package/dist/plugin-data/fields.js.map +1 -1
  170. package/dist/resize.d.ts +10 -3
  171. package/dist/resize.d.ts.map +1 -1
  172. package/dist/resize.js +34 -3
  173. package/dist/resize.js.map +1 -1
  174. package/dist/scaling/index.d.ts +2 -2
  175. package/dist/scaling/index.js +2 -2
  176. package/dist/scaling/node.d.ts +2 -1
  177. package/dist/scaling/node.d.ts.map +1 -1
  178. package/dist/scaling/node.js +27 -7
  179. package/dist/scaling/node.js.map +1 -1
  180. package/dist/shaders/index.d.ts +2 -3
  181. package/dist/shaders/index.js +2 -3
  182. package/dist/shaders/model.d.ts +3 -11
  183. package/dist/shaders/model.d.ts.map +1 -1
  184. package/dist/shaders/model.js +13 -36
  185. package/dist/shaders/model.js.map +1 -1
  186. package/dist/shaders/schema.d.ts +1 -1
  187. package/dist/shaders/schema.js +1 -1
  188. package/dist/shaders/schema.js.map +1 -1
  189. package/dist/shape-with-text/document.js +276 -0
  190. package/dist/shape-with-text/document.js.map +1 -0
  191. package/dist/shape-with-text/geometry.d.ts +13 -1
  192. package/dist/shape-with-text/geometry.d.ts.map +1 -1
  193. package/dist/shape-with-text/geometry.js +103 -41
  194. package/dist/shape-with-text/geometry.js.map +1 -1
  195. package/dist/shape-with-text/index.d.ts +2 -2
  196. package/dist/shape-with-text/index.js +2 -2
  197. package/dist/shared-style-index.d.ts +21 -0
  198. package/dist/shared-style-index.d.ts.map +1 -0
  199. package/dist/shared-style-index.js +72 -0
  200. package/dist/shared-style-index.js.map +1 -0
  201. package/dist/shared-styles.d.ts +3 -3
  202. package/dist/shared-styles.d.ts.map +1 -1
  203. package/dist/shared-styles.js +16 -7
  204. package/dist/shared-styles.js.map +1 -1
  205. package/dist/slides/drop.d.ts +32 -0
  206. package/dist/slides/drop.d.ts.map +1 -0
  207. package/dist/slides/drop.js +81 -0
  208. package/dist/slides/drop.js.map +1 -0
  209. package/dist/slides/index.d.ts +56 -0
  210. package/dist/slides/index.d.ts.map +1 -0
  211. package/dist/slides/index.js +134 -0
  212. package/dist/slides/index.js.map +1 -0
  213. package/dist/slides/motion.d.ts +55 -0
  214. package/dist/slides/motion.d.ts.map +1 -0
  215. package/dist/slides/motion.js +142 -0
  216. package/dist/slides/motion.js.map +1 -0
  217. package/dist/slides/playback.d.ts +59 -0
  218. package/dist/slides/playback.d.ts.map +1 -0
  219. package/dist/slides/playback.js +188 -0
  220. package/dist/slides/playback.js.map +1 -0
  221. package/dist/slides/types.d.ts +62 -0
  222. package/dist/slides/types.d.ts.map +1 -0
  223. package/dist/slides/types.js +39 -0
  224. package/dist/slides/types.js.map +1 -0
  225. package/dist/statuses/workflow.js +1 -1
  226. package/dist/text/direction.d.ts +6 -3
  227. package/dist/text/direction.d.ts.map +1 -1
  228. package/dist/text/direction.js +12 -12
  229. package/dist/text/direction.js.map +1 -1
  230. package/dist/text/index.d.ts +2 -1
  231. package/dist/text/index.js +3 -2
  232. package/dist/text/name.d.ts +22 -0
  233. package/dist/text/name.d.ts.map +1 -0
  234. package/dist/text/name.js +38 -0
  235. package/dist/text/name.js.map +1 -0
  236. package/dist/text/references.d.ts.map +1 -1
  237. package/dist/text/references.js +1 -2
  238. package/dist/text/references.js.map +1 -1
  239. package/dist/thumbnail.d.ts +23 -0
  240. package/dist/thumbnail.d.ts.map +1 -0
  241. package/dist/thumbnail.js +47 -0
  242. package/dist/thumbnail.js.map +1 -0
  243. package/dist/transfer/apply.d.ts.map +1 -1
  244. package/dist/transfer/apply.js +14 -3
  245. package/dist/transfer/apply.js.map +1 -1
  246. package/dist/transfer/copied.d.ts +6 -1
  247. package/dist/transfer/copied.d.ts.map +1 -1
  248. package/dist/transfer/copied.js +2 -2
  249. package/dist/transfer/copied.js.map +1 -1
  250. package/dist/transfer/dependencies.d.ts +4 -1
  251. package/dist/transfer/dependencies.d.ts.map +1 -1
  252. package/dist/transfer/dependencies.js +10 -1
  253. package/dist/transfer/dependencies.js.map +1 -1
  254. package/dist/transfer/history.js +2 -0
  255. package/dist/transfer/history.js.map +1 -1
  256. package/dist/transfer/identity.js +18 -3
  257. package/dist/transfer/identity.js.map +1 -1
  258. package/dist/transfer/index.d.ts +2 -1
  259. package/dist/transfer/index.js +4 -3
  260. package/dist/transfer/library.d.ts +45 -0
  261. package/dist/transfer/library.d.ts.map +1 -0
  262. package/dist/transfer/library.js +102 -0
  263. package/dist/transfer/library.js.map +1 -0
  264. package/dist/transfer/nodes.d.ts.map +1 -1
  265. package/dist/transfer/nodes.js +9 -0
  266. package/dist/transfer/nodes.js.map +1 -1
  267. package/dist/transfer/plan.d.ts +9 -0
  268. package/dist/transfer/plan.d.ts.map +1 -1
  269. package/dist/transfer/plan.js +51 -6
  270. package/dist/transfer/plan.js.map +1 -1
  271. package/dist/transfer/remove.js +7 -1
  272. package/dist/transfer/remove.js.map +1 -1
  273. package/dist/transfer/validation.js +8 -4
  274. package/dist/transfer/validation.js.map +1 -1
  275. package/dist/transfer/variables.d.ts.map +1 -1
  276. package/dist/transfer/variables.js +2 -1
  277. package/dist/transfer/variables.js.map +1 -1
  278. package/dist/types.d.ts +90 -12
  279. package/dist/types.d.ts.map +1 -1
  280. package/dist/types.js +33 -1
  281. package/dist/types.js.map +1 -1
  282. package/dist/variables/bindings.d.ts +11 -6
  283. package/dist/variables/bindings.d.ts.map +1 -1
  284. package/dist/variables/bindings.js +162 -38
  285. package/dist/variables/bindings.js.map +1 -1
  286. package/dist/variables/easing.d.ts +48 -0
  287. package/dist/variables/easing.d.ts.map +1 -0
  288. package/dist/variables/easing.js +321 -0
  289. package/dist/variables/easing.js.map +1 -0
  290. package/dist/variables/formula/index.d.ts +4 -2
  291. package/dist/variables/formula/index.d.ts.map +1 -1
  292. package/dist/variables/formula/index.js +26 -9
  293. package/dist/variables/formula/index.js.map +1 -1
  294. package/dist/variables/formula/parse.js +1 -1
  295. package/dist/variables/formula/schema.js +1 -1
  296. package/dist/variables/formula/signatures.d.ts +2 -2
  297. package/dist/variables/formula/signatures.d.ts.map +1 -1
  298. package/dist/variables/formula/signatures.js.map +1 -1
  299. package/dist/variables/formula/tree.d.ts +2 -2
  300. package/dist/variables/formula/tree.d.ts.map +1 -1
  301. package/dist/variables/formula/tree.js.map +1 -1
  302. package/dist/variables/index.d.ts +22 -5
  303. package/dist/variables/index.d.ts.map +1 -1
  304. package/dist/variables/index.js +67 -17
  305. package/dist/variables/index.js.map +1 -1
  306. package/dist/variables/lookup.js +1 -1
  307. package/dist/variables/lookup.js.map +1 -1
  308. package/dist/variables/motion.d.ts +46 -0
  309. package/dist/variables/motion.d.ts.map +1 -0
  310. package/dist/variables/motion.js +107 -0
  311. package/dist/variables/motion.js.map +1 -0
  312. package/dist/variables/paints.d.ts +10 -2
  313. package/dist/variables/paints.d.ts.map +1 -1
  314. package/dist/variables/paints.js +24 -10
  315. package/dist/variables/paints.js.map +1 -1
  316. package/dist/variables/resolution.d.ts.map +1 -1
  317. package/dist/variables/resolution.js +10 -9
  318. package/dist/variables/resolution.js.map +1 -1
  319. package/dist/variables/schema.d.ts +59 -1
  320. package/dist/variables/schema.d.ts.map +1 -1
  321. package/dist/variables/schema.js +36 -1
  322. package/dist/variables/schema.js.map +1 -1
  323. package/dist/variables/style-bindings.d.ts +23 -0
  324. package/dist/variables/style-bindings.d.ts.map +1 -0
  325. package/dist/variables/style-bindings.js +83 -0
  326. package/dist/variables/style-bindings.js.map +1 -0
  327. package/package.json +21 -2
  328. package/src/behaviours/index.ts +1 -0
  329. package/src/behaviours/kinds.ts +5 -1
  330. package/src/behaviours/model.ts +2 -2
  331. package/src/behaviours/schema.ts +19 -2
  332. package/src/behaviours/spec.ts +5 -0
  333. package/src/behaviours/transition.ts +46 -0
  334. package/src/checkpoint.ts +2 -0
  335. package/src/color/index.ts +15 -24
  336. package/src/color/management.ts +144 -168
  337. package/src/color/okhcl.ts +62 -43
  338. package/src/color/schema.ts +13 -0
  339. package/src/constraints.ts +2 -1
  340. package/src/copy.ts +29 -0
  341. package/src/corners.ts +3 -0
  342. package/src/css/index.ts +1 -0
  343. package/src/css/motion.ts +51 -0
  344. package/src/css/shadows.ts +7 -6
  345. package/src/css/values.ts +7 -3
  346. package/src/fields/layout.ts +9 -0
  347. package/src/font-style.ts +8 -3
  348. package/src/generated/index.ts +15 -3
  349. package/src/geometry.ts +13 -1
  350. package/src/graph.ts +72 -9
  351. package/src/group-bounds.ts +54 -13
  352. package/src/hit-test.ts +13 -8
  353. package/src/instances/fields.ts +2 -0
  354. package/src/instances/index.ts +71 -12
  355. package/src/instances/sync.ts +93 -11
  356. package/src/layout/flow.ts +34 -0
  357. package/src/layout/index.ts +1 -0
  358. package/src/layout/keywords.ts +7 -1
  359. package/src/layout/sizing.ts +30 -0
  360. package/src/layout/spacing.ts +12 -0
  361. package/src/mesh/geometry.ts +433 -0
  362. package/src/mesh/index.ts +3 -0
  363. package/src/mesh/model.ts +50 -0
  364. package/src/mesh/schema.ts +81 -0
  365. package/src/mutation-impact.ts +23 -7
  366. package/src/node-defaults.ts +22 -5
  367. package/src/paints/index.ts +2 -0
  368. package/src/paints/pattern.ts +55 -0
  369. package/src/paints/shows.ts +9 -0
  370. package/src/paints/solid.ts +10 -0
  371. package/src/paints/stills.ts +182 -0
  372. package/src/plugin-data/field.ts +3 -0
  373. package/src/plugin-data/fields.ts +15 -2
  374. package/src/resize.ts +32 -20
  375. package/src/scaling/index.ts +1 -1
  376. package/src/scaling/node.ts +43 -6
  377. package/src/shaders/index.ts +0 -1
  378. package/src/shaders/model.ts +19 -49
  379. package/src/shaders/schema.ts +1 -1
  380. package/src/shape-with-text/document.ts +175 -0
  381. package/src/shape-with-text/geometry.ts +99 -27
  382. package/src/shared-style-index.ts +106 -0
  383. package/src/shared-styles.ts +21 -14
  384. package/src/slides/drop.ts +94 -0
  385. package/src/slides/index.ts +125 -0
  386. package/src/slides/motion.ts +169 -0
  387. package/src/slides/playback.ts +200 -0
  388. package/src/slides/types.ts +94 -0
  389. package/src/text/direction.ts +13 -23
  390. package/src/text/index.ts +1 -0
  391. package/src/text/name.ts +39 -0
  392. package/src/text/references.ts +3 -3
  393. package/src/thumbnail.ts +52 -0
  394. package/src/transfer/apply.ts +16 -3
  395. package/src/transfer/copied.ts +7 -2
  396. package/src/transfer/dependencies.ts +10 -1
  397. package/src/transfer/history.ts +2 -0
  398. package/src/transfer/identity.ts +34 -2
  399. package/src/transfer/index.ts +6 -0
  400. package/src/transfer/library.ts +153 -0
  401. package/src/transfer/nodes.ts +20 -0
  402. package/src/transfer/plan.ts +96 -10
  403. package/src/transfer/remove.ts +6 -2
  404. package/src/transfer/validation.ts +15 -7
  405. package/src/transfer/variables.ts +8 -2
  406. package/src/types.ts +114 -10
  407. package/src/variables/bindings.ts +260 -36
  408. package/src/variables/easing.ts +282 -0
  409. package/src/variables/formula/index.ts +33 -14
  410. package/src/variables/formula/signatures.ts +6 -2
  411. package/src/variables/formula/tree.ts +3 -3
  412. package/src/variables/index.ts +90 -37
  413. package/src/variables/lookup.ts +1 -1
  414. package/src/variables/motion.ts +151 -0
  415. package/src/variables/paints.ts +29 -7
  416. package/src/variables/resolution.ts +11 -5
  417. package/src/variables/schema.ts +41 -0
  418. package/src/variables/style-bindings.ts +137 -0
  419. package/dist/shaders/still.d.ts +0 -18
  420. package/dist/shaders/still.d.ts.map +0 -1
  421. package/dist/shaders/still.js +0 -36
  422. package/dist/shaders/still.js.map +0 -1
  423. package/src/shaders/still.ts +0 -41
@@ -1,10 +1,13 @@
1
+ import { uniq } from 'es-toolkit/array'
2
+ import { pick } from 'es-toolkit/object'
1
3
  import { isEmptyObject, isEqual } from 'es-toolkit/predicate'
2
4
 
3
5
  import type { SceneGraph } from '../graph'
4
6
  import { hasInstanceOverride } from '../instances/overrides'
5
7
  import type { Color } from '../primitives'
8
+ import { isTextStyleLength, scaledTextStyleLengths } from '../scaling/node'
6
9
  import { pixelLength } from '../text/lengths'
7
- import type { Effect, SceneNode, StyleRun, TextLength } from '../types'
10
+ import type { CharacterStyleOverride, Effect, SceneNode, StyleRun, TextLength } from '../types'
8
11
  import {
9
12
  effectBindableProperties,
10
13
  effectBindingTarget,
@@ -12,8 +15,19 @@ import {
12
15
  isNumericVariableBindingField,
13
16
  type EffectBindingProperty
14
17
  } from './fields'
15
- import { refreshBoundPaints } from './paints'
18
+ import { refreshBoundPaints, refreshedRunFills } from './paints'
16
19
  import { collectionsResolving } from './resolution'
20
+ import {
21
+ createStyleBindingLookup,
22
+ effectiveBoundVariables,
23
+ effectiveRunBoundVariables,
24
+ inheritedRunStyleBindings,
25
+ inheritedTextStyleBindings,
26
+ layerBindingsInEffect,
27
+ textStyleConsumers,
28
+ type TextStyleLookup,
29
+ type VariableBindings
30
+ } from './style-bindings'
17
31
  import { subtreeNodes } from './subtree'
18
32
  import { variableText, variableTruthy } from './text'
19
33
  import { variableValueReferences } from './value'
@@ -65,6 +79,26 @@ function overriddenInInstance(graph: SceneGraph, nodeId: string, field: string):
65
79
  return !!instance && hasInstanceOverride(instance.instanceOverrides, instance.id, nodeId, field)
66
80
  }
67
81
 
82
+ function lengthScale(field: string, componentScale: number): number {
83
+ return isTextStyleLength(field) ? componentScale : 1
84
+ }
85
+
86
+ /** A style's lengths take the occurrence's scale; the layer's own keep the scale they recorded. */
87
+ function bindingScale(node: SceneNode, field: string, styleBound: VariableBindings): number {
88
+ if (field in styleBound) {
89
+ return lengthScale(field, node.componentScale)
90
+ }
91
+ return node.variableBindingScales[field] ?? 1
92
+ }
93
+
94
+ /** Fields of a range of characters a bound number gives its value; the rest need fonts loaded. */
95
+ const NUMERIC_RUN_FIELDS = ['fontSize', 'fontWeight', 'letterSpacing', 'lineHeight'] as const
96
+ const NUMERIC_TEXT_FIELDS: ReadonlySet<string> = new Set(NUMERIC_RUN_FIELDS)
97
+
98
+ export function resolvesToNumber(field: string): boolean {
99
+ return NUMERIC_FIELDS.has(field) || NUMERIC_TEXT_FIELDS.has(field)
100
+ }
101
+
68
102
  /** A bound number as the field stores it. */
69
103
  export function boundNumberValue(field: string, value: number): number | TextLength {
70
104
  return TEXT_LENGTH_FIELDS.has(field) ? pixelLength(value) : value
@@ -74,16 +108,29 @@ export function boundNumberValue(field: string, value: number): number | TextLen
74
108
  * The values a node's numeric bindings resolve to in its mode, in scene units, for the fields
75
109
  * whose stored value differs. A width or height an instance overrides keeps its own value.
76
110
  */
77
- export function resolvedNumericBindings(graph: SceneGraph, node: SceneNode): Partial<SceneNode> {
111
+ export function resolvedNumericBindings(
112
+ graph: SceneGraph,
113
+ node: SceneNode,
114
+ styles: TextStyleLookup = createStyleBindingLookup(graph)
115
+ ): Partial<SceneNode> {
116
+ const styleBound = inheritedTextStyleBindings(node, styles)
117
+ return resolvedNumbers(graph, node, effectiveBoundVariables(node, styles), styleBound)
118
+ }
119
+
120
+ function resolvedNumbers(
121
+ graph: SceneGraph,
122
+ node: SceneNode,
123
+ bindings: VariableBindings,
124
+ styleBound: VariableBindings
125
+ ): Partial<SceneNode> {
78
126
  const updates: Partial<SceneNode> = {}
79
- for (const [field, variableId] of Object.entries(node.boundVariables)) {
80
- if (!NUMERIC_FIELDS.has(field)) continue
127
+ for (const [field, variableId] of Object.entries(bindings)) {
128
+ if (!resolvesToNumber(field)) continue
81
129
  if ((field === 'width' || field === 'height') && overriddenInInstance(graph, node.id, field))
82
130
  continue
83
131
  const value = graph.resolveNumberVariableForNode(node.id, variableId)
84
132
  if (value === undefined || !Number.isFinite(value)) continue
85
- const scale = node.variableBindingScales[field] ?? 1
86
- const effective = value * scale
133
+ const effective = value * bindingScale(node, field, styleBound)
87
134
  if (!Number.isFinite(effective)) continue
88
135
  const next = boundNumberValue(
89
136
  field,
@@ -116,27 +163,44 @@ export function resolvedValueBindings(graph: SceneGraph, node: SceneNode): Parti
116
163
  return updates
117
164
  }
118
165
 
119
- /** Fields of a range of characters a bound number gives its value; the rest need fonts loaded. */
120
- const NUMERIC_RUN_FIELDS = ['fontSize', 'fontWeight', 'letterSpacing', 'lineHeight'] as const
166
+ type RunHolder = Pick<SceneNode, 'id' | 'styleRuns' | 'componentScale'>
121
167
 
122
168
  /**
123
- * The runs a text's character bindings resolve to in its mode, when a bound number differs from
124
- * what its characters hold. Bound font families and styles are left as stored, as the text's own.
169
+ * The runs a text's character bindings resolve to in its mode, when a bound number or a bound
170
+ * colour of a range's own fills differs from what its characters hold. Bound font families and
171
+ * styles are left as stored, as the text's own.
125
172
  */
126
173
  export function resolvedRunBindings(
127
174
  graph: SceneGraph,
128
- node: SceneNode
175
+ node: RunHolder,
176
+ styles: TextStyleLookup = createStyleBindingLookup(graph)
177
+ ): Pick<Partial<SceneNode>, 'styleRuns'> {
178
+ const resolved = resolvedRuns(graph, node, (run) => effectiveRunBoundVariables(run, styles))
179
+ const runs = resolved.styleRuns ?? node.styleRuns
180
+ let painted: StyleRun[] | undefined
181
+ for (const [index, run] of runs.entries()) {
182
+ const fills = refreshedRunFills(graph, node.id, run.style)
183
+ if (!fills) continue
184
+ painted ??= [...runs]
185
+ painted[index] = { ...run, style: { ...run.style, fills } }
186
+ }
187
+ return painted ? { styleRuns: painted } : resolved
188
+ }
189
+
190
+ function resolvedRuns(
191
+ graph: SceneGraph,
192
+ node: RunHolder,
193
+ runBindings: (run: StyleRun) => VariableBindings
129
194
  ): Pick<Partial<SceneNode>, 'styleRuns'> {
130
195
  const styleRuns = node.styleRuns.map((run): StyleRun => {
131
- const bound = run.style.boundVariables
132
- if (!bound) return run
196
+ const bound = runBindings(run)
133
197
  let style = run.style
134
198
  for (const field of NUMERIC_RUN_FIELDS) {
135
199
  const variableId = bound[field]
136
200
  if (!variableId) continue
137
201
  const value = graph.resolveNumberVariableForNode(node.id, variableId)
138
202
  if (value === undefined || !Number.isFinite(value)) continue
139
- const next = boundNumberValue(field, value)
203
+ const next = boundNumberValue(field, value * lengthScale(field, node.componentScale))
140
204
  if (isEqual(style[field], next)) continue
141
205
  style = { ...style, [field]: next }
142
206
  }
@@ -146,11 +210,26 @@ export function resolvedRunBindings(
146
210
  return changed ? { styleRuns } : {}
147
211
  }
148
212
 
149
- /** The variables a node binds, its own and its characters'. */
150
- function boundVariableIds(node: SceneNode): string[] {
213
+ export function resolvedTextStyleBindings(
214
+ graph: SceneGraph,
215
+ node: SceneNode,
216
+ styles: TextStyleLookup
217
+ ): Partial<SceneNode> {
218
+ if (node.type !== 'TEXT') {
219
+ return {}
220
+ }
221
+ const styleBound = inheritedTextStyleBindings(node, styles)
222
+ return {
223
+ ...resolvedNumbers(graph, node, styleBound, styleBound),
224
+ ...resolvedRuns(graph, node, (run) => inheritedRunStyleBindings(run, styles))
225
+ }
226
+ }
227
+
228
+ /** The variables a node binds: its own, its characters' and those its text styles bind. */
229
+ function boundVariableIds(node: SceneNode, styles: TextStyleLookup): string[] {
151
230
  return [
152
- ...Object.values(node.boundVariables),
153
- ...node.styleRuns.flatMap((run) => Object.values(run.style.boundVariables ?? {}))
231
+ ...Object.values(effectiveBoundVariables(node, styles)),
232
+ ...node.styleRuns.flatMap((run) => Object.values(effectiveRunBoundVariables(run, styles)))
154
233
  ].filter((id): id is string => typeof id === 'string')
155
234
  }
156
235
 
@@ -234,17 +313,25 @@ export function variablesResolvingThrough(graph: SceneGraph, ids: Iterable<strin
234
313
  * those the bound variables alias, so a mode set on the subtrees can change what they show.
235
314
  */
236
315
  export function collectionsBoundIn(graph: SceneGraph, roots: Iterable<string>): Set<string> {
237
- return collectionsResolving(graph, subtreeNodes(graph, roots).flatMap(boundVariableIds))
316
+ const styles = createStyleBindingLookup(graph)
317
+ return collectionsResolving(
318
+ graph,
319
+ subtreeNodes(graph, roots).flatMap((node) => boundVariableIds(node, styles))
320
+ )
238
321
  }
239
322
 
240
323
  /**
241
324
  * The layers a change to these variables reaches: those binding one of them, or a variable that
242
325
  * aliases one, on the layer itself or on its characters.
243
326
  */
244
- export function layersUsingVariables(graph: SceneGraph, ids: Iterable<string>): SceneNode[] {
327
+ export function layersUsingVariables(
328
+ graph: SceneGraph,
329
+ ids: Iterable<string>,
330
+ styles: TextStyleLookup = createStyleBindingLookup(graph)
331
+ ): SceneNode[] {
245
332
  const variables = variablesResolvingThrough(graph, ids)
246
333
  return [...graph.getAllNodes()].filter((node) =>
247
- boundVariableIds(node).some((id) => variables.has(id))
334
+ boundVariableIds(node, styles).some((id) => variables.has(id))
248
335
  )
249
336
  }
250
337
 
@@ -264,15 +351,139 @@ export function layersResolvingCollections(
264
351
  graph,
265
352
  inCollections.map((variable) => variable.id)
266
353
  )
354
+ const styles = createStyleBindingLookup(graph)
267
355
  return subtreeNodes(graph, roots).filter((node) =>
268
- boundVariableIds(node).some((id) => variables.has(id))
356
+ boundVariableIds(node, styles).some((id) => variables.has(id))
357
+ )
358
+ }
359
+
360
+ interface ScopedLayers {
361
+ nodes: Iterable<SceneNode>
362
+ restyled: ReadonlySet<string>
363
+ }
364
+
365
+ const NO_RESTYLED: ReadonlySet<string> = new Set()
366
+
367
+ function scopedLayers(
368
+ graph: SceneGraph,
369
+ scope: BindingScope | undefined,
370
+ styles: TextStyleLookup
371
+ ): ScopedLayers {
372
+ if (!scope) {
373
+ return { nodes: graph.getAllNodes(), restyled: NO_RESTYLED }
374
+ }
375
+ if ('subtrees' in scope) {
376
+ return withTextStyleConsumers(graph, subtreeNodes(graph, scope.subtrees))
377
+ }
378
+ return { nodes: layersUsingVariables(graph, scope.variables, styles), restyled: NO_RESTYLED }
379
+ }
380
+
381
+ function withTextStyleConsumers(graph: SceneGraph, nodes: SceneNode[]): ScopedLayers {
382
+ const styleIds = new Set<string>()
383
+ for (const node of nodes) {
384
+ if (node.sharedStyleType === 'TEXT' && node.source.id) {
385
+ styleIds.add(node.source.id)
386
+ }
387
+ }
388
+ if (styleIds.size === 0) {
389
+ return { nodes, restyled: NO_RESTYLED }
390
+ }
391
+ return { nodes: uniq([...nodes, ...textStyleConsumers(graph, styleIds)]), restyled: styleIds }
392
+ }
393
+
394
+ type NumericTextValues = Pick<CharacterStyleOverride, (typeof NUMERIC_RUN_FIELDS)[number]>
395
+
396
+ function unboundStyleValues(
397
+ styleValues: NumericTextValues,
398
+ current: NumericTextValues,
399
+ bound: VariableBindings
400
+ ): NumericTextValues | undefined {
401
+ let values: NumericTextValues | undefined
402
+ for (const field of NUMERIC_RUN_FIELDS) {
403
+ if (!(field in bound) && !isEqual(current[field], styleValues[field])) {
404
+ values = { ...values, [field]: styleValues[field] }
405
+ }
406
+ }
407
+ return values
408
+ }
409
+
410
+ function restyledValues(
411
+ graph: SceneGraph,
412
+ node: SceneNode,
413
+ styleId: string | null | undefined,
414
+ current: NumericTextValues,
415
+ bound: VariableBindings,
416
+ restyled: ReadonlySet<string>
417
+ ): NumericTextValues | undefined {
418
+ if (!styleId || !restyled.has(styleId)) {
419
+ return undefined
420
+ }
421
+ const style = graph.getSharedStyleNode(styleId, 'TEXT')
422
+ if (!style) {
423
+ return undefined
424
+ }
425
+ const styleValues = scaledTextStyleLengths(pick(style, NUMERIC_RUN_FIELDS), node.componentScale)
426
+ return unboundStyleValues(styleValues, current, bound)
427
+ }
428
+
429
+ function restyledRuns(
430
+ graph: SceneGraph,
431
+ node: SceneNode,
432
+ restyled: ReadonlySet<string>,
433
+ styles: TextStyleLookup
434
+ ): Pick<Partial<SceneNode>, 'styleRuns'> {
435
+ const styleRuns = node.styleRuns.map((run): StyleRun => {
436
+ const bound = effectiveRunBoundVariables(run, styles)
437
+ const values = restyledValues(graph, node, run.style.textStyleId, run.style, bound, restyled)
438
+ return values ? { ...run, style: { ...run.style, ...values } } : run
439
+ })
440
+ const changed = styleRuns.some((run, index) => run !== node.styleRuns[index])
441
+ return changed ? { styleRuns } : {}
442
+ }
443
+
444
+ function styleOwnValues(
445
+ graph: SceneGraph,
446
+ node: SceneNode,
447
+ restyled: ReadonlySet<string>,
448
+ styles: TextStyleLookup
449
+ ): Partial<SceneNode> {
450
+ if (restyled.size === 0) {
451
+ return {}
452
+ }
453
+ const bound = effectiveBoundVariables(node, styles)
454
+ return {
455
+ ...restyledValues(graph, node, node.textStyleId, node, bound, restyled),
456
+ ...restyledRuns(graph, node, restyled, styles)
457
+ }
458
+ }
459
+
460
+ function runReadsVariables({ style }: StyleRun): boolean {
461
+ return (
462
+ Boolean(style.textStyleId) ||
463
+ (style.boundVariables !== undefined && !isEmptyObject(style.boundVariables))
269
464
  )
270
465
  }
271
466
 
272
- function scopedNodes(graph: SceneGraph, scope: BindingScope | undefined): Iterable<SceneNode> {
273
- if (!scope) return graph.getAllNodes()
274
- if ('subtrees' in scope) return subtreeNodes(graph, scope.subtrees)
275
- return layersUsingVariables(graph, scope.variables)
467
+ function readsVariables(node: SceneNode): boolean {
468
+ return (
469
+ !isEmptyObject(node.boundVariables) ||
470
+ Boolean(node.textStyleId) ||
471
+ node.styleRuns.some(runReadsVariables)
472
+ )
473
+ }
474
+
475
+ function resolvedOwnBindings(
476
+ graph: SceneGraph,
477
+ node: SceneNode,
478
+ styleBound: VariableBindings,
479
+ styles: TextStyleLookup
480
+ ): Partial<SceneNode> {
481
+ const own = layerBindingsInEffect(node, styles)
482
+ return {
483
+ ...resolvedNumbers(graph, node, own, styleBound),
484
+ ...resolvedValueBindings(graph, node),
485
+ ...resolvedRunBindings(graph, node, styles)
486
+ }
276
487
  }
277
488
 
278
489
  /**
@@ -296,18 +507,31 @@ export function reconcileVariableBindingUpdates(
296
507
  scope?: BindingScope
297
508
  ): Map<string, Partial<SceneNode>> {
298
509
  const relaid = new Map<string, Partial<SceneNode>>()
299
- const nodes = [...scopedNodes(graph, scope)]
510
+ const styles = createStyleBindingLookup(graph)
511
+ const { nodes: scoped, restyled } = scopedLayers(graph, scope, styles)
512
+ const nodes = [...scoped]
300
513
  for (const node of nodes) refreshBoundPaints(graph, node)
301
514
  for (const node of nodes) {
302
- const layoutUpdates = {
303
- ...resolvedNumericBindings(graph, node),
304
- ...resolvedValueBindings(graph, node),
305
- ...resolvedRunBindings(graph, node)
515
+ if (!readsVariables(node)) {
516
+ continue
517
+ }
518
+ const unbound = styleOwnValues(graph, node, restyled, styles)
519
+ if (!isEmptyObject(unbound)) {
520
+ graph.updateNode(node.id, unbound)
521
+ }
522
+ const styleBound = inheritedTextStyleBindings(node, styles)
523
+ const resolved = {
524
+ ...resolvedNumbers(graph, node, styleBound, styleBound),
525
+ ...resolvedOwnBindings(graph, node, styleBound, styles)
526
+ }
527
+ const updates = { ...resolved, ...resolvedEffectBindings(graph, node) }
528
+ if (!isEmptyObject(updates)) {
529
+ graph.updateNode(node.id, updates)
530
+ }
531
+ const layoutUpdates = { ...unbound, ...resolved }
532
+ if (!isEmptyObject(layoutUpdates)) {
533
+ relaid.set(node.id, layoutUpdates)
306
534
  }
307
- const updates = { ...layoutUpdates, ...resolvedEffectBindings(graph, node) }
308
- if (isEmptyObject(updates)) continue
309
- graph.updateNode(node.id, updates)
310
- if (!isEmptyObject(layoutUpdates)) relaid.set(node.id, layoutUpdates)
311
535
  }
312
536
  return relaid
313
537
  }
@@ -0,0 +1,282 @@
1
+ import { isEqual } from 'es-toolkit/predicate'
2
+
3
+ import type { CubicBezier, Easing, EasingCurve, Spring, VariableValue } from '../types'
4
+
5
+ const EASE_IN_AND_OUT: CubicBezier = { x1: 0.42, y1: 0, x2: 0.58, y2: 1 }
6
+
7
+ /** The curve each cubic preset eases along, as Figma writes it beside the preset's name. */
8
+ const PRESET_BEZIERS: Partial<Record<EasingCurve, CubicBezier>> = {
9
+ // A new easing variable in Figma holds `IN_CUBIC` with no numbers, which its editor draws and
10
+ // names as Ease in.
11
+ IN_CUBIC: { x1: 0.42, y1: 0, x2: 1, y2: 1 },
12
+ EASE_IN: { x1: 0.42, y1: 0, x2: 1, y2: 1 },
13
+ OUT_CUBIC: { x1: 0, y1: 0, x2: 0.58, y2: 1 },
14
+ INOUT_CUBIC: EASE_IN_AND_OUT,
15
+ LINEAR: { x1: 0, y1: 0, x2: 1, y2: 1 },
16
+ IN_BACK_CUBIC: { x1: 0.3, y1: -0.05, x2: 0.7, y2: -0.5 },
17
+ OUT_BACK_CUBIC: { x1: 0.45, y1: 1.45, x2: 0.8, y2: 1 },
18
+ INOUT_BACK_CUBIC: { x1: 0.7, y1: -0.4, x2: 0.4, y2: 1.4 }
19
+ }
20
+
21
+ /** The spring each spring preset is. */
22
+ const PRESET_SPRINGS: Partial<Record<EasingCurve, Spring>> = {
23
+ GENTLE_SPRING: { stiffness: 100, damping: 15, mass: 1 },
24
+ SPRING_PRESET_ONE: { stiffness: 300, damping: 20, mass: 1 },
25
+ SPRING_PRESET_TWO: { stiffness: 600, damping: 15, mass: 1 },
26
+ SPRING_PRESET_THREE: { stiffness: 80, damping: 20, mass: 1 }
27
+ }
28
+
29
+ const SPRING_CURVES = new Set<EasingCurve>([
30
+ 'SPRING',
31
+ 'GENTLE_SPRING',
32
+ 'CUSTOM_SPRING',
33
+ 'SPRING_PRESET_ONE',
34
+ 'SPRING_PRESET_TWO',
35
+ 'SPRING_PRESET_THREE'
36
+ ])
37
+
38
+ /** The stiffness and mass a spring edited by its bounce keeps, as Figma keeps them. */
39
+ const BOUNCE_STIFFNESS = 100
40
+ const BOUNCE_MASS = 1
41
+
42
+ export function isEasing(value: VariableValue | undefined): value is Easing {
43
+ return typeof value === 'object' && 'easing' in value
44
+ }
45
+
46
+ export function isSpringCurve(curve: EasingCurve): boolean {
47
+ return SPRING_CURVES.has(curve)
48
+ }
49
+
50
+ /** The curve an easing follows: its own, or its preset's; springs and `HOLD` have none. */
51
+ export function easingBezier(easing: Easing): CubicBezier | undefined {
52
+ if (isSpringCurve(easing.easing)) return undefined
53
+ return easing.bezier ?? PRESET_BEZIERS[easing.easing]
54
+ }
55
+
56
+ /** The spring an easing is: its own, or its preset's, as a spring without numbers starts; curves have none. */
57
+ export function easingSpring(easing: Easing): Spring | undefined {
58
+ if (!isSpringCurve(easing.easing)) return undefined
59
+ return easing.spring ?? PRESET_SPRINGS[easing.easing] ?? springWithBounce(0)
60
+ }
61
+
62
+ /** The curves a curve's numbers name: the preset with exactly them, else a custom curve. */
63
+ const NAMED_CURVES: readonly EasingCurve[] = [
64
+ 'EASE_IN',
65
+ 'OUT_CUBIC',
66
+ 'INOUT_CUBIC',
67
+ 'LINEAR',
68
+ 'IN_BACK_CUBIC',
69
+ 'OUT_BACK_CUBIC',
70
+ 'INOUT_BACK_CUBIC'
71
+ ]
72
+
73
+ /** An easing along this curve: the preset with exactly these numbers, else a custom curve. */
74
+ export function easingForBezier(bezier: CubicBezier): Easing {
75
+ const preset = NAMED_CURVES.find((curve) => isEqual(PRESET_BEZIERS[curve], bezier))
76
+ return preset ? easingPreset(preset) : { easing: 'CUSTOM_CUBIC', bezier: { ...bezier } }
77
+ }
78
+
79
+ /** A preset with the numbers Figma writes beside it, or a custom curve or spring to start from. */
80
+ export function easingPreset(curve: EasingCurve): Easing {
81
+ if (curve === 'HOLD') return { easing: curve }
82
+ if (curve === 'CUSTOM_CUBIC') return { easing: curve, bezier: { ...EASE_IN_AND_OUT } }
83
+ if (curve === 'CUSTOM_SPRING' || curve === 'SPRING')
84
+ return { easing: curve, spring: springWithBounce(0) }
85
+ const spring = PRESET_SPRINGS[curve]
86
+ if (spring) return { easing: curve, spring: { ...spring } }
87
+ const bezier = PRESET_BEZIERS[curve]
88
+ return bezier ? { easing: curve, bezier: { ...bezier } } : { easing: curve }
89
+ }
90
+
91
+ /**
92
+ * How far a spring overshoots, from 0 (none) to 1 (it never settles): one less its damping
93
+ * ratio, as Figma's bounce is.
94
+ */
95
+ export function springBounce(spring: Spring): number {
96
+ const critical = 2 * Math.sqrt(spring.stiffness * spring.mass)
97
+ return critical > 0 ? Math.max(0, 1 - spring.damping / critical) : 0
98
+ }
99
+
100
+ /** The spring with this bounce, at the stiffness and mass Figma gives a spring edited by bounce. */
101
+ export function springWithBounce(bounce: number): Spring {
102
+ const critical = 2 * Math.sqrt(BOUNCE_STIFFNESS * BOUNCE_MASS)
103
+ const clamped = Math.min(1, Math.max(0, bounce))
104
+ return { stiffness: BOUNCE_STIFFNESS, damping: critical * (1 - clamped), mass: BOUNCE_MASS }
105
+ }
106
+
107
+ /** The names Figma gives its presets. */
108
+ const PRESET_NAMES: Partial<Record<EasingCurve, string>> = {
109
+ IN_CUBIC: 'Ease in',
110
+ EASE_IN: 'Ease in',
111
+ OUT_CUBIC: 'Ease out',
112
+ INOUT_CUBIC: 'Ease in and out',
113
+ LINEAR: 'Linear',
114
+ IN_BACK_CUBIC: 'Ease in back',
115
+ OUT_BACK_CUBIC: 'Ease out back',
116
+ INOUT_BACK_CUBIC: 'Ease in and out back',
117
+ GENTLE_SPRING: 'Gentle',
118
+ SPRING_PRESET_ONE: 'Quick',
119
+ SPRING_PRESET_TWO: 'Bouncy',
120
+ SPRING_PRESET_THREE: 'Slow',
121
+ HOLD: 'Hold'
122
+ }
123
+
124
+ function round(value: number, digits = 2): string {
125
+ return String(Number(value.toFixed(digits)))
126
+ }
127
+
128
+ /**
129
+ * An easing as a line of text, as Figma's variables table shows it: a preset by name, a custom
130
+ * curve as `cubic-bezier(…)`, a spring by its bounce, `spring(0.4)`, or, one edited by its
131
+ * physics, `physical-spring(stiffness, damping, mass)`.
132
+ */
133
+ export function easingText(easing: Easing): string {
134
+ const preset = PRESET_NAMES[easing.easing]
135
+ if (preset) return preset
136
+ const spring = easingSpring(easing)
137
+ if (spring?.type === 'PHYSICAL')
138
+ return `physical-spring(${[spring.stiffness, spring.damping, spring.mass].map((value) => round(value)).join(', ')})`
139
+ if (spring) return `spring(${round(springBounce(spring))})`
140
+ const bezier = easingBezier(easing)
141
+ if (!bezier) return easing.easing
142
+ return `cubic-bezier(${[bezier.x1, bezier.y1, bezier.x2, bezier.y2].map((value) => round(value)).join(', ')})`
143
+ }
144
+
145
+ /** How close to its target a spring counts as settled. */
146
+ const SETTLED = 0.001
147
+ /** The longest a spring is followed, for one that barely damps. */
148
+ const MAX_SPRING_SECONDS = 10
149
+
150
+ function springMotion(spring: Spring) {
151
+ const natural = Math.sqrt(spring.stiffness / spring.mass)
152
+ const ratio = spring.damping / (2 * Math.sqrt(spring.stiffness * spring.mass))
153
+ return { natural, ratio }
154
+ }
155
+
156
+ /** Where a spring released at rest from 0 towards 1 is after `seconds`. */
157
+ export function springPosition(spring: Spring, seconds: number): number {
158
+ const { natural, ratio } = springMotion(spring)
159
+ if (!(natural > 0)) return 1
160
+ if (ratio < 1) {
161
+ const damped = natural * Math.sqrt(1 - ratio * ratio)
162
+ const decay = Math.exp(-ratio * natural * seconds)
163
+ return (
164
+ 1 -
165
+ decay *
166
+ (Math.cos(damped * seconds) + ((ratio * natural) / damped) * Math.sin(damped * seconds))
167
+ )
168
+ }
169
+ if (ratio === 1) return 1 - (1 + natural * seconds) * Math.exp(-natural * seconds)
170
+ const spread = natural * Math.sqrt(ratio * ratio - 1)
171
+ const slow = -ratio * natural + spread
172
+ const fast = -ratio * natural - spread
173
+ return (
174
+ 1 +
175
+ (-fast / (fast - slow)) * Math.exp(slow * seconds) +
176
+ (slow / (fast - slow)) * Math.exp(fast * seconds)
177
+ )
178
+ }
179
+
180
+ /** How long a spring takes to come within a thousandth of its target and stay there. */
181
+ export function springSettleTime(spring: Spring): number {
182
+ const { natural, ratio } = springMotion(spring)
183
+ if (!(natural > 0) || !(ratio > 0)) return MAX_SPRING_SECONDS
184
+ return Math.min(MAX_SPRING_SECONDS, settleBound(natural, ratio))
185
+ }
186
+
187
+ /**
188
+ * When the response's bound, its decay times the largest its oscillation or second term can add,
189
+ * falls under `SETTLED`: the decay alone undershoots for a spring damped close to critical.
190
+ */
191
+ function settleBound(natural: number, ratio: number): number {
192
+ const tolerance = Math.log(1 / SETTLED)
193
+ if (ratio < 1) {
194
+ // The oscillation's amplitude is 1 / sqrt(1 - ratio²) of the decay.
195
+ const amplitude = 1 / Math.sqrt(1 - ratio * ratio)
196
+ return (tolerance + Math.log(amplitude)) / (ratio * natural)
197
+ }
198
+ if (ratio > 1) {
199
+ const spread = natural * Math.sqrt(ratio * ratio - 1)
200
+ const slow = -ratio * natural + spread
201
+ const fast = -ratio * natural - spread
202
+ // Both terms decay at least as fast as the slow root; their weights add at most this much.
203
+ const weight = (Math.abs(fast) + Math.abs(slow)) / (slow - fast)
204
+ return (tolerance + Math.log(weight)) / -slow
205
+ }
206
+ // Critically damped: 1 - x = (1 + ωt) e^(-ωt); a few fixed-point steps solve it for SETTLED.
207
+ let seconds = tolerance / natural
208
+ for (let step = 0; step < 8; step++)
209
+ seconds = (tolerance + Math.log(1 + natural * seconds)) / natural
210
+ return seconds
211
+ }
212
+
213
+ /** The first overshoot of a bouncing spring: when it comes, and how far past 1 it goes. */
214
+ export function springPeak(spring: Spring): { seconds: number; value: number } | undefined {
215
+ const { natural, ratio } = springMotion(spring)
216
+ if (!(natural > 0) || ratio >= 1) return undefined
217
+ const root = Math.sqrt(1 - ratio * ratio)
218
+ return {
219
+ seconds: Math.PI / (natural * root),
220
+ value: 1 + Math.exp((-ratio * Math.PI) / root)
221
+ }
222
+ }
223
+
224
+ /** The bounce whose spring overshoots its target by `overshoot` (0 to 1) at its first peak. */
225
+ export function bounceForOvershoot(overshoot: number): number {
226
+ if (overshoot <= 0) return 0
227
+ if (overshoot >= 1) return 1
228
+ const log = Math.log(overshoot)
229
+ return 1 - -log / Math.hypot(Math.PI, log)
230
+ }
231
+
232
+ /** Where a cubic bezier curve is at `progress` (0 to 1) along its time axis. */
233
+ export function bezierPosition(bezier: CubicBezier, progress: number): number {
234
+ if (progress <= 0) return 0
235
+ if (progress >= 1) return 1
236
+ const axis = (a: number, b: number, t: number) =>
237
+ 3 * (1 - t) * (1 - t) * t * a + 3 * (1 - t) * t * t * b + t * t * t
238
+ const slope = (a: number, b: number, t: number) =>
239
+ 3 * (1 - t) * (1 - t) * a + 6 * (1 - t) * t * (b - a) + 3 * t * t * (1 - b)
240
+ // Newton's method finds the curve's parameter for this time quickly; halving keeps it safe
241
+ // where the curve is nearly flat in time.
242
+ let t = progress
243
+ for (let step = 0; step < 8; step++) {
244
+ const error = axis(bezier.x1, bezier.x2, t) - progress
245
+ const rate = slope(bezier.x1, bezier.x2, t)
246
+ if (Math.abs(error) < 1e-7) return axis(bezier.y1, bezier.y2, t)
247
+ if (Math.abs(rate) < 1e-6) break
248
+ t -= error / rate
249
+ }
250
+ let low = 0
251
+ let high = 1
252
+ t = progress
253
+ for (let step = 0; step < 40; step++) {
254
+ const x = axis(bezier.x1, bezier.x2, t)
255
+ if (Math.abs(x - progress) < 1e-7) break
256
+ if (x < progress) low = t
257
+ else high = t
258
+ t = (low + high) / 2
259
+ }
260
+ return axis(bezier.y1, bezier.y2, t)
261
+ }
262
+
263
+ /** How long an easing plays: a spring settles in its own time, as Figma's do; others take `duration`. */
264
+ export function easingSeconds(easing: Easing, duration: number): number {
265
+ const spring = easingSpring(easing)
266
+ return spring ? springSettleTime(spring) : Math.max(0, duration)
267
+ }
268
+
269
+ /**
270
+ * Where an easing is `seconds` into a motion of `duration`, from 0 to 1; a spring may pass 1 on
271
+ * its way. `HOLD` stays at 0 until the end.
272
+ */
273
+ export function easingPosition(easing: Easing, seconds: number, duration: number): number {
274
+ const total = easingSeconds(easing, duration)
275
+ if (seconds >= total) return 1
276
+ if (seconds <= 0) return 0
277
+ const spring = easingSpring(easing)
278
+ if (spring) return springPosition(spring, seconds)
279
+ const bezier = easingBezier(easing)
280
+ if (bezier) return bezierPosition(bezier, seconds / total)
281
+ return easing.easing === 'HOLD' ? 0 : seconds / total
282
+ }