@open-pencil/scene-graph 0.17.0-beta.7 → 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 +3 -2
- 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 +8 -18
- package/dist/css/shadows.js.map +1 -1
- package/dist/css/values.d.ts +6 -3
- package/dist/css/values.d.ts.map +1 -1
- package/dist/css/values.js +23 -5
- 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 +91 -13
- 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 +2 -0
- package/src/css/motion.ts +51 -0
- package/src/css/shadows.ts +8 -23
- package/src/css/values.ts +34 -4
- 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 +115 -11
- 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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sizing.js","names":[],"sources":["../../src/layout/sizing.ts"],"sourcesContent":["import type { SceneGraph } from '../graph'\nimport type { LayoutSizing, SceneNode, TextAutoResize } from '../types'\n\n/**\n * How a node sizes along one screen axis, as Figma's `layoutSizingHorizontal` and\n * `layoutSizingVertical` report it. Fill is stored on the child, the way Figma stores it:\n * along the parent's primary axis as `layoutGrow`, across it as `layoutAlignSelf: 'STRETCH'`.\n * A grid lays its children out like a horizontal stack. Hug is stored on the node itself:\n * `primaryAxisSizing` and `counterAxisSizing` for an auto-layout frame, `textAutoResize`\n * for text.\n */\nexport type LayoutSizingAxis = 'HORIZONTAL' | 'VERTICAL'\n\ntype AutoLayoutMode = Exclude<SceneNode['layoutMode'], 'NONE'>\n\nfunction primaryAxis(mode: AutoLayoutMode): LayoutSizingAxis {\n return mode === 'VERTICAL' ? 'VERTICAL' : 'HORIZONTAL'\n}\n\n/** The auto-layout parent that sizes a node's fill, or `undefined` when nothing does. */\nfunction fillingParent(parent: SceneNode | undefined, node: SceneNode): SceneNode | undefined {\n if (!parent || parent.layoutMode === 'NONE' || node.layoutPositioning === 'ABSOLUTE')\n return undefined\n return parent\n}\n\nfunction graphParent(graph: SceneGraph, node: SceneNode): SceneNode | undefined {\n return node.parentId ? graph.getNode(node.parentId) : undefined\n}\n\nfunction fills(parent: SceneNode, node: SceneNode, axis: LayoutSizingAxis): boolean {\n if (parent.layoutMode === 'NONE') return false\n if (axis === primaryAxis(parent.layoutMode)) return node.layoutGrow > 0\n if (node.layoutAlignSelf === 'STRETCH') return true\n return (\n node.layoutAlignSelf === 'AUTO' &&\n parent.layoutMode !== 'GRID' &&\n parent.counterAxisAlign === 'STRETCH'\n )\n}\n\nfunction ownSizingField(\n node: SceneNode,\n axis: LayoutSizingAxis\n): 'primaryAxisSizing' | 'counterAxisSizing' | undefined {\n if (node.layoutMode === 'NONE') return undefined\n return axis === primaryAxis(node.layoutMode) ? 'primaryAxisSizing' : 'counterAxisSizing'\n}\n\nfunction textHugs(autoResize: TextAutoResize, axis: LayoutSizingAxis): boolean {\n if (autoResize === 'WIDTH_AND_HEIGHT') return true\n return axis === 'VERTICAL' && autoResize === 'HEIGHT'\n}\n\nfunction hugs(node: SceneNode, axis: LayoutSizingAxis): boolean {\n if (node.type === 'TEXT') return textHugs(node.textAutoResize, axis)\n const field = ownSizingField(node, axis)\n return field !== undefined && node[field] === 'HUG'\n}\n\n/** The node's sizing along `axis`: fill from its parent, else its own hug, else fixed. */\nexport function layoutSizing(\n graph: SceneGraph,\n node: SceneNode,\n axis: LayoutSizingAxis\n): LayoutSizing {\n return layoutSizingInParent(graphParent(graph, node), node, axis)\n}\n\n/** `layoutSizing` for code that holds the parent rather than the graph. */\nexport function layoutSizingInParent(\n parentNode: SceneNode | undefined,\n node: SceneNode,\n axis: LayoutSizingAxis\n): LayoutSizing {\n const parent = fillingParent(parentNode, node)\n if (parent && fills(parent, node, axis)) return 'FILL'\n // Figma reports text outside auto-layout as fixed, though it still resizes to its content.\n if (hugs(node, axis) && (parent || node.layoutMode !== 'NONE')) return 'HUG'\n return 'FIXED'\n}\n\n/** The sizings this node accepts on either axis, in Figma's order. */\nexport function layoutSizingOptions(graph: SceneGraph, node: SceneNode): LayoutSizing[] {\n return (['FIXED', 'HUG', 'FILL'] as const).filter(\n (value) => layoutSizingError(graph, node, value) === undefined\n )\n}\n\n/** Figma's reason for refusing `value` on this node, or `undefined` when it applies. */\nexport function layoutSizingError(\n graph: SceneGraph,\n node: SceneNode,\n value: LayoutSizing\n): string | undefined {\n const parent = graphParent(graph, node)\n const inAutoLayout = parent !== undefined && parent.layoutMode !== 'NONE'\n if (!inAutoLayout && node.layoutMode === 'NONE') {\n return 'node must be an auto-layout frame or a child of an auto-layout frame'\n }\n if (value === 'FILL') {\n if (!inAutoLayout) return 'FILL can only be set on children of auto-layout frames'\n if (node.layoutPositioning === 'ABSOLUTE') {\n return 'FILL cannot be set on absolute positioned auto-layout children'\n }\n }\n if (value === 'HUG' && node.layoutMode === 'NONE' && node.type !== 'TEXT') {\n return 'HUG can only be set on auto-layout frames or text children of auto-layout frames'\n }\n return undefined\n}\n\n/** The text auto-resize that hugs the content along the axes given. */\nexport function textAutoResizeFor(hugsWidth: boolean, hugsHeight: boolean): TextAutoResize {\n if (hugsWidth && hugsHeight) return 'WIDTH_AND_HEIGHT'\n return hugsHeight ? 'HEIGHT' : 'NONE'\n}\n\nfunction textSizingUpdates(\n graph: SceneGraph,\n node: SceneNode,\n axis: LayoutSizingAxis,\n value: LayoutSizing\n): Partial<SceneNode> {\n const other: LayoutSizingAxis = axis === 'HORIZONTAL' ? 'VERTICAL' : 'HORIZONTAL'\n const hugsAxis = value === 'HUG'\n const hugsOther = layoutSizing(graph, node, other) === 'HUG'\n const autoResize =\n axis === 'HORIZONTAL'\n ? textAutoResizeFor(hugsAxis, hugsOther)\n : textAutoResizeFor(hugsOther, hugsAxis)\n return autoResize === node.textAutoResize ? {} : { textAutoResize: autoResize }\n}\n\n/**\n * The fields that set the node's sizing along `axis` to `value`, as Figma's setter writes\n * them. A value Figma refuses yields no updates; `layoutSizingError` says why.\n */\nexport function layoutSizingUpdates(\n graph: SceneGraph,\n node: SceneNode,\n axis: LayoutSizingAxis,\n value: LayoutSizing\n): Partial<SceneNode> {\n if (layoutSizingError(graph, node, value) !== undefined) return {}\n const updates: Partial<SceneNode> = {}\n const parent = fillingParent(graphParent(graph, node), node)\n if (parent && parent.layoutMode !== 'NONE') {\n const fill = value === 'FILL'\n if (axis === primaryAxis(parent.layoutMode)) {\n if (fill !== node.layoutGrow > 0) updates.layoutGrow = fill ? 1 : 0\n } else if (fill && node.layoutAlignSelf !== 'STRETCH') {\n updates.layoutAlignSelf = 'STRETCH'\n } else if (!fill && fills(parent, node, axis)) {\n // A flex parent set to stretch its children stretches this one too unless it opts out.\n const inheritsStretch = parent.layoutMode !== 'GRID' && parent.counterAxisAlign === 'STRETCH'\n updates.layoutAlignSelf = inheritsStretch ? 'MIN' : 'AUTO'\n }\n }\n const field = ownSizingField(node, axis)\n if (field) {\n const own = value === 'HUG' ? 'HUG' : 'FIXED'\n if (node[field] !== own) updates[field] = own\n }\n if (node.type === 'TEXT') Object.assign(updates, textSizingUpdates(graph, node, axis, value))\n return updates\n}\n\n/**\n * The fields that make a child fill `axis` of a parent laid out as `parentLayout`, for a node\n * an importer builds before it joins the graph. Under a parent without auto layout, such as a\n * staging parent the node leaves when placed, both are set, so it fills whichever axis its\n * eventual parent stacks along.\n */\nexport function fillSizingFields(\n parentLayout: SceneNode['layoutMode'],\n axis: LayoutSizingAxis\n): Partial<Pick<SceneNode, 'layoutGrow' | 'layoutAlignSelf'>> {\n if (parentLayout === 'NONE') return { layoutGrow: 1, layoutAlignSelf: 'STRETCH' }\n return axis === primaryAxis(parentLayout) ? { layoutGrow: 1 } : { layoutAlignSelf: 'STRETCH' }\n}\n\n/** An auto-layout frame's own sizing when it hugs or fixes its width and height. */\nexport function autoLayoutSizingFields(\n direction: AutoLayoutMode,\n width: 'FIXED' | 'HUG',\n height: 'FIXED' | 'HUG'\n): Pick<SceneNode, 'primaryAxisSizing' | 'counterAxisSizing'> {\n const horizontal = primaryAxis(direction) === 'HORIZONTAL'\n return {\n primaryAxisSizing: horizontal ? width : height,\n counterAxisSizing: horizontal ? height : width\n }\n}\n"],"mappings":";AAeA,SAAS,YAAY,MAAwC;CAC3D,OAAO,SAAS,aAAa,aAAa;AAC5C;;AAGA,SAAS,cAAc,QAA+B,MAAwC;CAC5F,IAAI,CAAC,UAAU,OAAO,eAAe,UAAU,KAAK,sBAAsB,YACxE,OAAO,KAAA;CACT,OAAO;AACT;AAEA,SAAS,YAAY,OAAmB,MAAwC;CAC9E,OAAO,KAAK,WAAW,MAAM,QAAQ,KAAK,QAAQ,IAAI,KAAA;AACxD;AAEA,SAAS,MAAM,QAAmB,MAAiB,MAAiC;CAClF,IAAI,OAAO,eAAe,QAAQ,OAAO;CACzC,IAAI,SAAS,YAAY,OAAO,UAAU,GAAG,OAAO,KAAK,aAAa;CACtE,IAAI,KAAK,oBAAoB,WAAW,OAAO;CAC/C,OACE,KAAK,oBAAoB,UACzB,OAAO,eAAe,UACtB,OAAO,qBAAqB;AAEhC;AAEA,SAAS,eACP,MACA,MACuD;CACvD,IAAI,KAAK,eAAe,QAAQ,OAAO,KAAA;CACvC,OAAO,SAAS,YAAY,KAAK,UAAU,IAAI,sBAAsB;AACvE;AAEA,SAAS,SAAS,YAA4B,MAAiC;CAC7E,I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,MAAM,aAAa,YAAY,SAAS,MAAM;CAC9C,OAAO;EACL,mBAAmB,aAAa,QAAQ;EACxC,mBAAmB,aAAa,SAAS;CAC3C;AACF"}
|
|
1
|
+
{"version":3,"file":"sizing.js","names":[],"sources":["../../src/layout/sizing.ts"],"sourcesContent":["import type { SceneGraph } from '../graph'\nimport type { LayoutSizing, SceneNode, TextAutoResize } from '../types'\nimport { flowChildren } from './flow'\n\n/**\n * How a node sizes along one screen axis, as Figma's `layoutSizingHorizontal` and\n * `layoutSizingVertical` report it. Fill is stored on the child, the way Figma stores it:\n * along the parent's primary axis as `layoutGrow`, across it as `layoutAlignSelf: 'STRETCH'`.\n * A grid lays its children out like a horizontal stack. Hug is stored on the node itself:\n * `primaryAxisSizing` and `counterAxisSizing` for an auto-layout frame, `textAutoResize`\n * for text.\n */\nexport type LayoutSizingAxis = 'HORIZONTAL' | 'VERTICAL'\n\ntype AutoLayoutMode = Exclude<SceneNode['layoutMode'], 'NONE'>\n\nfunction primaryAxis(mode: AutoLayoutMode): LayoutSizingAxis {\n return mode === 'VERTICAL' ? 'VERTICAL' : 'HORIZONTAL'\n}\n\n/** The auto-layout parent that sizes a node's fill, or `undefined` when nothing does. */\nfunction fillingParent(parent: SceneNode | undefined, node: SceneNode): SceneNode | undefined {\n if (!parent || parent.layoutMode === 'NONE' || node.layoutPositioning === 'ABSOLUTE')\n return undefined\n return parent\n}\n\nfunction graphParent(graph: SceneGraph, node: SceneNode): SceneNode | undefined {\n return node.parentId ? graph.getNode(node.parentId) : undefined\n}\n\nfunction fills(parent: SceneNode, node: SceneNode, axis: LayoutSizingAxis): boolean {\n if (parent.layoutMode === 'NONE') return false\n if (axis === primaryAxis(parent.layoutMode)) return node.layoutGrow > 0\n if (node.layoutAlignSelf === 'STRETCH') return true\n return (\n node.layoutAlignSelf === 'AUTO' &&\n parent.layoutMode !== 'GRID' &&\n parent.counterAxisAlign === 'STRETCH'\n )\n}\n\nfunction ownSizingField(\n node: SceneNode,\n axis: LayoutSizingAxis\n): 'primaryAxisSizing' | 'counterAxisSizing' | undefined {\n if (node.layoutMode === 'NONE') return undefined\n return axis === primaryAxis(node.layoutMode) ? 'primaryAxisSizing' : 'counterAxisSizing'\n}\n\nfunction textHugs(autoResize: TextAutoResize, axis: LayoutSizingAxis): boolean {\n if (autoResize === 'WIDTH_AND_HEIGHT') return true\n return axis === 'VERTICAL' && autoResize === 'HEIGHT'\n}\n\nfunction hugs(node: SceneNode, axis: LayoutSizingAxis): boolean {\n if (node.type === 'TEXT') return textHugs(node.textAutoResize, axis)\n const field = ownSizingField(node, axis)\n return field !== undefined && node[field] === 'HUG'\n}\n\n/** The node's sizing along `axis`: fill from its parent, else its own hug, else fixed. */\nexport function layoutSizing(\n graph: SceneGraph,\n node: SceneNode,\n axis: LayoutSizingAxis\n): LayoutSizing {\n return layoutSizingInParent(graphParent(graph, node), node, axis)\n}\n\n/** `layoutSizing` for code that holds the parent rather than the graph. */\nexport function layoutSizingInParent(\n parentNode: SceneNode | undefined,\n node: SceneNode,\n axis: LayoutSizingAxis\n): LayoutSizing {\n const parent = fillingParent(parentNode, node)\n if (parent && fills(parent, node, axis)) return 'FILL'\n // Figma reports text outside auto-layout as fixed, though it still resizes to its content.\n if (hugs(node, axis) && (parent || node.layoutMode !== 'NONE')) return 'HUG'\n return 'FIXED'\n}\n\n/** The sizings this node accepts on either axis, in Figma's order. */\nexport function layoutSizingOptions(graph: SceneGraph, node: SceneNode): LayoutSizing[] {\n return (['FIXED', 'HUG', 'FILL'] as const).filter(\n (value) => layoutSizingError(graph, node, value) === undefined\n )\n}\n\n/** Figma's reason for refusing `value` on this node, or `undefined` when it applies. */\nexport function layoutSizingError(\n graph: SceneGraph,\n node: SceneNode,\n value: LayoutSizing\n): string | undefined {\n const parent = graphParent(graph, node)\n const inAutoLayout = parent !== undefined && parent.layoutMode !== 'NONE'\n if (!inAutoLayout && node.layoutMode === 'NONE') {\n return 'node must be an auto-layout frame or a child of an auto-layout frame'\n }\n if (value === 'FILL') {\n if (!inAutoLayout) return 'FILL can only be set on children of auto-layout frames'\n if (node.layoutPositioning === 'ABSOLUTE') {\n return 'FILL cannot be set on absolute positioned auto-layout children'\n }\n }\n if (value === 'HUG' && node.layoutMode === 'NONE' && node.type !== 'TEXT') {\n return 'HUG can only be set on auto-layout frames or text children of auto-layout frames'\n }\n return undefined\n}\n\n/** The text auto-resize that hugs the content along the axes given. */\nexport function textAutoResizeFor(hugsWidth: boolean, hugsHeight: boolean): TextAutoResize {\n if (hugsWidth && hugsHeight) return 'WIDTH_AND_HEIGHT'\n return hugsHeight ? 'HEIGHT' : 'NONE'\n}\n\nfunction textSizingUpdates(\n graph: SceneGraph,\n node: SceneNode,\n axis: LayoutSizingAxis,\n value: LayoutSizing\n): Partial<SceneNode> {\n const other: LayoutSizingAxis = axis === 'HORIZONTAL' ? 'VERTICAL' : 'HORIZONTAL'\n const hugsAxis = value === 'HUG'\n const hugsOther = layoutSizing(graph, node, other) === 'HUG'\n const autoResize =\n axis === 'HORIZONTAL'\n ? textAutoResizeFor(hugsAxis, hugsOther)\n : textAutoResizeFor(hugsOther, hugsAxis)\n return autoResize === node.textAutoResize ? {} : { textAutoResize: autoResize }\n}\n\n/**\n * The fields that set the node's sizing along `axis` to `value`, as Figma's setter writes\n * them. A value Figma refuses yields no updates; `layoutSizingError` says why.\n */\nexport function layoutSizingUpdates(\n graph: SceneGraph,\n node: SceneNode,\n axis: LayoutSizingAxis,\n value: LayoutSizing\n): Partial<SceneNode> {\n if (layoutSizingError(graph, node, value) !== undefined) return {}\n const updates: Partial<SceneNode> = {}\n const parent = fillingParent(graphParent(graph, node), node)\n if (parent && parent.layoutMode !== 'NONE') {\n const fill = value === 'FILL'\n if (axis === primaryAxis(parent.layoutMode)) {\n if (fill !== node.layoutGrow > 0) updates.layoutGrow = fill ? 1 : 0\n } else if (fill && node.layoutAlignSelf !== 'STRETCH') {\n updates.layoutAlignSelf = 'STRETCH'\n } else if (!fill && fills(parent, node, axis)) {\n // A flex parent set to stretch its children stretches this one too unless it opts out.\n const inheritsStretch = parent.layoutMode !== 'GRID' && parent.counterAxisAlign === 'STRETCH'\n updates.layoutAlignSelf = inheritsStretch ? 'MIN' : 'AUTO'\n }\n }\n const field = ownSizingField(node, axis)\n if (field) {\n const own = value === 'HUG' ? 'HUG' : 'FIXED'\n if (node[field] !== own) updates[field] = own\n }\n if (node.type === 'TEXT') Object.assign(updates, textSizingUpdates(graph, node, axis, value))\n return updates\n}\n\n/**\n * The fields that make a child fill `axis` of a parent laid out as `parentLayout`, for a node\n * an importer builds before it joins the graph. Under a parent without auto layout, such as a\n * staging parent the node leaves when placed, both are set, so it fills whichever axis its\n * eventual parent stacks along.\n */\nexport function fillSizingFields(\n parentLayout: SceneNode['layoutMode'],\n axis: LayoutSizingAxis\n): Partial<Pick<SceneNode, 'layoutGrow' | 'layoutAlignSelf'>> {\n if (parentLayout === 'NONE') return { layoutGrow: 1, layoutAlignSelf: 'STRETCH' }\n return axis === primaryAxis(parentLayout) ? { layoutGrow: 1 } : { layoutAlignSelf: 'STRETCH' }\n}\n\n/** An auto-layout frame's own sizing when it hugs or fixes its width and height. */\nexport function autoLayoutSizingFields(\n direction: AutoLayoutMode,\n width: 'FIXED' | 'HUG',\n height: 'FIXED' | 'HUG'\n): Pick<SceneNode, 'primaryAxisSizing' | 'counterAxisSizing'> {\n const horizontal = primaryAxis(direction) === 'HORIZONTAL'\n return {\n primaryAxisSizing: horizontal ? width : height,\n counterAxisSizing: horizontal ? height : width\n }\n}\n\n/**\n * The axes a frame hugs but keeps its size on, as Figma keeps them. With nothing in its flow,\n * no visible layer that is not placed absolutely, it keeps every axis it hugs: a new frame keeps\n * the size it was made at, and one that loses its last layer keeps the size it hugged to. On the\n * counter axis it also keeps its size when every child in its flow fills that axis and none has\n * content of its own to hug.\n */\nexport function heldHugAxes(graph: SceneGraph, frame: SceneNode): ('width' | 'height')[] {\n if (frame.layoutMode !== 'HORIZONTAL' && frame.layoutMode !== 'VERTICAL') return []\n const [primary, counter] =\n frame.layoutMode === 'HORIZONTAL'\n ? (['width', 'height'] as const)\n : (['height', 'width'] as const)\n const hugged = [\n ...(frame.primaryAxisSizing === 'HUG' ? [primary] : []),\n ...(frame.counterAxisSizing === 'HUG' ? [counter] : [])\n ]\n const flow = flowChildren(graph, frame)\n if (flow.length === 0) return hugged\n const filled = flow.every(\n (child) =>\n child.layoutMode === 'NONE' &&\n child.type !== 'TEXT' &&\n (child.layoutAlignSelf === 'STRETCH' ||\n (child.layoutAlignSelf === 'AUTO' && frame.counterAxisAlign === 'STRETCH'))\n )\n return filled && frame.counterAxisSizing === 'HUG' ? [counter] : []\n}\n"],"mappings":";;AAgBA,SAAS,YAAY,MAAwC;CAC3D,OAAO,SAAS,aAAa,aAAa;AAC5C;;AAGA,SAAS,cAAc,QAA+B,MAAwC;CAC5F,IAAI,CAAC,UAAU,OAAO,eAAe,UAAU,KAAK,sBAAsB,YACxE,OAAO,KAAA;CACT,OAAO;AACT;AAEA,SAAS,YAAY,OAAmB,MAAwC;CAC9E,OAAO,KAAK,WAAW,MAAM,QAAQ,KAAK,QAAQ,IAAI,KAAA;AACxD;AAEA,SAAS,MAAM,QAAmB,MAAiB,MAAiC;CAClF,IAAI,OAAO,eAAe,QAAQ,OAAO;CACzC,IAAI,SAAS,YAAY,OAAO,UAAU,GAAG,OAAO,KAAK,aAAa;CACtE,IAAI,KAAK,oBAAoB,WAAW,OAAO;CAC/C,OACE,KAAK,oBAAoB,UACzB,OAAO,eAAe,UACtB,OAAO,qBAAqB;AAEhC;AAEA,SAAS,eACP,MACA,MACuD;CACvD,IAAI,KAAK,eAAe,QAAQ,OAAO,KAAA;CACvC,OAAO,SAAS,YAAY,KAAK,UAAU,IAAI,sBAAsB;AACvE;AAEA,SAAS,SAAS,YAA4B,MAAiC;CAC7E,IAAI,eAAe,oBAAoB,OAAO;CAC9C,OAAO,SAAS,cAAc,eAAe;AAC/C;AAEA,SAAS,KAAK,MAAiB,MAAiC;CAC9D,IAAI,KA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;;;;;AASA,SAAgB,YAAY,OAAmB,OAA0C;CACvF,IAAI,MAAM,eAAe,gBAAgB,MAAM,eAAe,YAAY,OAAO,CAAC;CAClF,MAAM,CAAC,SAAS,WACd,MAAM,eAAe,eAChB,CAAC,SAAS,QAAQ,IAClB,CAAC,UAAU,OAAO;CACzB,MAAM,SAAS,CACb,GAAI,MAAM,sBAAsB,QAAQ,CAAC,OAAO,IAAI,CAAC,GACrD,GAAI,MAAM,sBAAsB,QAAQ,CAAC,OAAO,IAAI,CAAC,CACvD;CACA,MAAM,OAAO,aAAa,OAAO,KAAK;CACtC,IAAI,KAAK,WAAW,GAAG,OAAO;CAQ9B,OAPe,KAAK,OACjB,UACC,MAAM,eAAe,UACrB,MAAM,SAAS,WACd,MAAM,oBAAoB,aACxB,MAAM,oBAAoB,UAAU,MAAM,qBAAqB,UAE1D,KAAK,MAAM,sBAAsB,QAAQ,CAAC,OAAO,IAAI,CAAC;AACpE"}
|
package/dist/layout/spacing.d.ts
CHANGED
|
@@ -5,6 +5,12 @@ import { SceneNode } from "../types.js";
|
|
|
5
5
|
* spacing, as Figma's `counterAxisSpacing: null` does.
|
|
6
6
|
*/
|
|
7
7
|
declare function counterAxisSpacingOf(node: Pick<SceneNode, 'counterAxisSpacing' | 'itemSpacing'>): number;
|
|
8
|
+
/**
|
|
9
|
+
* The space an auto layout keeps between its items along its axis. Space between takes the place
|
|
10
|
+
* of the gap, as in Figma. A generated frame keeps it as the least space between its sublayers,
|
|
11
|
+
* as a sticky note keeps its author clear of a long body.
|
|
12
|
+
*/
|
|
13
|
+
declare function primaryAxisSpacingOf(node: Pick<SceneNode, 'type' | 'primaryAxisAlign' | 'itemSpacing'>): number;
|
|
8
14
|
//#endregion
|
|
9
|
-
export { counterAxisSpacingOf };
|
|
15
|
+
export { counterAxisSpacingOf, primaryAxisSpacingOf };
|
|
10
16
|
//# sourceMappingURL=spacing.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"spacing.d.ts","names":[],"sources":["../../src/layout/spacing.ts"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"spacing.d.ts","names":[],"sources":["../../src/layout/spacing.ts"],"mappings":";;;;;;iBAOgB,qBACd,MAAM,KAAK;;;;;;iBAUG,qBACd,MAAM,KAAK"}
|
package/dist/layout/spacing.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isGeneratedFrame } from "../generated/index.js";
|
|
1
2
|
//#region src/layout/spacing.ts
|
|
2
3
|
/**
|
|
3
4
|
* The space between wrapped lines of an auto layout. A frame without its own follows the item
|
|
@@ -6,7 +7,15 @@
|
|
|
6
7
|
function counterAxisSpacingOf(node) {
|
|
7
8
|
return node.counterAxisSpacing ?? node.itemSpacing;
|
|
8
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* The space an auto layout keeps between its items along its axis. Space between takes the place
|
|
12
|
+
* of the gap, as in Figma. A generated frame keeps it as the least space between its sublayers,
|
|
13
|
+
* as a sticky note keeps its author clear of a long body.
|
|
14
|
+
*/
|
|
15
|
+
function primaryAxisSpacingOf(node) {
|
|
16
|
+
return node.primaryAxisAlign === "SPACE_BETWEEN" && !isGeneratedFrame(node) ? 0 : node.itemSpacing;
|
|
17
|
+
}
|
|
9
18
|
//#endregion
|
|
10
|
-
export { counterAxisSpacingOf };
|
|
19
|
+
export { counterAxisSpacingOf, primaryAxisSpacingOf };
|
|
11
20
|
|
|
12
21
|
//# sourceMappingURL=spacing.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"spacing.js","names":[],"sources":["../../src/layout/spacing.ts"],"sourcesContent":["import type { SceneNode } from '../types'\n\n/**\n * The space between wrapped lines of an auto layout. A frame without its own follows the item\n * spacing, as Figma's `counterAxisSpacing: null` does.\n */\nexport function counterAxisSpacingOf(\n node: Pick<SceneNode, 'counterAxisSpacing' | 'itemSpacing'>\n): number {\n return node.counterAxisSpacing ?? node.itemSpacing\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"spacing.js","names":[],"sources":["../../src/layout/spacing.ts"],"sourcesContent":["import { isGeneratedFrame } from '../generated'\nimport type { SceneNode } from '../types'\n\n/**\n * The space between wrapped lines of an auto layout. A frame without its own follows the item\n * spacing, as Figma's `counterAxisSpacing: null` does.\n */\nexport function counterAxisSpacingOf(\n node: Pick<SceneNode, 'counterAxisSpacing' | 'itemSpacing'>\n): number {\n return node.counterAxisSpacing ?? node.itemSpacing\n}\n\n/**\n * The space an auto layout keeps between its items along its axis. Space between takes the place\n * of the gap, as in Figma. A generated frame keeps it as the least space between its sublayers,\n * as a sticky note keeps its author clear of a long body.\n */\nexport function primaryAxisSpacingOf(\n node: Pick<SceneNode, 'type' | 'primaryAxisAlign' | 'itemSpacing'>\n): number {\n return node.primaryAxisAlign === 'SPACE_BETWEEN' && !isGeneratedFrame(node) ? 0 : node.itemSpacing\n}\n"],"mappings":";;;;;;AAOA,SAAgB,qBACd,MACQ;CACR,OAAO,KAAK,sBAAsB,KAAK;AACzC;;;;;;AAOA,SAAgB,qBACd,MACQ;CACR,OAAO,KAAK,qBAAqB,mBAAmB,CAAC,iBAAiB,IAAI,IAAI,IAAI,KAAK;AACzF"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Color, Vector } from "../primitives/index.js";
|
|
2
|
+
import { MeshGradient, MeshPoint, MeshSide } from "./schema.js";
|
|
3
|
+
//#region src/mesh/geometry.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* How a mesh gradient is drawn, as pen.dev draws its `mesh_gradient` (measured against its
|
|
6
|
+
* exports): each cell is a Coons patch of its four cubic Bézier edges, and its corner colours
|
|
7
|
+
* blend in smoothstep steps along both axes, so a colour changes slowest at the points. Colours
|
|
8
|
+
* blend as stored, in the document's colour space.
|
|
9
|
+
*/
|
|
10
|
+
declare const MESH_SIDES: readonly ["left", "right", "top", "bottom"];
|
|
11
|
+
/** The handle on the other side of the point along the same axis. */
|
|
12
|
+
declare function oppositeMeshSide(side: MeshSide): MeshSide;
|
|
13
|
+
/** Where the point at `column`, `row` is in `points`. */
|
|
14
|
+
declare function meshIndex(mesh: Pick<MeshGradient, 'columns'>, column: number, row: number): number;
|
|
15
|
+
/** The column and row of the point at `index`. */
|
|
16
|
+
declare function meshCell(mesh: Pick<MeshGradient, 'columns'>, index: number): {
|
|
17
|
+
column: number;
|
|
18
|
+
row: number;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* The handle a point has towards a side when none is set: a quarter of the default spacing of
|
|
22
|
+
* the grid, whatever the point's position, as pen.dev chooses.
|
|
23
|
+
*/
|
|
24
|
+
declare function meshAutoHandle(mesh: Pick<MeshGradient, 'columns' | 'rows'>, side: MeshSide): {
|
|
25
|
+
x: number;
|
|
26
|
+
y: number;
|
|
27
|
+
};
|
|
28
|
+
/** The handle of the point at `index` towards `side`, as an offset from the point. */
|
|
29
|
+
declare function meshHandle(mesh: MeshGradient, index: number, side: MeshSide): Vector;
|
|
30
|
+
/** The sides of the point at `index` that have a neighbour, and so a handle. */
|
|
31
|
+
declare function meshPointSides(mesh: Pick<MeshGradient, 'columns' | 'rows'>, index: number): ("left" | "right" | "top" | "bottom")[];
|
|
32
|
+
/** Where (`u`, `v`) of the cell at `column`, `row` lands in the layer, from 0 to 1 across it. */
|
|
33
|
+
declare function meshCellPoint(mesh: MeshGradient, column: number, row: number, u: number, v: number): Vector;
|
|
34
|
+
/** The colour at (`u`, `v`) of the cell at `column`, `row`. */
|
|
35
|
+
declare function meshCellColor(mesh: MeshGradient, column: number, row: number, u: number, v: number): Color;
|
|
36
|
+
/** Triangles that draw a mesh, in the layer's unit square, with a colour per vertex. */
|
|
37
|
+
interface MeshTessellation {
|
|
38
|
+
/** x, y per vertex. */
|
|
39
|
+
positions: Float32Array;
|
|
40
|
+
/** r, g, b, a per vertex. */
|
|
41
|
+
colors: Float32Array;
|
|
42
|
+
/** Three vertices per triangle. */
|
|
43
|
+
indices: number[];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The mesh as triangles, each cell split into `steps` × `steps` quads; fewer when the mesh is too
|
|
47
|
+
* large to fit 16-bit indices.
|
|
48
|
+
*/
|
|
49
|
+
declare function tessellateMesh(mesh: MeshGradient, steps: number): MeshTessellation;
|
|
50
|
+
/** A mesh of straight, evenly spaced points with automatic handles, coloured by `colorAt`. */
|
|
51
|
+
declare function uniformMesh(columns: number, rows: number, colorAt: (column: number, row: number) => Color): MeshGradient;
|
|
52
|
+
/**
|
|
53
|
+
* The mesh with `columns` × `rows` points that draws as `mesh` does: each new point sits on the
|
|
54
|
+
* surface, takes the colour drawn there, and has handles along the surface, so bends survive.
|
|
55
|
+
* Growing a grid keeps its look; shrinking it averages detail away. A uniform mesh stays uniform.
|
|
56
|
+
*/
|
|
57
|
+
declare function resampleMesh(mesh: MeshGradient, columns: number, rows: number): MeshGradient;
|
|
58
|
+
/**
|
|
59
|
+
* `point` as the point at `index` may be: one on an edge of the layer stays on it, and its handles
|
|
60
|
+
* along that edge stay on it too, so the mesh always covers the whole layer. Corners stay put.
|
|
61
|
+
*/
|
|
62
|
+
declare function constrainMeshPoint(mesh: Pick<MeshGradient, 'columns' | 'rows'>, index: number, point: MeshPoint): MeshPoint;
|
|
63
|
+
/** `mesh` with point `index` at `position`, kept on its edge as `constrainMeshPoint` keeps it. */
|
|
64
|
+
declare function withMeshPointAt(mesh: MeshGradient, index: number, position: Vector): MeshGradient;
|
|
65
|
+
/**
|
|
66
|
+
* `mesh` with the handle of point `index` towards `side` at `offset`. With `mirror`, the handle on
|
|
67
|
+
* the other side turns to point the opposite way and keeps its own length, as a smooth point's
|
|
68
|
+
* handles do in vector editing.
|
|
69
|
+
*/
|
|
70
|
+
declare function withMeshHandle(mesh: MeshGradient, index: number, side: MeshSide, offset: Vector, mirror: boolean): MeshGradient;
|
|
71
|
+
/** `mesh` with point `index` coloured `color`. */
|
|
72
|
+
declare function withMeshColor(mesh: MeshGradient, index: number, color: Color): MeshGradient;
|
|
73
|
+
/** `mesh` with point `index` back where a uniform grid puts it, keeping its handles. */
|
|
74
|
+
declare function withMeshPositionReset(mesh: MeshGradient, index: number): MeshGradient;
|
|
75
|
+
/** `mesh` with point `index`'s handles back to automatic. */
|
|
76
|
+
declare function withMeshCurveReset(mesh: MeshGradient, index: number): MeshGradient;
|
|
77
|
+
/** Where the handle of point `index` towards `side` ends, in the layer's unit square. */
|
|
78
|
+
declare function meshHandleEnd(mesh: MeshGradient, index: number, side: MeshSide): Vector;
|
|
79
|
+
/** The mesh a paint becomes when switched to a mesh gradient: 3 × 3, uniform. */
|
|
80
|
+
declare function defaultMeshGradient(): MeshGradient;
|
|
81
|
+
//#endregion
|
|
82
|
+
export { MESH_SIDES, MeshTessellation, constrainMeshPoint, defaultMeshGradient, meshAutoHandle, meshCell, meshCellColor, meshCellPoint, meshHandle, meshHandleEnd, meshIndex, meshPointSides, oppositeMeshSide, resampleMesh, tessellateMesh, uniformMesh, withMeshColor, withMeshCurveReset, withMeshHandle, withMeshPointAt, withMeshPositionReset };
|
|
83
|
+
//# sourceMappingURL=geometry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"geometry.d.ts","names":[],"sources":["../../src/mesh/geometry.ts"],"mappings":";;;;;;;;;cAUa;;iBAiBG,iBAAiB,MAAM,WAAW;;iBAKlC,UAAU,MAAM,KAAK,0BAA0B,gBAAgB;;iBAK/D,SAAS,MAAM,KAAK,0BAA0B;;;;;;;;iBAQ9C,eAAe,MAAM,KAAK,mCAAmC,MAAM;;;;;iBAMnE,WAAW,MAAM,cAAc,eAAe,MAAM,WAAW;;iBAK/D,eAAe,MAAM,KAAK,mCAAmC;;iBAmD7D,cACd,MAAM,cACN,gBACA,aACA,WACA,YACC;;iBAyBa,cACd,MAAM,cACN,gBACA,aACA,WACA,YACC;;UAac;;EAEf,WAAW;;EAEX,QAAQ;;EAER;;;;;;iBAUc,eAAe,MAAM,cAAc,gBAAgB;;iBAmCnD,YACd,iBACA,cACA,UAAU,gBAAgB,gBAAgB,QACzC;;;;;;iBA2Da,aAAa,MAAM,cAAc,iBAAiB,eAAe;;;;;iBA4CjE,mBACd,MAAM,KAAK,mCACX,eACA,OAAO,YACN;;iBAwCa,gBAAgB,MAAM,cAAc,eAAe,UAAU,SAAS;;;;;;iBAStE,eACd,MAAM,cACN,eACA,MAAM,UACN,QAAQ,QACR,kBACC;;iBAaa,cAAc,MAAM,cAAc,eAAe,OAAO,QAAQ;;iBAKhE,sBAAsB,MAAM,cAAc,gBAAgB;;iBAS1D,mBAAmB,MAAM,cAAc,gBAAgB;;iBAKvD,cAAc,MAAM,cAAc,eAAe,MAAM,WAAW;;iBAwBlE,uBAAuB"}
|
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
//#region src/mesh/geometry.ts
|
|
2
|
+
/**
|
|
3
|
+
* How a mesh gradient is drawn, as pen.dev draws its `mesh_gradient` (measured against its
|
|
4
|
+
* exports): each cell is a Coons patch of its four cubic Bézier edges, and its corner colours
|
|
5
|
+
* blend in smoothstep steps along both axes, so a colour changes slowest at the points. Colours
|
|
6
|
+
* blend as stored, in the document's colour space.
|
|
7
|
+
*/
|
|
8
|
+
const MESH_SIDES = [
|
|
9
|
+
"left",
|
|
10
|
+
"right",
|
|
11
|
+
"top",
|
|
12
|
+
"bottom"
|
|
13
|
+
];
|
|
14
|
+
const SIDE_STEP = {
|
|
15
|
+
left: {
|
|
16
|
+
x: -1,
|
|
17
|
+
y: 0
|
|
18
|
+
},
|
|
19
|
+
right: {
|
|
20
|
+
x: 1,
|
|
21
|
+
y: 0
|
|
22
|
+
},
|
|
23
|
+
top: {
|
|
24
|
+
x: 0,
|
|
25
|
+
y: -1
|
|
26
|
+
},
|
|
27
|
+
bottom: {
|
|
28
|
+
x: 0,
|
|
29
|
+
y: 1
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
const OPPOSITE = {
|
|
33
|
+
left: "right",
|
|
34
|
+
right: "left",
|
|
35
|
+
top: "bottom",
|
|
36
|
+
bottom: "top"
|
|
37
|
+
};
|
|
38
|
+
/** The handle on the other side of the point along the same axis. */
|
|
39
|
+
function oppositeMeshSide(side) {
|
|
40
|
+
return OPPOSITE[side];
|
|
41
|
+
}
|
|
42
|
+
/** Where the point at `column`, `row` is in `points`. */
|
|
43
|
+
function meshIndex(mesh, column, row) {
|
|
44
|
+
return row * mesh.columns + column;
|
|
45
|
+
}
|
|
46
|
+
/** The column and row of the point at `index`. */
|
|
47
|
+
function meshCell(mesh, index) {
|
|
48
|
+
return {
|
|
49
|
+
column: index % mesh.columns,
|
|
50
|
+
row: Math.floor(index / mesh.columns)
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The handle a point has towards a side when none is set: a quarter of the default spacing of
|
|
55
|
+
* the grid, whatever the point's position, as pen.dev chooses.
|
|
56
|
+
*/
|
|
57
|
+
function meshAutoHandle(mesh, side) {
|
|
58
|
+
const step = SIDE_STEP[side];
|
|
59
|
+
return {
|
|
60
|
+
x: step.x / (4 * (mesh.columns - 1)),
|
|
61
|
+
y: step.y / (4 * (mesh.rows - 1))
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/** The handle of the point at `index` towards `side`, as an offset from the point. */
|
|
65
|
+
function meshHandle(mesh, index, side) {
|
|
66
|
+
return mesh.points[index][side] ?? meshAutoHandle(mesh, side);
|
|
67
|
+
}
|
|
68
|
+
/** The sides of the point at `index` that have a neighbour, and so a handle. */
|
|
69
|
+
function meshPointSides(mesh, index) {
|
|
70
|
+
const { column, row } = meshCell(mesh, index);
|
|
71
|
+
return MESH_SIDES.filter((side) => {
|
|
72
|
+
if (side === "left") return column > 0;
|
|
73
|
+
if (side === "right") return column < mesh.columns - 1;
|
|
74
|
+
if (side === "top") return row > 0;
|
|
75
|
+
return row < mesh.rows - 1;
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
function cubic(p0, p1, p2, p3, t) {
|
|
79
|
+
const s = 1 - t;
|
|
80
|
+
const a = s * s * s;
|
|
81
|
+
const b = 3 * t * s * s;
|
|
82
|
+
const c = 3 * t * t * s;
|
|
83
|
+
const d = t * t * t;
|
|
84
|
+
return {
|
|
85
|
+
x: a * p0.x + b * p1.x + c * p2.x + d * p3.x,
|
|
86
|
+
y: a * p0.y + b * p1.y + c * p2.y + d * p3.y
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function plus(a, b) {
|
|
90
|
+
return {
|
|
91
|
+
x: a.x + b.x,
|
|
92
|
+
y: a.y + b.y
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/** The edge from point `from` to point `to`, leaving `from` towards `side`, at `t`. */
|
|
96
|
+
function edgeAt(mesh, from, to, side, t) {
|
|
97
|
+
const a = mesh.points[from].position;
|
|
98
|
+
const b = mesh.points[to].position;
|
|
99
|
+
return cubic(a, plus(a, meshHandle(mesh, from, side)), plus(b, meshHandle(mesh, to, OPPOSITE[side])), b, t);
|
|
100
|
+
}
|
|
101
|
+
/** The four corner points of the cell whose top left point is at `column`, `row`. */
|
|
102
|
+
function corners(mesh, column, row) {
|
|
103
|
+
const topLeft = meshIndex(mesh, column, row);
|
|
104
|
+
return {
|
|
105
|
+
topLeft,
|
|
106
|
+
topRight: topLeft + 1,
|
|
107
|
+
bottomLeft: topLeft + mesh.columns,
|
|
108
|
+
bottomRight: topLeft + mesh.columns + 1
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/** Where (`u`, `v`) of the cell at `column`, `row` lands in the layer, from 0 to 1 across it. */
|
|
112
|
+
function meshCellPoint(mesh, column, row, u, v) {
|
|
113
|
+
const c = corners(mesh, column, row);
|
|
114
|
+
const top = edgeAt(mesh, c.topLeft, c.topRight, "right", u);
|
|
115
|
+
const bottom = edgeAt(mesh, c.bottomLeft, c.bottomRight, "right", u);
|
|
116
|
+
const left = edgeAt(mesh, c.topLeft, c.bottomLeft, "bottom", v);
|
|
117
|
+
const right = edgeAt(mesh, c.topRight, c.bottomRight, "bottom", v);
|
|
118
|
+
const p00 = mesh.points[c.topLeft].position;
|
|
119
|
+
const p10 = mesh.points[c.topRight].position;
|
|
120
|
+
const p01 = mesh.points[c.bottomLeft].position;
|
|
121
|
+
const p11 = mesh.points[c.bottomRight].position;
|
|
122
|
+
const bilinear = (key) => (1 - u) * (1 - v) * p00[key] + u * (1 - v) * p10[key] + (1 - u) * v * p01[key] + u * v * p11[key];
|
|
123
|
+
const coons = (key) => (1 - v) * top[key] + v * bottom[key] + (1 - u) * left[key] + u * right[key] - bilinear(key);
|
|
124
|
+
return {
|
|
125
|
+
x: coons("x"),
|
|
126
|
+
y: coons("y")
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
function smoothstep(t) {
|
|
130
|
+
return t * t * (3 - 2 * t);
|
|
131
|
+
}
|
|
132
|
+
/** The colour at (`u`, `v`) of the cell at `column`, `row`. */
|
|
133
|
+
function meshCellColor(mesh, column, row, u, v) {
|
|
134
|
+
const c = corners(mesh, column, row);
|
|
135
|
+
const su = smoothstep(u);
|
|
136
|
+
const sv = smoothstep(v);
|
|
137
|
+
const mix = (key) => {
|
|
138
|
+
const top = mesh.colors[c.topLeft][key] * (1 - su) + mesh.colors[c.topRight][key] * su;
|
|
139
|
+
const bottom = mesh.colors[c.bottomLeft][key] * (1 - su) + mesh.colors[c.bottomRight][key] * su;
|
|
140
|
+
return top * (1 - sv) + bottom * sv;
|
|
141
|
+
};
|
|
142
|
+
return {
|
|
143
|
+
r: mix("r"),
|
|
144
|
+
g: mix("g"),
|
|
145
|
+
b: mix("b"),
|
|
146
|
+
a: mix("a")
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
/** The most vertices a tessellation holds, so indices fit 16 bits. */
|
|
150
|
+
const MAX_VERTICES = 65535;
|
|
151
|
+
/**
|
|
152
|
+
* The mesh as triangles, each cell split into `steps` × `steps` quads; fewer when the mesh is too
|
|
153
|
+
* large to fit 16-bit indices.
|
|
154
|
+
*/
|
|
155
|
+
function tessellateMesh(mesh, steps) {
|
|
156
|
+
const cells = (mesh.columns - 1) * (mesh.rows - 1);
|
|
157
|
+
const n = Math.max(1, Math.min(Math.round(steps), Math.floor(Math.sqrt(MAX_VERTICES / cells)) - 1));
|
|
158
|
+
const side = n + 1;
|
|
159
|
+
const positions = new Float32Array(cells * side * side * 2);
|
|
160
|
+
const colors = new Float32Array(cells * side * side * 4);
|
|
161
|
+
const indices = [];
|
|
162
|
+
let vertex = 0;
|
|
163
|
+
for (let row = 0; row < mesh.rows - 1; row++) for (let column = 0; column < mesh.columns - 1; column++) {
|
|
164
|
+
const first = vertex;
|
|
165
|
+
for (let j = 0; j <= n; j++) for (let i = 0; i <= n; i++) {
|
|
166
|
+
const point = meshCellPoint(mesh, column, row, i / n, j / n);
|
|
167
|
+
const color = meshCellColor(mesh, column, row, i / n, j / n);
|
|
168
|
+
positions.set([point.x, point.y], vertex * 2);
|
|
169
|
+
colors.set([
|
|
170
|
+
color.r,
|
|
171
|
+
color.g,
|
|
172
|
+
color.b,
|
|
173
|
+
color.a
|
|
174
|
+
], vertex * 4);
|
|
175
|
+
vertex++;
|
|
176
|
+
}
|
|
177
|
+
for (let j = 0; j < n; j++) for (let i = 0; i < n; i++) {
|
|
178
|
+
const a = first + j * side + i;
|
|
179
|
+
indices.push(a, a + 1, a + side, a + 1, a + side + 1, a + side);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return {
|
|
183
|
+
positions,
|
|
184
|
+
colors,
|
|
185
|
+
indices
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
/** A mesh of straight, evenly spaced points with automatic handles, coloured by `colorAt`. */
|
|
189
|
+
function uniformMesh(columns, rows, colorAt) {
|
|
190
|
+
const points = [];
|
|
191
|
+
const colors = [];
|
|
192
|
+
for (let row = 0; row < rows; row++) for (let column = 0; column < columns; column++) {
|
|
193
|
+
points.push({ position: {
|
|
194
|
+
x: column / (columns - 1),
|
|
195
|
+
y: row / (rows - 1)
|
|
196
|
+
} });
|
|
197
|
+
colors.push(colorAt(column, row));
|
|
198
|
+
}
|
|
199
|
+
return {
|
|
200
|
+
columns,
|
|
201
|
+
rows,
|
|
202
|
+
points,
|
|
203
|
+
colors
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
/** Whether every point sits where a uniform grid puts it, with automatic handles. */
|
|
207
|
+
function isUniform(mesh) {
|
|
208
|
+
return mesh.points.every((point, index) => {
|
|
209
|
+
const { column, row } = meshCell(mesh, index);
|
|
210
|
+
return MESH_SIDES.every((side) => point[side] === void 0) && Math.abs(point.position.x - column / (mesh.columns - 1)) < 1e-9 && Math.abs(point.position.y - row / (mesh.rows - 1)) < 1e-9;
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
/** The cell and the place in it at `s` points along an axis of `count` points. */
|
|
214
|
+
function locate(s, count, leaning) {
|
|
215
|
+
const last = count - 2;
|
|
216
|
+
let cell = Math.min(Math.floor(s), last);
|
|
217
|
+
if (leaning === "before" && cell > 0 && s - cell < 1e-9) cell--;
|
|
218
|
+
return {
|
|
219
|
+
cell,
|
|
220
|
+
t: s - cell
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
/** How fast the surface moves along an axis at (`u`, `v`) of a cell, per unit of the axis. */
|
|
224
|
+
function surfaceSlope(mesh, column, row, u, v, axis) {
|
|
225
|
+
const h = 1e-4;
|
|
226
|
+
const at = (d) => axis === "u" ? meshCellPoint(mesh, column, row, Math.min(1, Math.max(0, u + d)), v) : meshCellPoint(mesh, column, row, u, Math.min(1, Math.max(0, v + d)));
|
|
227
|
+
const lo = Math.max(0, (axis === "u" ? u : v) - h);
|
|
228
|
+
const hi = Math.min(1, (axis === "u" ? u : v) + h);
|
|
229
|
+
const base = axis === "u" ? u : v;
|
|
230
|
+
const a = at(lo - base);
|
|
231
|
+
const b = at(hi - base);
|
|
232
|
+
return {
|
|
233
|
+
x: (b.x - a.x) / (hi - lo),
|
|
234
|
+
y: (b.y - a.y) / (hi - lo)
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* The mesh with `columns` × `rows` points that draws as `mesh` does: each new point sits on the
|
|
239
|
+
* surface, takes the colour drawn there, and has handles along the surface, so bends survive.
|
|
240
|
+
* Growing a grid keeps its look; shrinking it averages detail away. A uniform mesh stays uniform.
|
|
241
|
+
*/
|
|
242
|
+
function resampleMesh(mesh, columns, rows) {
|
|
243
|
+
if (columns === mesh.columns && rows === mesh.rows) return structuredClone(mesh);
|
|
244
|
+
const stepU = (mesh.columns - 1) / (columns - 1);
|
|
245
|
+
const stepV = (mesh.rows - 1) / (rows - 1);
|
|
246
|
+
const colorAt = (column, row) => {
|
|
247
|
+
const u = locate(column * stepU, mesh.columns, "after");
|
|
248
|
+
const v = locate(row * stepV, mesh.rows, "after");
|
|
249
|
+
return meshCellColor(mesh, u.cell, v.cell, u.t, v.t);
|
|
250
|
+
};
|
|
251
|
+
if (isUniform(mesh)) return uniformMesh(columns, rows, colorAt);
|
|
252
|
+
const resampled = uniformMesh(columns, rows, colorAt);
|
|
253
|
+
resampled.points = resampled.points.map((_, index) => {
|
|
254
|
+
const { column, row } = meshCell(resampled, index);
|
|
255
|
+
const s = column * stepU;
|
|
256
|
+
const t = row * stepV;
|
|
257
|
+
const u = locate(s, mesh.columns, "after");
|
|
258
|
+
const v = locate(t, mesh.rows, "after");
|
|
259
|
+
const point = { position: meshCellPoint(mesh, u.cell, v.cell, u.t, v.t) };
|
|
260
|
+
const handle = (side) => {
|
|
261
|
+
const horizontal = side === "left" || side === "right";
|
|
262
|
+
const leaning = side === "left" || side === "top" ? "before" : "after";
|
|
263
|
+
const cu = horizontal ? locate(s, mesh.columns, leaning) : u;
|
|
264
|
+
const cv = horizontal ? v : locate(t, mesh.rows, leaning);
|
|
265
|
+
const slope = surfaceSlope(mesh, cu.cell, cv.cell, cu.t, cv.t, horizontal ? "u" : "v");
|
|
266
|
+
const length = (horizontal ? stepU : stepV) / 3 * (leaning === "before" ? -1 : 1);
|
|
267
|
+
return {
|
|
268
|
+
x: slope.x * length,
|
|
269
|
+
y: slope.y * length
|
|
270
|
+
};
|
|
271
|
+
};
|
|
272
|
+
for (const side of meshPointSides(resampled, index)) {
|
|
273
|
+
const value = handle(side);
|
|
274
|
+
const auto = meshAutoHandle(resampled, side);
|
|
275
|
+
if (Math.abs(value.x - auto.x) > 1e-6 || Math.abs(value.y - auto.y) > 1e-6) point[side] = value;
|
|
276
|
+
}
|
|
277
|
+
return constrainMeshPoint(resampled, index, point);
|
|
278
|
+
});
|
|
279
|
+
return resampled;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* `point` as the point at `index` may be: one on an edge of the layer stays on it, and its handles
|
|
283
|
+
* along that edge stay on it too, so the mesh always covers the whole layer. Corners stay put.
|
|
284
|
+
*/
|
|
285
|
+
function constrainMeshPoint(mesh, index, point) {
|
|
286
|
+
const { column, row } = meshCell(mesh, index);
|
|
287
|
+
const onLeft = column === 0;
|
|
288
|
+
const onRight = column === mesh.columns - 1;
|
|
289
|
+
const onTop = row === 0;
|
|
290
|
+
const onBottom = row === mesh.rows - 1;
|
|
291
|
+
/** A coordinate inside the layer, pinned to the edge the point lies on, if any. */
|
|
292
|
+
const pin = (value, start, end) => {
|
|
293
|
+
if (start) return 0;
|
|
294
|
+
if (end) return 1;
|
|
295
|
+
return Math.min(1, Math.max(0, value));
|
|
296
|
+
};
|
|
297
|
+
const constrained = { position: {
|
|
298
|
+
x: pin(point.position.x, onLeft, onRight),
|
|
299
|
+
y: pin(point.position.y, onTop, onBottom)
|
|
300
|
+
} };
|
|
301
|
+
for (const side of MESH_SIDES) {
|
|
302
|
+
const handle = point[side];
|
|
303
|
+
if (!handle) continue;
|
|
304
|
+
const alongVerticalEdge = (onLeft || onRight) && (side === "top" || side === "bottom");
|
|
305
|
+
const alongHorizontalEdge = (onTop || onBottom) && (side === "left" || side === "right");
|
|
306
|
+
constrained[side] = {
|
|
307
|
+
x: alongVerticalEdge ? 0 : handle.x,
|
|
308
|
+
y: alongHorizontalEdge ? 0 : handle.y
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
return constrained;
|
|
312
|
+
}
|
|
313
|
+
function withPoint(mesh, index, point) {
|
|
314
|
+
return {
|
|
315
|
+
...mesh,
|
|
316
|
+
points: mesh.points.map((existing, at) => at === index ? constrainMeshPoint(mesh, index, point) : existing)
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
/** `mesh` with point `index` at `position`, kept on its edge as `constrainMeshPoint` keeps it. */
|
|
320
|
+
function withMeshPointAt(mesh, index, position) {
|
|
321
|
+
return withPoint(mesh, index, {
|
|
322
|
+
...mesh.points[index],
|
|
323
|
+
position
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* `mesh` with the handle of point `index` towards `side` at `offset`. With `mirror`, the handle on
|
|
328
|
+
* the other side turns to point the opposite way and keeps its own length, as a smooth point's
|
|
329
|
+
* handles do in vector editing.
|
|
330
|
+
*/
|
|
331
|
+
function withMeshHandle(mesh, index, side, offset, mirror) {
|
|
332
|
+
const point = {
|
|
333
|
+
...mesh.points[index],
|
|
334
|
+
[side]: offset
|
|
335
|
+
};
|
|
336
|
+
const opposite = OPPOSITE[side];
|
|
337
|
+
const length = Math.hypot(offset.x, offset.y);
|
|
338
|
+
if (mirror && length > 1e-9 && meshPointSides(mesh, index).includes(opposite)) {
|
|
339
|
+
const current = meshHandle(mesh, index, opposite);
|
|
340
|
+
const keep = Math.hypot(current.x, current.y) / length;
|
|
341
|
+
point[opposite] = {
|
|
342
|
+
x: -offset.x * keep,
|
|
343
|
+
y: -offset.y * keep
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
return withPoint(mesh, index, point);
|
|
347
|
+
}
|
|
348
|
+
/** `mesh` with point `index` coloured `color`. */
|
|
349
|
+
function withMeshColor(mesh, index, color) {
|
|
350
|
+
return {
|
|
351
|
+
...mesh,
|
|
352
|
+
colors: mesh.colors.map((existing, at) => at === index ? color : existing)
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
/** `mesh` with point `index` back where a uniform grid puts it, keeping its handles. */
|
|
356
|
+
function withMeshPositionReset(mesh, index) {
|
|
357
|
+
const { column, row } = meshCell(mesh, index);
|
|
358
|
+
return withMeshPointAt(mesh, index, {
|
|
359
|
+
x: column / (mesh.columns - 1),
|
|
360
|
+
y: row / (mesh.rows - 1)
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
/** `mesh` with point `index`'s handles back to automatic. */
|
|
364
|
+
function withMeshCurveReset(mesh, index) {
|
|
365
|
+
return withPoint(mesh, index, { position: mesh.points[index].position });
|
|
366
|
+
}
|
|
367
|
+
/** Where the handle of point `index` towards `side` ends, in the layer's unit square. */
|
|
368
|
+
function meshHandleEnd(mesh, index, side) {
|
|
369
|
+
return plus(mesh.points[index].position, meshHandle(mesh, index, side));
|
|
370
|
+
}
|
|
371
|
+
/** Colours a new mesh starts with, corner to corner: warm at the bottom left, cool at the top right. */
|
|
372
|
+
const DEFAULT_MESH_COLORS = [
|
|
373
|
+
[
|
|
374
|
+
{
|
|
375
|
+
r: .98,
|
|
376
|
+
g: .56,
|
|
377
|
+
b: .45,
|
|
378
|
+
a: 1
|
|
379
|
+
},
|
|
380
|
+
{
|
|
381
|
+
r: .61,
|
|
382
|
+
g: .36,
|
|
383
|
+
b: .9,
|
|
384
|
+
a: 1
|
|
385
|
+
},
|
|
386
|
+
{
|
|
387
|
+
r: .3,
|
|
388
|
+
g: .79,
|
|
389
|
+
b: .94,
|
|
390
|
+
a: 1
|
|
391
|
+
}
|
|
392
|
+
],
|
|
393
|
+
[
|
|
394
|
+
{
|
|
395
|
+
r: .98,
|
|
396
|
+
g: .56,
|
|
397
|
+
b: .45,
|
|
398
|
+
a: 1
|
|
399
|
+
},
|
|
400
|
+
{
|
|
401
|
+
r: .98,
|
|
402
|
+
g: .56,
|
|
403
|
+
b: .45,
|
|
404
|
+
a: 1
|
|
405
|
+
},
|
|
406
|
+
{
|
|
407
|
+
r: .61,
|
|
408
|
+
g: .36,
|
|
409
|
+
b: .9,
|
|
410
|
+
a: 1
|
|
411
|
+
}
|
|
412
|
+
],
|
|
413
|
+
[
|
|
414
|
+
{
|
|
415
|
+
r: .98,
|
|
416
|
+
g: .88,
|
|
417
|
+
b: .5,
|
|
418
|
+
a: 1
|
|
419
|
+
},
|
|
420
|
+
{
|
|
421
|
+
r: .98,
|
|
422
|
+
g: .56,
|
|
423
|
+
b: .45,
|
|
424
|
+
a: 1
|
|
425
|
+
},
|
|
426
|
+
{
|
|
427
|
+
r: .61,
|
|
428
|
+
g: .36,
|
|
429
|
+
b: .9,
|
|
430
|
+
a: 1
|
|
431
|
+
}
|
|
432
|
+
]
|
|
433
|
+
];
|
|
434
|
+
/** The mesh a paint becomes when switched to a mesh gradient: 3 × 3, uniform. */
|
|
435
|
+
function defaultMeshGradient() {
|
|
436
|
+
return uniformMesh(3, 3, (column, row) => DEFAULT_MESH_COLORS[row][column]);
|
|
437
|
+
}
|
|
438
|
+
//#endregion
|
|
439
|
+
export { MESH_SIDES, constrainMeshPoint, defaultMeshGradient, meshAutoHandle, meshCell, meshCellColor, meshCellPoint, meshHandle, meshHandleEnd, meshIndex, meshPointSides, oppositeMeshSide, resampleMesh, tessellateMesh, uniformMesh, withMeshColor, withMeshCurveReset, withMeshHandle, withMeshPointAt, withMeshPositionReset };
|
|
440
|
+
|
|
441
|
+
//# sourceMappingURL=geometry.js.map
|