@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.
- package/dist/behaviours/index.d.ts +3 -2
- package/dist/behaviours/index.js +2 -1
- package/dist/behaviours/kinds.d.ts.map +1 -1
- package/dist/behaviours/kinds.js +15 -8
- package/dist/behaviours/kinds.js.map +1 -1
- package/dist/behaviours/model.d.ts +2 -2
- package/dist/behaviours/model.d.ts.map +1 -1
- package/dist/behaviours/model.js.map +1 -1
- package/dist/behaviours/schema.d.ts +136 -1
- package/dist/behaviours/schema.d.ts.map +1 -1
- package/dist/behaviours/schema.js +17 -2
- package/dist/behaviours/schema.js.map +1 -1
- package/dist/behaviours/spec.d.ts.map +1 -1
- package/dist/behaviours/spec.js +4 -0
- package/dist/behaviours/spec.js.map +1 -1
- package/dist/behaviours/transition.d.ts +23 -0
- package/dist/behaviours/transition.d.ts.map +1 -0
- package/dist/behaviours/transition.js +36 -0
- package/dist/behaviours/transition.js.map +1 -0
- package/dist/checkpoint.d.ts.map +1 -1
- package/dist/checkpoint.js +2 -0
- package/dist/checkpoint.js.map +1 -1
- package/dist/color/index.d.ts +12 -9
- package/dist/color/index.d.ts.map +1 -1
- package/dist/color/index.js +15 -25
- package/dist/color/index.js.map +1 -1
- package/dist/color/management.d.ts +51 -23
- package/dist/color/management.d.ts.map +1 -1
- package/dist/color/management.js +154 -149
- package/dist/color/management.js.map +1 -1
- package/dist/color/okhcl.d.ts +28 -10
- package/dist/color/okhcl.d.ts.map +1 -1
- package/dist/color/okhcl.js +56 -40
- package/dist/color/okhcl.js.map +1 -1
- package/dist/color/schema.d.ts +6 -1
- package/dist/color/schema.d.ts.map +1 -1
- package/dist/color/schema.js +10 -1
- package/dist/color/schema.js.map +1 -1
- package/dist/constraints.d.ts.map +1 -1
- package/dist/constraints.js +2 -1
- package/dist/constraints.js.map +1 -1
- package/dist/copy.d.ts +7 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +22 -1
- package/dist/copy.js.map +1 -1
- package/dist/corners.d.ts +3 -1
- package/dist/corners.d.ts.map +1 -1
- package/dist/corners.js +3 -1
- package/dist/corners.js.map +1 -1
- package/dist/css/grid.js +1 -1
- package/dist/css/index.d.ts +2 -1
- package/dist/css/index.js +3 -2
- package/dist/css/motion.d.ts +12 -0
- package/dist/css/motion.d.ts.map +1 -0
- package/dist/css/motion.js +60 -0
- package/dist/css/motion.js.map +1 -0
- package/dist/css/shadows.d.ts +4 -3
- package/dist/css/shadows.d.ts.map +1 -1
- package/dist/css/shadows.js +7 -6
- package/dist/css/shadows.js.map +1 -1
- package/dist/css/values.d.ts +3 -2
- package/dist/css/values.d.ts.map +1 -1
- package/dist/css/values.js +4 -4
- package/dist/css/values.js.map +1 -1
- package/dist/fields/index.d.ts +2 -2
- package/dist/fields/index.js +2 -2
- package/dist/fields/layout.d.ts +3 -1
- package/dist/fields/layout.d.ts.map +1 -1
- package/dist/fields/layout.js +9 -1
- package/dist/fields/layout.js.map +1 -1
- package/dist/figjam/palette.js +1 -1
- package/dist/font-style.d.ts.map +1 -1
- package/dist/font-style.js +7 -3
- package/dist/font-style.js.map +1 -1
- package/dist/generated/index.d.ts +6 -2
- package/dist/generated/index.d.ts.map +1 -1
- package/dist/generated/index.js +14 -4
- package/dist/generated/index.js.map +1 -1
- package/dist/geometry.d.ts +6 -1
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +10 -2
- package/dist/geometry.js.map +1 -1
- package/dist/graph.d.ts +29 -4
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +59 -9
- package/dist/graph.js.map +1 -1
- package/dist/group-bounds.d.ts +5 -2
- package/dist/group-bounds.d.ts.map +1 -1
- package/dist/group-bounds.js +38 -8
- package/dist/group-bounds.js.map +1 -1
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +19 -8
- package/dist/hit-test.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/instances/content-size.js +1 -1
- package/dist/instances/fields.d.ts +2 -2
- package/dist/instances/fields.d.ts.map +1 -1
- package/dist/instances/fields.js +2 -0
- package/dist/instances/fields.js.map +1 -1
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +50 -11
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/sync.js +67 -7
- package/dist/instances/sync.js.map +1 -1
- package/dist/layout/flow.d.ts +16 -0
- package/dist/layout/flow.d.ts.map +1 -0
- package/dist/layout/flow.js +34 -0
- package/dist/layout/flow.js.map +1 -0
- package/dist/layout/index.d.ts +4 -3
- package/dist/layout/index.js +4 -3
- package/dist/layout/keywords.d.ts.map +1 -1
- package/dist/layout/keywords.js +7 -1
- package/dist/layout/keywords.js.map +1 -1
- package/dist/layout/sizing.d.ts +9 -1
- package/dist/layout/sizing.d.ts.map +1 -1
- package/dist/layout/sizing.js +17 -1
- package/dist/layout/sizing.js.map +1 -1
- package/dist/layout/spacing.d.ts +7 -1
- package/dist/layout/spacing.d.ts.map +1 -1
- package/dist/layout/spacing.js +10 -1
- package/dist/layout/spacing.js.map +1 -1
- package/dist/mesh/geometry.d.ts +83 -0
- package/dist/mesh/geometry.d.ts.map +1 -0
- package/dist/mesh/geometry.js +441 -0
- package/dist/mesh/geometry.js.map +1 -0
- package/dist/mesh/index.d.ts +4 -0
- package/dist/mesh/index.js +4 -0
- package/dist/mesh/model.d.ts +28 -0
- package/dist/mesh/model.d.ts.map +1 -0
- package/dist/mesh/model.js +40 -0
- package/dist/mesh/model.js.map +1 -0
- package/dist/mesh/schema.d.ts +191 -0
- package/dist/mesh/schema.d.ts.map +1 -0
- package/dist/mesh/schema.js +56 -0
- package/dist/mesh/schema.js.map +1 -0
- package/dist/mutation-impact.d.ts +2 -0
- package/dist/mutation-impact.d.ts.map +1 -1
- package/dist/mutation-impact.js +18 -7
- package/dist/mutation-impact.js.map +1 -1
- package/dist/node-defaults.d.ts +3 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +22 -6
- package/dist/node-defaults.js.map +1 -1
- package/dist/paints/index.d.ts +5 -3
- package/dist/paints/index.js +5 -3
- package/dist/paints/pattern.d.ts +5 -1
- package/dist/paints/pattern.d.ts.map +1 -1
- package/dist/paints/pattern.js +33 -1
- package/dist/paints/pattern.js.map +1 -1
- package/dist/paints/shows.d.ts +4 -1
- package/dist/paints/shows.d.ts.map +1 -1
- package/dist/paints/shows.js +8 -1
- package/dist/paints/shows.js.map +1 -1
- package/dist/paints/solid.d.ts +11 -0
- package/dist/paints/solid.d.ts.map +1 -0
- package/dist/paints/solid.js +18 -0
- package/dist/paints/solid.js.map +1 -0
- package/dist/paints/stills.d.ts +74 -0
- package/dist/paints/stills.d.ts.map +1 -0
- package/dist/paints/stills.js +113 -0
- package/dist/paints/stills.js.map +1 -0
- package/dist/plugin-data/field.d.ts.map +1 -1
- package/dist/plugin-data/field.js +2 -0
- package/dist/plugin-data/field.js.map +1 -1
- package/dist/plugin-data/fields.d.ts +67 -0
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +12 -7
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/resize.d.ts +10 -3
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +34 -3
- package/dist/resize.js.map +1 -1
- package/dist/scaling/index.d.ts +2 -2
- package/dist/scaling/index.js +2 -2
- package/dist/scaling/node.d.ts +2 -1
- package/dist/scaling/node.d.ts.map +1 -1
- package/dist/scaling/node.js +27 -7
- package/dist/scaling/node.js.map +1 -1
- package/dist/shaders/index.d.ts +2 -3
- package/dist/shaders/index.js +2 -3
- package/dist/shaders/model.d.ts +3 -11
- package/dist/shaders/model.d.ts.map +1 -1
- package/dist/shaders/model.js +13 -36
- package/dist/shaders/model.js.map +1 -1
- package/dist/shaders/schema.d.ts +1 -1
- package/dist/shaders/schema.js +1 -1
- package/dist/shaders/schema.js.map +1 -1
- package/dist/shape-with-text/document.js +276 -0
- package/dist/shape-with-text/document.js.map +1 -0
- package/dist/shape-with-text/geometry.d.ts +13 -1
- package/dist/shape-with-text/geometry.d.ts.map +1 -1
- package/dist/shape-with-text/geometry.js +103 -41
- package/dist/shape-with-text/geometry.js.map +1 -1
- package/dist/shape-with-text/index.d.ts +2 -2
- package/dist/shape-with-text/index.js +2 -2
- package/dist/shared-style-index.d.ts +21 -0
- package/dist/shared-style-index.d.ts.map +1 -0
- package/dist/shared-style-index.js +72 -0
- package/dist/shared-style-index.js.map +1 -0
- package/dist/shared-styles.d.ts +3 -3
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +16 -7
- package/dist/shared-styles.js.map +1 -1
- package/dist/slides/drop.d.ts +32 -0
- package/dist/slides/drop.d.ts.map +1 -0
- package/dist/slides/drop.js +81 -0
- package/dist/slides/drop.js.map +1 -0
- package/dist/slides/index.d.ts +56 -0
- package/dist/slides/index.d.ts.map +1 -0
- package/dist/slides/index.js +134 -0
- package/dist/slides/index.js.map +1 -0
- package/dist/slides/motion.d.ts +55 -0
- package/dist/slides/motion.d.ts.map +1 -0
- package/dist/slides/motion.js +142 -0
- package/dist/slides/motion.js.map +1 -0
- package/dist/slides/playback.d.ts +59 -0
- package/dist/slides/playback.d.ts.map +1 -0
- package/dist/slides/playback.js +188 -0
- package/dist/slides/playback.js.map +1 -0
- package/dist/slides/types.d.ts +62 -0
- package/dist/slides/types.d.ts.map +1 -0
- package/dist/slides/types.js +39 -0
- package/dist/slides/types.js.map +1 -0
- package/dist/statuses/workflow.js +1 -1
- package/dist/text/direction.d.ts +6 -3
- package/dist/text/direction.d.ts.map +1 -1
- package/dist/text/direction.js +12 -12
- package/dist/text/direction.js.map +1 -1
- package/dist/text/index.d.ts +2 -1
- package/dist/text/index.js +3 -2
- package/dist/text/name.d.ts +22 -0
- package/dist/text/name.d.ts.map +1 -0
- package/dist/text/name.js +38 -0
- package/dist/text/name.js.map +1 -0
- package/dist/text/references.d.ts.map +1 -1
- package/dist/text/references.js +1 -2
- package/dist/text/references.js.map +1 -1
- package/dist/thumbnail.d.ts +23 -0
- package/dist/thumbnail.d.ts.map +1 -0
- package/dist/thumbnail.js +47 -0
- package/dist/thumbnail.js.map +1 -0
- package/dist/transfer/apply.d.ts.map +1 -1
- package/dist/transfer/apply.js +14 -3
- package/dist/transfer/apply.js.map +1 -1
- package/dist/transfer/copied.d.ts +6 -1
- package/dist/transfer/copied.d.ts.map +1 -1
- package/dist/transfer/copied.js +2 -2
- package/dist/transfer/copied.js.map +1 -1
- package/dist/transfer/dependencies.d.ts +4 -1
- package/dist/transfer/dependencies.d.ts.map +1 -1
- package/dist/transfer/dependencies.js +10 -1
- package/dist/transfer/dependencies.js.map +1 -1
- package/dist/transfer/history.js +2 -0
- package/dist/transfer/history.js.map +1 -1
- package/dist/transfer/identity.js +18 -3
- package/dist/transfer/identity.js.map +1 -1
- package/dist/transfer/index.d.ts +2 -1
- package/dist/transfer/index.js +4 -3
- package/dist/transfer/library.d.ts +45 -0
- package/dist/transfer/library.d.ts.map +1 -0
- package/dist/transfer/library.js +102 -0
- package/dist/transfer/library.js.map +1 -0
- package/dist/transfer/nodes.d.ts.map +1 -1
- package/dist/transfer/nodes.js +9 -0
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/transfer/plan.d.ts +9 -0
- package/dist/transfer/plan.d.ts.map +1 -1
- package/dist/transfer/plan.js +51 -6
- package/dist/transfer/plan.js.map +1 -1
- package/dist/transfer/remove.js +7 -1
- package/dist/transfer/remove.js.map +1 -1
- package/dist/transfer/validation.js +8 -4
- package/dist/transfer/validation.js.map +1 -1
- package/dist/transfer/variables.d.ts.map +1 -1
- package/dist/transfer/variables.js +2 -1
- package/dist/transfer/variables.js.map +1 -1
- package/dist/types.d.ts +90 -12
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +33 -1
- package/dist/types.js.map +1 -1
- package/dist/variables/bindings.d.ts +11 -6
- package/dist/variables/bindings.d.ts.map +1 -1
- package/dist/variables/bindings.js +162 -38
- package/dist/variables/bindings.js.map +1 -1
- package/dist/variables/easing.d.ts +48 -0
- package/dist/variables/easing.d.ts.map +1 -0
- package/dist/variables/easing.js +321 -0
- package/dist/variables/easing.js.map +1 -0
- package/dist/variables/formula/index.d.ts +4 -2
- package/dist/variables/formula/index.d.ts.map +1 -1
- package/dist/variables/formula/index.js +26 -9
- package/dist/variables/formula/index.js.map +1 -1
- package/dist/variables/formula/parse.js +1 -1
- package/dist/variables/formula/schema.js +1 -1
- package/dist/variables/formula/signatures.d.ts +2 -2
- package/dist/variables/formula/signatures.d.ts.map +1 -1
- package/dist/variables/formula/signatures.js.map +1 -1
- package/dist/variables/formula/tree.d.ts +2 -2
- package/dist/variables/formula/tree.d.ts.map +1 -1
- package/dist/variables/formula/tree.js.map +1 -1
- package/dist/variables/index.d.ts +22 -5
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +67 -17
- package/dist/variables/index.js.map +1 -1
- package/dist/variables/lookup.js +1 -1
- package/dist/variables/lookup.js.map +1 -1
- package/dist/variables/motion.d.ts +46 -0
- package/dist/variables/motion.d.ts.map +1 -0
- package/dist/variables/motion.js +107 -0
- package/dist/variables/motion.js.map +1 -0
- package/dist/variables/paints.d.ts +10 -2
- package/dist/variables/paints.d.ts.map +1 -1
- package/dist/variables/paints.js +24 -10
- package/dist/variables/paints.js.map +1 -1
- package/dist/variables/resolution.d.ts.map +1 -1
- package/dist/variables/resolution.js +10 -9
- package/dist/variables/resolution.js.map +1 -1
- package/dist/variables/schema.d.ts +59 -1
- package/dist/variables/schema.d.ts.map +1 -1
- package/dist/variables/schema.js +36 -1
- package/dist/variables/schema.js.map +1 -1
- package/dist/variables/style-bindings.d.ts +23 -0
- package/dist/variables/style-bindings.d.ts.map +1 -0
- package/dist/variables/style-bindings.js +83 -0
- package/dist/variables/style-bindings.js.map +1 -0
- package/package.json +21 -2
- package/src/behaviours/index.ts +1 -0
- package/src/behaviours/kinds.ts +5 -1
- package/src/behaviours/model.ts +2 -2
- package/src/behaviours/schema.ts +19 -2
- package/src/behaviours/spec.ts +5 -0
- package/src/behaviours/transition.ts +46 -0
- package/src/checkpoint.ts +2 -0
- package/src/color/index.ts +15 -24
- package/src/color/management.ts +144 -168
- package/src/color/okhcl.ts +62 -43
- package/src/color/schema.ts +13 -0
- package/src/constraints.ts +2 -1
- package/src/copy.ts +29 -0
- package/src/corners.ts +3 -0
- package/src/css/index.ts +1 -0
- package/src/css/motion.ts +51 -0
- package/src/css/shadows.ts +7 -6
- package/src/css/values.ts +7 -3
- package/src/fields/layout.ts +9 -0
- package/src/font-style.ts +8 -3
- package/src/generated/index.ts +15 -3
- package/src/geometry.ts +13 -1
- package/src/graph.ts +72 -9
- package/src/group-bounds.ts +54 -13
- package/src/hit-test.ts +13 -8
- package/src/instances/fields.ts +2 -0
- package/src/instances/index.ts +71 -12
- package/src/instances/sync.ts +93 -11
- package/src/layout/flow.ts +34 -0
- package/src/layout/index.ts +1 -0
- package/src/layout/keywords.ts +7 -1
- package/src/layout/sizing.ts +30 -0
- package/src/layout/spacing.ts +12 -0
- package/src/mesh/geometry.ts +433 -0
- package/src/mesh/index.ts +3 -0
- package/src/mesh/model.ts +50 -0
- package/src/mesh/schema.ts +81 -0
- package/src/mutation-impact.ts +23 -7
- package/src/node-defaults.ts +22 -5
- package/src/paints/index.ts +2 -0
- package/src/paints/pattern.ts +55 -0
- package/src/paints/shows.ts +9 -0
- package/src/paints/solid.ts +10 -0
- package/src/paints/stills.ts +182 -0
- package/src/plugin-data/field.ts +3 -0
- package/src/plugin-data/fields.ts +15 -2
- package/src/resize.ts +32 -20
- package/src/scaling/index.ts +1 -1
- package/src/scaling/node.ts +43 -6
- package/src/shaders/index.ts +0 -1
- package/src/shaders/model.ts +19 -49
- package/src/shaders/schema.ts +1 -1
- package/src/shape-with-text/document.ts +175 -0
- package/src/shape-with-text/geometry.ts +99 -27
- package/src/shared-style-index.ts +106 -0
- package/src/shared-styles.ts +21 -14
- package/src/slides/drop.ts +94 -0
- package/src/slides/index.ts +125 -0
- package/src/slides/motion.ts +169 -0
- package/src/slides/playback.ts +200 -0
- package/src/slides/types.ts +94 -0
- package/src/text/direction.ts +13 -23
- package/src/text/index.ts +1 -0
- package/src/text/name.ts +39 -0
- package/src/text/references.ts +3 -3
- package/src/thumbnail.ts +52 -0
- package/src/transfer/apply.ts +16 -3
- package/src/transfer/copied.ts +7 -2
- package/src/transfer/dependencies.ts +10 -1
- package/src/transfer/history.ts +2 -0
- package/src/transfer/identity.ts +34 -2
- package/src/transfer/index.ts +6 -0
- package/src/transfer/library.ts +153 -0
- package/src/transfer/nodes.ts +20 -0
- package/src/transfer/plan.ts +96 -10
- package/src/transfer/remove.ts +6 -2
- package/src/transfer/validation.ts +15 -7
- package/src/transfer/variables.ts +8 -2
- package/src/types.ts +114 -10
- package/src/variables/bindings.ts +260 -36
- package/src/variables/easing.ts +282 -0
- package/src/variables/formula/index.ts +33 -14
- package/src/variables/formula/signatures.ts +6 -2
- package/src/variables/formula/tree.ts +3 -3
- package/src/variables/index.ts +90 -37
- package/src/variables/lookup.ts +1 -1
- package/src/variables/motion.ts +151 -0
- package/src/variables/paints.ts +29 -7
- package/src/variables/resolution.ts +11 -5
- package/src/variables/schema.ts +41 -0
- package/src/variables/style-bindings.ts +137 -0
- package/dist/shaders/still.d.ts +0 -18
- package/dist/shaders/still.d.ts.map +0 -1
- package/dist/shaders/still.js +0 -36
- package/dist/shaders/still.js.map +0 -1
- 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(
|
|
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(
|
|
80
|
-
if (!
|
|
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
|
|
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
|
-
|
|
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
|
|
124
|
-
* what its characters hold. Bound font families and
|
|
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:
|
|
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
|
|
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
|
-
|
|
150
|
-
|
|
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
|
|
153
|
-
...node.styleRuns.flatMap((run) => Object.values(run
|
|
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
|
-
|
|
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(
|
|
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
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
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
|
|
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
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
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
|
+
}
|