paper 0.16.0 → 1.0.0-rc.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/CHANGELOG.md +294 -0
- package/LICENSE.txt +1 -1
- package/MIGRATION.md +402 -0
- package/README.md +137 -143
- package/dist/esm/anim/Tween.js +404 -0
- package/dist/esm/basic/Line.js +252 -0
- package/dist/esm/basic/Matrix.js +603 -0
- package/dist/esm/basic/Point.js +738 -0
- package/dist/esm/basic/Rectangle.js +788 -0
- package/dist/esm/basic/Size.js +368 -0
- package/dist/esm/boolean/graph.js +950 -0
- package/dist/esm/boolean/index.js +143 -0
- package/dist/esm/boolean/operations.js +94 -0
- package/dist/esm/boolean/split.js +89 -0
- package/dist/esm/canvas/blendModes.js +115 -0
- package/dist/esm/canvas/canvasProvider.js +102 -0
- package/dist/esm/constants.js +130 -0
- package/dist/esm/core/Base.js +52 -0
- package/dist/esm/core/Emitter.js +215 -0
- package/dist/esm/core/features.js +53 -0
- package/dist/esm/core/serialization.js +174 -0
- package/dist/esm/core.js +15 -0
- package/dist/esm/dom/domElement.js +112 -0
- package/dist/esm/dom/domEvent.js +106 -0
- package/dist/esm/env.js +66 -0
- package/dist/esm/event/DrawEvent.js +38 -0
- package/dist/esm/event/Event.js +79 -0
- package/dist/esm/event/Key.js +236 -0
- package/dist/esm/event/KeyEvent.js +39 -0
- package/dist/esm/event/MouseEvent.js +91 -0
- package/dist/esm/hosted/HostItem.js +60 -0
- package/dist/esm/hosted/Hosting.js +1061 -0
- package/dist/esm/hosted/index.js +20 -0
- package/dist/esm/index.js +38 -0
- package/dist/esm/internal.js +115 -0
- package/dist/esm/item/Effect.js +256 -0
- package/dist/esm/item/EffectInstance.js +184 -0
- package/dist/esm/item/Group.js +293 -0
- package/dist/esm/item/HitResult.js +126 -0
- package/dist/esm/item/Item.js +3339 -0
- package/dist/esm/item/ItemDefinition.js +95 -0
- package/dist/esm/item/Layer.js +69 -0
- package/dist/esm/item/Project.js +1013 -0
- package/dist/esm/item/Raster.js +722 -0
- package/dist/esm/item/Shape.js +355 -0
- package/dist/esm/item/SymbolDefinition.js +68 -0
- package/dist/esm/item/SymbolItem.js +131 -0
- package/dist/esm/item/bounds.js +324 -0
- package/dist/esm/item/draw.js +525 -0
- package/dist/esm/item/effects.js +124 -0
- package/dist/esm/item/expand.js +87 -0
- package/dist/esm/item/hitTest.js +203 -0
- package/dist/esm/item/hosting.js +12 -0
- package/dist/esm/item/props.js +12 -0
- package/dist/esm/item/query.js +139 -0
- package/dist/esm/item/sampling.js +67 -0
- package/dist/esm/item/transform.js +128 -0
- package/dist/esm/net/http.js +26 -0
- package/dist/esm/node/CanvasAdapter.js +137 -0
- package/dist/esm/node/index.js +150 -0
- package/dist/esm/node/peers.js +27 -0
- package/dist/esm/offset/curves.js +866 -0
- package/dist/esm/offset/index.js +70 -0
- package/dist/esm/offset/offset.js +178 -0
- package/dist/esm/offset/outline.js +91 -0
- package/dist/esm/offset/pen.js +298 -0
- package/dist/esm/offset/profile.js +772 -0
- package/dist/esm/offset/round.js +213 -0
- package/dist/esm/offset/sweep.js +100 -0
- package/dist/esm/paperscript/PaperScript.js +832 -0
- package/dist/esm/paperscript/autoload.js +27 -0
- package/dist/esm/paperscript/index.js +17 -0
- package/dist/esm/path/CompoundPath.js +367 -0
- package/dist/esm/path/Curve.js +996 -0
- package/dist/esm/path/CurveLocation.js +462 -0
- package/dist/esm/path/Path.js +1761 -0
- package/dist/esm/path/PathFlattener.js +186 -0
- package/dist/esm/path/PathItem.js +757 -0
- package/dist/esm/path/Segment.js +640 -0
- package/dist/esm/path/SegmentPoint.js +106 -0
- package/dist/esm/path/StrokeProfile.js +387 -0
- package/dist/esm/path/StrokeProfileStop.js +207 -0
- package/dist/esm/path/boolean.js +284 -0
- package/dist/esm/path/bounds.js +257 -0
- package/dist/esm/path/crossing.js +114 -0
- package/dist/esm/path/draw.js +161 -0
- package/dist/esm/path/hitTest.js +259 -0
- package/dist/esm/path/offset.js +152 -0
- package/dist/esm/path/shapes.js +231 -0
- package/dist/esm/path/smooth.js +84 -0
- package/dist/esm/path/values.js +40 -0
- package/dist/esm/scope/PaperScope.js +288 -0
- package/dist/esm/scope/PaperScopeItem.js +74 -0
- package/dist/esm/smoothing/index.js +71 -0
- package/dist/esm/style/Color.js +917 -0
- package/dist/esm/style/Gradient.js +191 -0
- package/dist/esm/style/GradientStop.js +168 -0
- package/dist/esm/style/PatternDefinition.js +383 -0
- package/dist/esm/style/Style.js +503 -0
- package/dist/esm/svg/index.js +27 -0
- package/dist/esm/svg/svgElement.js +97 -0
- package/dist/esm/svg/svgExport.js +847 -0
- package/dist/esm/svg/svgImport.js +1101 -0
- package/dist/esm/svg/svgStyles.js +96 -0
- package/dist/esm/text/AreaText.js +190 -0
- package/dist/esm/text/PathText.js +111 -0
- package/dist/esm/text/PointText.js +92 -0
- package/dist/esm/text/TextItem.js +538 -0
- package/dist/esm/text/TextRange.js +169 -0
- package/dist/esm/text/TextStory.js +394 -0
- package/dist/esm/text/areaLayout.js +439 -0
- package/dist/esm/text/layout.js +265 -0
- package/dist/esm/text/measure.js +57 -0
- package/dist/esm/text/pathLayout.js +93 -0
- package/dist/esm/tool/Tool.js +284 -0
- package/dist/esm/tool/ToolEvent.js +232 -0
- package/dist/esm/util/Formatter.js +57 -0
- package/dist/esm/util/collisions.js +216 -0
- package/dist/esm/util/list.js +110 -0
- package/dist/esm/util/numerical.js +656 -0
- package/dist/esm/util/object.js +81 -0
- package/dist/esm/util/string.js +29 -0
- package/dist/esm/util/uid.js +31 -0
- package/dist/esm/values/arcs.js +243 -0
- package/dist/esm/values/curve.js +975 -0
- package/dist/esm/values/fit.js +290 -0
- package/dist/esm/values/index.js +23 -0
- package/dist/esm/values/intersections.js +869 -0
- package/dist/esm/values/matrix.js +114 -0
- package/dist/esm/values/path.js +174 -0
- package/dist/esm/values/profile.js +221 -0
- package/dist/esm/values/region.js +63 -0
- package/dist/esm/values/smooth.js +248 -0
- package/dist/esm/values/svg.js +287 -0
- package/dist/esm/view/CanvasView.js +106 -0
- package/dist/esm/view/View.js +775 -0
- package/dist/esm/view/input.js +527 -0
- package/dist/paper-full.js +33083 -22681
- package/dist/paper-full.min.js +36 -31
- package/dist/paper-hosted.js +33898 -23223
- package/dist/paper-hosted.min.js +37 -0
- package/dist/paper-slim.js +28581 -0
- package/dist/paper-slim.min.js +17 -0
- package/dist/types/anim/Tween.d.ts +164 -0
- package/dist/types/basic/Line.d.ts +136 -0
- package/dist/types/basic/Matrix.d.ts +387 -0
- package/dist/types/basic/Point.d.ts +781 -0
- package/dist/types/basic/Rectangle.d.ts +550 -0
- package/dist/types/basic/Size.d.ts +457 -0
- package/dist/types/boolean/graph.d.ts +123 -0
- package/dist/types/boolean/index.d.ts +74 -0
- package/dist/types/boolean/operations.d.ts +9 -0
- package/dist/types/boolean/split.d.ts +19 -0
- package/dist/types/canvas/blendModes.d.ts +18 -0
- package/dist/types/canvas/canvasProvider.d.ts +32 -0
- package/dist/types/constants.d.ts +112 -0
- package/dist/types/core/Base.d.ts +50 -0
- package/dist/types/core/Emitter.d.ts +49 -0
- package/dist/types/core/features.d.ts +41 -0
- package/dist/types/core/serialization.d.ts +44 -0
- package/dist/types/core.d.ts +18 -0
- package/dist/types/dom/domElement.d.ts +33 -0
- package/dist/types/dom/domEvent.d.ts +29 -0
- package/dist/types/env.d.ts +26 -0
- package/dist/types/event/DrawEvent.d.ts +27 -0
- package/dist/types/event/Event.d.ts +47 -0
- package/dist/types/event/Key.d.ts +82 -0
- package/dist/types/event/KeyEvent.d.ts +35 -0
- package/dist/types/event/MouseEvent.d.ts +78 -0
- package/dist/types/hosted/HostItem.d.ts +26 -0
- package/dist/types/hosted/Hosting.d.ts +273 -0
- package/dist/types/hosted/index.d.ts +10 -0
- package/dist/types/index.d.ts +64 -0
- package/dist/types/internal.d.ts +101 -0
- package/dist/types/item/Effect.d.ts +128 -0
- package/dist/types/item/EffectInstance.d.ts +81 -0
- package/dist/types/item/Group.d.ts +225 -0
- package/dist/types/item/HitResult.d.ts +94 -0
- package/dist/types/item/Item.d.ts +2927 -0
- package/dist/types/item/ItemDefinition.d.ts +25 -0
- package/dist/types/item/Layer.d.ts +80 -0
- package/dist/types/item/Project.d.ts +748 -0
- package/dist/types/item/Raster.d.ts +431 -0
- package/dist/types/item/Shape.d.ts +219 -0
- package/dist/types/item/SymbolDefinition.d.ts +98 -0
- package/dist/types/item/SymbolItem.d.ts +92 -0
- package/dist/types/item/bounds.d.ts +56 -0
- package/dist/types/item/draw.d.ts +23 -0
- package/dist/types/item/effects.d.ts +22 -0
- package/dist/types/item/expand.d.ts +7 -0
- package/dist/types/item/hitTest.d.ts +10 -0
- package/dist/types/item/hosting.d.ts +20 -0
- package/dist/types/item/props.d.ts +35 -0
- package/dist/types/item/query.d.ts +14 -0
- package/dist/types/item/sampling.d.ts +5 -0
- package/dist/types/item/transform.d.ts +3 -0
- package/dist/types/net/http.d.ts +6 -0
- package/dist/types/node/CanvasAdapter.d.ts +19 -0
- package/dist/types/node/index.d.ts +60 -0
- package/dist/types/node/peers.d.ts +2 -0
- package/dist/types/offset/curves.d.ts +22 -0
- package/dist/types/offset/index.d.ts +65 -0
- package/dist/types/offset/offset.d.ts +18 -0
- package/dist/types/offset/outline.d.ts +14 -0
- package/dist/types/offset/pen.d.ts +36 -0
- package/dist/types/offset/profile.d.ts +37 -0
- package/dist/types/offset/round.d.ts +18 -0
- package/dist/types/offset/sweep.d.ts +44 -0
- package/dist/types/paperscript/PaperScript.d.ts +114 -0
- package/dist/types/paperscript/autoload.d.ts +1 -0
- package/dist/types/paperscript/index.d.ts +7 -0
- package/dist/types/path/CompoundPath.d.ts +183 -0
- package/dist/types/path/Curve.d.ts +567 -0
- package/dist/types/path/CurveLocation.d.ts +164 -0
- package/dist/types/path/Path.d.ts +1361 -0
- package/dist/types/path/PathFlattener.d.ts +48 -0
- package/dist/types/path/PathItem.d.ts +1190 -0
- package/dist/types/path/Segment.d.ts +335 -0
- package/dist/types/path/SegmentPoint.d.ts +1 -0
- package/dist/types/path/StrokeProfile.d.ts +74 -0
- package/dist/types/path/StrokeProfileStop.d.ts +110 -0
- package/dist/types/path/boolean.d.ts +24 -0
- package/dist/types/path/bounds.d.ts +24 -0
- package/dist/types/path/crossing.d.ts +2 -0
- package/dist/types/path/draw.d.ts +6 -0
- package/dist/types/path/hitTest.d.ts +8 -0
- package/dist/types/path/offset.d.ts +14 -0
- package/dist/types/path/shapes.d.ts +20 -0
- package/dist/types/path/smooth.d.ts +18 -0
- package/dist/types/path/values.d.ts +8 -0
- package/dist/types/scope/PaperScope.d.ts +182 -0
- package/dist/types/scope/PaperScopeItem.d.ts +26 -0
- package/dist/types/smoothing/index.d.ts +26 -0
- package/dist/types/style/Color.d.ts +829 -0
- package/dist/types/style/Gradient.d.ts +130 -0
- package/dist/types/style/GradientStop.d.ts +126 -0
- package/dist/types/style/PatternDefinition.d.ts +185 -0
- package/dist/types/style/Style.d.ts +757 -0
- package/dist/types/svg/index.d.ts +14 -0
- package/dist/types/svg/svgElement.d.ts +32 -0
- package/dist/types/svg/svgExport.d.ts +19 -0
- package/dist/types/svg/svgImport.d.ts +13 -0
- package/dist/types/svg/svgStyles.d.ts +20 -0
- package/dist/types/text/AreaText.d.ts +104 -0
- package/dist/types/text/PathText.d.ts +78 -0
- package/dist/types/text/PointText.d.ts +50 -0
- package/dist/types/text/TextItem.d.ts +163 -0
- package/dist/types/text/TextRange.d.ts +98 -0
- package/dist/types/text/TextStory.d.ts +119 -0
- package/dist/types/text/areaLayout.d.ts +3 -0
- package/dist/types/text/layout.d.ts +43 -0
- package/dist/types/text/measure.d.ts +7 -0
- package/dist/types/text/pathLayout.d.ts +3 -0
- package/dist/types/tool/Tool.d.ts +356 -0
- package/dist/types/tool/ToolEvent.d.ts +134 -0
- package/dist/types/util/Formatter.d.ts +34 -0
- package/dist/types/util/collisions.d.ts +77 -0
- package/dist/types/util/list.d.ts +23 -0
- package/dist/types/util/numerical.d.ts +196 -0
- package/dist/types/util/object.d.ts +19 -0
- package/dist/types/util/string.d.ts +12 -0
- package/dist/types/util/uid.d.ts +7 -0
- package/dist/types/values/arcs.d.ts +10 -0
- package/dist/types/values/curve.d.ts +42 -0
- package/dist/types/values/fit.d.ts +26 -0
- package/dist/types/values/index.d.ts +10 -0
- package/dist/types/values/intersections.d.ts +11 -0
- package/dist/types/values/matrix.d.ts +19 -0
- package/dist/types/values/path.d.ts +26 -0
- package/dist/types/values/profile.d.ts +41 -0
- package/dist/types/values/region.d.ts +13 -0
- package/dist/types/values/smooth.d.ts +39 -0
- package/dist/types/values/svg.d.ts +47 -0
- package/dist/types/view/CanvasView.d.ts +37 -0
- package/dist/types/view/View.d.ts +700 -0
- package/dist/types/view/input.d.ts +28 -0
- package/package.json +90 -58
- package/dist/docs/assets/css/docs.css +0 -414
- package/dist/docs/assets/js/codemirror.js +0 -4
- package/dist/docs/assets/js/docs.js +0 -493
- package/dist/docs/assets/js/jquery.js +0 -6
- package/dist/docs/assets/js/paper.js +0 -23759
- package/dist/docs/classes/AreaText.html +0 -8708
- package/dist/docs/classes/ChangeFlag.html +0 -385
- package/dist/docs/classes/Color.html +0 -2205
- package/dist/docs/classes/CompoundPath.html +0 -9168
- package/dist/docs/classes/Curve.html +0 -2326
- package/dist/docs/classes/CurveLocation.html +0 -673
- package/dist/docs/classes/Event.html +0 -177
- package/dist/docs/classes/Gradient.html +0 -241
- package/dist/docs/classes/GradientStop.html +0 -274
- package/dist/docs/classes/Group.html +0 -7139
- package/dist/docs/classes/HitResult.html +0 -241
- package/dist/docs/classes/HostItem.html +0 -6836
- package/dist/docs/classes/Item.html +0 -6892
- package/dist/docs/classes/Key.html +0 -140
- package/dist/docs/classes/KeyEvent.html +0 -309
- package/dist/docs/classes/Layer.html +0 -7122
- package/dist/docs/classes/Matrix.html +0 -1745
- package/dist/docs/classes/MouseEvent.html +0 -356
- package/dist/docs/classes/PaperScope.html +0 -548
- package/dist/docs/classes/PaperScript.html +0 -202
- package/dist/docs/classes/Path.html +0 -12064
- package/dist/docs/classes/PathItem.html +0 -8930
- package/dist/docs/classes/PathText.html +0 -8578
- package/dist/docs/classes/Point.html +0 -2860
- package/dist/docs/classes/PointText.html +0 -8489
- package/dist/docs/classes/Project.html +0 -1604
- package/dist/docs/classes/Raster.html +0 -8069
- package/dist/docs/classes/Rectangle.html +0 -1698
- package/dist/docs/classes/Segment.html +0 -1017
- package/dist/docs/classes/Shape.html +0 -7570
- package/dist/docs/classes/Size.html +0 -1915
- package/dist/docs/classes/Style.html +0 -1765
- package/dist/docs/classes/SymbolDefinition.html +0 -312
- package/dist/docs/classes/SymbolItem.html +0 -6999
- package/dist/docs/classes/TextItem.html +0 -8332
- package/dist/docs/classes/TextRange.html +0 -186
- package/dist/docs/classes/TextStory.html +0 -355
- package/dist/docs/classes/Tool.html +0 -888
- package/dist/docs/classes/ToolEvent.html +0 -525
- package/dist/docs/classes/Tween.html +0 -390
- package/dist/docs/classes/View.html +0 -2108
- package/dist/docs/classes/global.html +0 -426
- package/dist/docs/classes/index.html +0 -88
- package/dist/docs/index.html +0 -11
- package/dist/node/canvas.js +0 -68
- package/dist/node/extend.js +0 -156
- package/dist/node/self.js +0 -67
- package/dist/paper-core.d.ts +0 -1
- package/dist/paper-core.js +0 -21834
- package/dist/paper-core.min.js +0 -32
- package/dist/paper.d.ts +0 -9630
- package/examples/Animated/AnimatedStar.html +0 -53
- package/examples/Animated/BooleanOperations.html +0 -115
- package/examples/Animated/CandyCrash.html +0 -151
- package/examples/Animated/Extruded.html +0 -50
- package/examples/Animated/Lines.html +0 -53
- package/examples/Animated/Smoothing.html +0 -70
- package/examples/Animated/Space.html +0 -86
- package/examples/Animated/SpaceUsingShapes.html +0 -79
- package/examples/Games/Paperoids.html +0 -569
- package/examples/JSON/Circle Testing.html +0 -34
- package/examples/JSON/Compound Path.html +0 -25
- package/examples/JSON/Empty Path Testing.html +0 -31
- package/examples/JSON/Gradients.html +0 -34
- package/examples/JSON/Group Transform.html +0 -28
- package/examples/JSON/Line Testing.html +0 -44
- package/examples/JSON/Random Path Testing.html +0 -42
- package/examples/JSON/Raster.html +0 -29
- package/examples/JSON/Rect and Attribute Testing.html +0 -56
- package/examples/JSON/Rotated Primitives.html +0 -55
- package/examples/JSON/Selection.html +0 -43
- package/examples/JSON/Shapes.html +0 -54
- package/examples/JSON/Symbols.html +0 -30
- package/examples/JSON/Text Testing.html +0 -38
- package/examples/JSON/Tiger.html +0 -19
- package/examples/JSON/Transform Test 1.html +0 -32
- package/examples/JSON/Transform Test 2.html +0 -30
- package/examples/Node.js/AnimatedStar.js +0 -52
- package/examples/Node.js/BooleanOperations.js +0 -36
- package/examples/Node.js/JSONtoPDF.js +0 -20
- package/examples/Node.js/Raster.js +0 -32
- package/examples/Node.js/RasterRemote.js +0 -33
- package/examples/Node.js/SvgExport.js +0 -52
- package/examples/Node.js/SvgImport.js +0 -23
- package/examples/Node.js/Tadpoles.js +0 -16
- package/examples/Node.js/Tadpoles.pjs +0 -278
- package/examples/Node.js/in.json +0 -1
- package/examples/Node.js/in.svg +0 -16
- package/examples/Paperjs.org/BooleanOperattions.html +0 -115
- package/examples/Paperjs.org/BouncingBalls.html +0 -103
- package/examples/Paperjs.org/Chain.html +0 -54
- package/examples/Paperjs.org/DivisionRaster.html +0 -72
- package/examples/Paperjs.org/FutureSplash.html +0 -122
- package/examples/Paperjs.org/HitTesting.html +0 -103
- package/examples/Paperjs.org/InteractiveTiger.html +0 -777
- package/examples/Paperjs.org/MetaBalls.html +0 -132
- package/examples/Paperjs.org/NyanRainbow.html +0 -202
- package/examples/Paperjs.org/PathIntersections.html +0 -76
- package/examples/Paperjs.org/Qbertify.html +0 -146
- package/examples/Paperjs.org/RadialRainbows.html +0 -80
- package/examples/Paperjs.org/RoundedRectangles.html +0 -41
- package/examples/Paperjs.org/SatieLikedToDraw.html +0 -140
- package/examples/Paperjs.org/Simplify.html +0 -63
- package/examples/Paperjs.org/SpiralRaster.html +0 -120
- package/examples/Paperjs.org/Tadpoles.html +0 -283
- package/examples/Paperjs.org/Voronoi.html +0 -134
- package/examples/Rasters/PhyllotaxisRaster.html +0 -96
- package/examples/Rasters/Raster.html +0 -34
- package/examples/Rasters/RotationRaster.html +0 -44
- package/examples/Rasters/Smoothing.html +0 -36
- package/examples/SVG Export/Circle Testing.html +0 -32
- package/examples/SVG Export/Clipping.html +0 -17
- package/examples/SVG Export/Compound Path.html +0 -22
- package/examples/SVG Export/Empty Path Testing.html +0 -28
- package/examples/SVG Export/Gradients.html +0 -52
- package/examples/SVG Export/Group Transform.html +0 -25
- package/examples/SVG Export/Line Testing.html +0 -41
- package/examples/SVG Export/Random Path Testing.html +0 -39
- package/examples/SVG Export/Raster.html +0 -21
- package/examples/SVG Export/Rect and Attribute Testing.html +0 -53
- package/examples/SVG Export/Rotated Primitives.html +0 -80
- package/examples/SVG Export/Shapes.html +0 -58
- package/examples/SVG Export/Symbols.html +0 -27
- package/examples/SVG Export/Text Testing.html +0 -45
- package/examples/SVG Export/Tiger.html +0 -23
- package/examples/SVG Export/Transform Test 1.html +0 -29
- package/examples/SVG Export/Transform Test 2.html +0 -27
- package/examples/SVG Import/Arcs.html +0 -78
- package/examples/SVG Import/Butterfly.html +0 -223
- package/examples/SVG Import/Circle and Ellipse Testing.html +0 -26
- package/examples/SVG Import/Clipping.html +0 -100
- package/examples/SVG Import/From File.html +0 -30
- package/examples/SVG Import/Gradient.html +0 -138
- package/examples/SVG Import/Gradients.html +0 -30
- package/examples/SVG Import/Inkscape Pivot.html +0 -52
- package/examples/SVG Import/Inkscape.html +0 -88
- package/examples/SVG Import/Line Testing.html +0 -24
- package/examples/SVG Import/MoreGradients.html +0 -107
- package/examples/SVG Import/Multiple Paths Test 1.html +0 -42
- package/examples/SVG Import/Multiple Paths Test 2.html +0 -33
- package/examples/SVG Import/Nested Groups Test.html +0 -78
- package/examples/SVG Import/Polybezier.html +0 -43
- package/examples/SVG Import/Rect Testing.html +0 -25
- package/examples/SVG Import/Symbols.html +0 -24
- package/examples/SVG Import/Testing.html +0 -40
- package/examples/SVG Import/Text Testing.html +0 -19
- package/examples/SVG Import/Tiger.html +0 -742
- package/examples/SVG Import/Transform Testing.html +0 -22
- package/examples/SVG Import/Viewbox.html +0 -38
- package/examples/Scripts/Arcs.html +0 -39
- package/examples/Scripts/AreaText.html +0 -65
- package/examples/Scripts/BlendModes.html +0 -77
- package/examples/Scripts/BooleanOperations.html +0 -937
- package/examples/Scripts/CompoundPath.html +0 -19
- package/examples/Scripts/CurveTimeParameterization.html +0 -61
- package/examples/Scripts/HslColor.html +0 -53
- package/examples/Scripts/PathStructure.html +0 -82
- package/examples/Scripts/PathTangents.html +0 -73
- package/examples/Scripts/PathText.html +0 -48
- package/examples/Scripts/Resize.html +0 -42
- package/examples/Scripts/RoundRectangle.html +0 -28
- package/examples/Scripts/Shapes.html +0 -54
- package/examples/Scripts/StrokeBounds.html +0 -101
- package/examples/Scripts/StrokeScaling.html +0 -65
- package/examples/Scripts/TextRanges.html +0 -51
- package/examples/Tools/BezierTool.html +0 -90
- package/examples/Tools/Circles.html +0 -28
- package/examples/Tools/Clouds.html +0 -35
- package/examples/Tools/DrippingBrush.html +0 -73
- package/examples/Tools/FancyBrush.html +0 -82
- package/examples/Tools/Grid.html +0 -44
- package/examples/Tools/MultiLines.html +0 -44
- package/examples/Tools/MultipleTools.html +0 -44
- package/examples/Tools/PathEditing.html +0 -95
- package/examples/Tools/SquareRounded.html +0 -76
- package/examples/Tools/Stars.html +0 -38
- package/examples/Tools/Vektor.html +0 -200
- package/examples/Tools/Wave.html +0 -50
- package/examples/Tools/WormFarm.html +0 -88
- package/examples/Worker/Main.html +0 -53
- package/examples/Worker/Worker.js +0 -14
- package/examples/css/style.css +0 -12
|
@@ -0,0 +1,950 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
|
3
|
+
* http://paperjs.org/
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) 2011 - 2026, Jürg Lehni & Jonathan Puckey
|
|
6
|
+
* http://juerglehni.com/ & https://puckey.studio/
|
|
7
|
+
*
|
|
8
|
+
* Distributed under the MIT license. See LICENSE file for details.
|
|
9
|
+
*
|
|
10
|
+
* All rights reserved.
|
|
11
|
+
*/
|
|
12
|
+
// The planar graph that the boolean operations trace their results from, see
|
|
13
|
+
// createGraph() and traceGraph(). It works on plain descriptions of paths,
|
|
14
|
+
// and only uses the basic types and the functions on curve values.
|
|
15
|
+
import { CURVETIME_EPSILON, GEOMETRIC_EPSILON, getEpsilon } from "../util/numerical.js";
|
|
16
|
+
import { findBoundsCollisions, findCurveBoundsCollisions } from "../util/collisions.js";
|
|
17
|
+
import { Point } from "../basic/Point.js";
|
|
18
|
+
import { getCurve, getCurveArea, getCurveIntersections, getCurveLength, getCurvePart, getCurvePoint, getCurveRayWinding, getCurveReversals, getCurveSelfIntersection, getCurveTangent, getCurveTimeAt, getNearestCurveTime, getPathSegments, getRegionBounds, hasHandles } from "../values/index.js";
|
|
19
|
+
const { min, max, abs } = Math;
|
|
20
|
+
// Pieces shorter than this many epsilons merge into their vertices, see
|
|
21
|
+
// mergeVertices():
|
|
22
|
+
const maxPiece = 4;
|
|
23
|
+
/**
|
|
24
|
+
* Creates a planar graph of the operands' curves: All intersections become
|
|
25
|
+
* vertices, the pieces of curves between them edges, and the edges bound
|
|
26
|
+
* faces with a known winding for each operand.
|
|
27
|
+
*
|
|
28
|
+
* @param operands one or two operands
|
|
29
|
+
* @return the graph's vertices, edges, half-edges and faces
|
|
30
|
+
*/
|
|
31
|
+
export function createGraph(operands) {
|
|
32
|
+
const graph = {
|
|
33
|
+
...readCurves(operands),
|
|
34
|
+
pieces: [],
|
|
35
|
+
vertices: [],
|
|
36
|
+
edges: [],
|
|
37
|
+
halfEdges: [],
|
|
38
|
+
faces: []
|
|
39
|
+
};
|
|
40
|
+
const points = {
|
|
41
|
+
cuts: [],
|
|
42
|
+
xs: [],
|
|
43
|
+
ys: [],
|
|
44
|
+
isEnds: [],
|
|
45
|
+
parents: [],
|
|
46
|
+
vertexIndices: {}
|
|
47
|
+
};
|
|
48
|
+
const collisions = cutCurves(graph, points);
|
|
49
|
+
mergeVertices(graph, points, collisions);
|
|
50
|
+
createEdges(graph, points);
|
|
51
|
+
sortHalfEdges(graph);
|
|
52
|
+
collectFaces(graph);
|
|
53
|
+
propagateWindings(graph);
|
|
54
|
+
return graph;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Traces the outlines of the areas that the operator includes, along the
|
|
58
|
+
* edges of the operands' graph that have the result on their left and not
|
|
59
|
+
* on their right. At each vertex, we turn through the result until the
|
|
60
|
+
* next of them, which keeps contours apart that only touch.
|
|
61
|
+
*
|
|
62
|
+
* @param operands one or two operands, with closed paths only
|
|
63
|
+
* @param operator decides if a face is part of the result, from
|
|
64
|
+
* whether it is inside each of the operands
|
|
65
|
+
* @return the traced paths
|
|
66
|
+
*/
|
|
67
|
+
export function traceGraph(operands, operator) {
|
|
68
|
+
const graph = createGraph(operands);
|
|
69
|
+
const { halfEdges, faces } = graph;
|
|
70
|
+
for (const face of faces) {
|
|
71
|
+
// propagateWindings() gave all faces their windings:
|
|
72
|
+
const windings = face.windings;
|
|
73
|
+
face.isInside = !!operator(isFilled(graph, windings, 0), isFilled(graph, windings, 1));
|
|
74
|
+
}
|
|
75
|
+
function isBoundary(index) {
|
|
76
|
+
return (!!halfEdges[index].face.isInside &&
|
|
77
|
+
!halfEdges[index ^ 1].face.isInside);
|
|
78
|
+
}
|
|
79
|
+
// The curve times along a half-edge, and if the next one continues
|
|
80
|
+
// it along the same curve:
|
|
81
|
+
function getTimes(index) {
|
|
82
|
+
const { edge } = halfEdges[index];
|
|
83
|
+
return index & 1 ? [edge.time2, edge.time1] : [edge.time1, edge.time2];
|
|
84
|
+
}
|
|
85
|
+
function isContinued(index, next) {
|
|
86
|
+
return (halfEdges[index].edge.curve === halfEdges[next].edge.curve &&
|
|
87
|
+
getTimes(index)[1] === getTimes(next)[0]);
|
|
88
|
+
}
|
|
89
|
+
const paths = [];
|
|
90
|
+
const isVisited = [];
|
|
91
|
+
for (let i = 0, l = halfEdges.length; i < l; i++) {
|
|
92
|
+
if (!isVisited[i] && isBoundary(i)) {
|
|
93
|
+
const contour = [];
|
|
94
|
+
let index = i;
|
|
95
|
+
do {
|
|
96
|
+
isVisited[index] = true;
|
|
97
|
+
contour.push(index);
|
|
98
|
+
index = halfEdges[index].next;
|
|
99
|
+
while (!isBoundary(index)) {
|
|
100
|
+
index = halfEdges[index ^ 1].next;
|
|
101
|
+
}
|
|
102
|
+
} while (index !== i);
|
|
103
|
+
// Start where a piece doesn't continue the one before it, as
|
|
104
|
+
// the pieces of a closed curve don't continue across its end:
|
|
105
|
+
const { length } = contour;
|
|
106
|
+
let start = 0;
|
|
107
|
+
while (start < length &&
|
|
108
|
+
isContinued(contour[(start || length) - 1], contour[start])) {
|
|
109
|
+
start++;
|
|
110
|
+
}
|
|
111
|
+
const { segments, parts } = joinPieces(graph, Array.from({ length }, (_, j) => {
|
|
112
|
+
const index = contour[(start + j) % length];
|
|
113
|
+
const [time1, time2] = getTimes(index);
|
|
114
|
+
const { edge, values } = halfEdges[index];
|
|
115
|
+
return { curve: edge.curve, edge: index >> 1, time1, time2, values };
|
|
116
|
+
}));
|
|
117
|
+
// The contour ends where it starts:
|
|
118
|
+
segments[0][1] = segments.pop()[1];
|
|
119
|
+
paths.push({
|
|
120
|
+
segments,
|
|
121
|
+
sources: parts.map(({ curve, time1, time2 }) => {
|
|
122
|
+
const { operand, path, curve: index } = graph.curves[curve];
|
|
123
|
+
return { operand, path, curve: index, time1, time2 };
|
|
124
|
+
})
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return paths;
|
|
129
|
+
}
|
|
130
|
+
// Whether the windings fill the operand by its fill rule:
|
|
131
|
+
export function isFilled(graph, windings, operand) {
|
|
132
|
+
const winding = windings[operand];
|
|
133
|
+
return graph.nonZeros[operand] ? !!winding : !!(winding & 1);
|
|
134
|
+
}
|
|
135
|
+
// Returns the segments along the pieces, from the start of the first one to the
|
|
136
|
+
// end of the last one, with the pieces of the same curve that follow each
|
|
137
|
+
// other joined back into one part of it, and these parts:
|
|
138
|
+
export function joinPieces(graph, pieces) {
|
|
139
|
+
const segments = [];
|
|
140
|
+
const parts = [];
|
|
141
|
+
let handleIn = [0, 0];
|
|
142
|
+
for (let i = 0, l = pieces.length; i < l;) {
|
|
143
|
+
const first = pieces[i];
|
|
144
|
+
let last = first;
|
|
145
|
+
while (++i < l &&
|
|
146
|
+
pieces[i].curve === first.curve &&
|
|
147
|
+
pieces[i].time1 === last.time2) {
|
|
148
|
+
last = pieces[i];
|
|
149
|
+
}
|
|
150
|
+
const { curve, time1 } = first;
|
|
151
|
+
const { time2 } = last;
|
|
152
|
+
const { hasHandles } = graph.curves[curve];
|
|
153
|
+
const v = first === last
|
|
154
|
+
? first.values
|
|
155
|
+
: getPart(graph.values[curve], hasHandles, time1, time2, { x: first.values[0], y: first.values[1] }, { x: last.values[6], y: last.values[7] });
|
|
156
|
+
segments.push([
|
|
157
|
+
[v[0], v[1]],
|
|
158
|
+
handleIn,
|
|
159
|
+
hasHandles ? [v[2] - v[0], v[3] - v[1]] : [0, 0]
|
|
160
|
+
]);
|
|
161
|
+
handleIn = hasHandles ? [v[4] - v[6], v[5] - v[7]] : [0, 0];
|
|
162
|
+
parts.push({ curve, time1, time2 });
|
|
163
|
+
if (i === l) {
|
|
164
|
+
segments.push([[v[6], v[7]], handleIn, [0, 0]]);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return { segments, parts };
|
|
168
|
+
}
|
|
169
|
+
// Read the curves of the operands' paths as their values, each with its
|
|
170
|
+
// neighbors and where it comes from, along with the fill rules, and the
|
|
171
|
+
// distance under which points count as the same:
|
|
172
|
+
function readCurves(operands) {
|
|
173
|
+
const curves = [];
|
|
174
|
+
const values = [];
|
|
175
|
+
const nonZeros = [];
|
|
176
|
+
let bounds = null;
|
|
177
|
+
for (let i = 0, l = operands.length; i < l; i++) {
|
|
178
|
+
const operand = operands[i];
|
|
179
|
+
const { paths } = operand;
|
|
180
|
+
nonZeros.push(operand.fillRule === 'nonzero');
|
|
181
|
+
bounds = bounds
|
|
182
|
+
? bounds.unite(getRegionBounds(operand))
|
|
183
|
+
: getRegionBounds(operand);
|
|
184
|
+
for (let j = 0, k = paths.length; j < k; j++) {
|
|
185
|
+
// Open paths of regions are filled as if closed by a straight line:
|
|
186
|
+
const isClosing = !paths[j].closed && !!operand.fillRule;
|
|
187
|
+
const closed = paths[j].closed || isClosing;
|
|
188
|
+
const segments = getPathSegments(paths[j]);
|
|
189
|
+
const count = segments.length;
|
|
190
|
+
const first = curves.length;
|
|
191
|
+
for (let m = 0, n = closed ? count : count - 1; m < n; m++) {
|
|
192
|
+
const segment1 = segments[m];
|
|
193
|
+
const segment2 = segments[(m + 1) % count];
|
|
194
|
+
const straight = isClosing && m === count - 1;
|
|
195
|
+
values.push(getCurve(segment1, segment2, straight));
|
|
196
|
+
curves.push({
|
|
197
|
+
next: null,
|
|
198
|
+
previous: null,
|
|
199
|
+
index: curves.length,
|
|
200
|
+
operand: i,
|
|
201
|
+
path: j,
|
|
202
|
+
curve: m,
|
|
203
|
+
hasHandles: !straight && hasHandles(segment1, segment2)
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
// The curves of closed paths follow each other around their ends:
|
|
207
|
+
for (let m = first, n = curves.length; m < n; m++) {
|
|
208
|
+
const curve = curves[m];
|
|
209
|
+
curve.next = m + 1 < n ? curves[m + 1] : closed ? curves[first] : null;
|
|
210
|
+
curve.previous =
|
|
211
|
+
m > first ? curves[m - 1] : closed ? curves[n - 1] : null;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
// Points closer than epsilon are one vertex. It is relative to the size
|
|
216
|
+
// of the operands, but not below the resolution of their coordinates:
|
|
217
|
+
// There is at least one operand:
|
|
218
|
+
const { left, right, top, bottom, topLeft, bottomRight } = bounds;
|
|
219
|
+
const epsilon = getEpsilon(max(abs(left), abs(right), abs(top), abs(bottom)), topLeft.getDistance(bottomRight) * GEOMETRIC_EPSILON);
|
|
220
|
+
return { curves, values, nonZeros, epsilon };
|
|
221
|
+
}
|
|
222
|
+
// Cut the curves at their ends and at all intersections, including
|
|
223
|
+
// those within the same operand and the ends of overlaps:
|
|
224
|
+
function cutCurves(graph, points) {
|
|
225
|
+
const { curves, values, epsilon } = graph;
|
|
226
|
+
const { cuts } = points;
|
|
227
|
+
for (let i = 0, l = curves.length; i < l; i++) {
|
|
228
|
+
const v = values[i];
|
|
229
|
+
cuts.push([]);
|
|
230
|
+
addCut(points, i, 0, new Point(v[0], v[1]), true);
|
|
231
|
+
addCut(points, i, 1, new Point(v[6], v[7]), true);
|
|
232
|
+
}
|
|
233
|
+
// The curves whose bounds come within 2 epsilon of each other, for the
|
|
234
|
+
// intersections and the hot pixels of mergeVertices():
|
|
235
|
+
const collisions = findCurveBoundsCollisions(values, values, 2 * epsilon);
|
|
236
|
+
// Cut both curves wherever two of them intersect, and those with a loop
|
|
237
|
+
// where they cross themselves. An intersection that is found again, at
|
|
238
|
+
// the same points of the same two curves, only cuts them once:
|
|
239
|
+
const hits = [];
|
|
240
|
+
const pairs = {};
|
|
241
|
+
const count = curves.length;
|
|
242
|
+
const addHits = (curve1, curve2, intersections) => {
|
|
243
|
+
for (const [time1, time2] of intersections) {
|
|
244
|
+
const hit1 = getHit(graph, curve1, time1);
|
|
245
|
+
const hit2 = getHit(graph, curve2, time2);
|
|
246
|
+
const others = (pairs[min(hit1.index, hit2.index) * count + max(hit1.index, hit2.index)] ??= []);
|
|
247
|
+
const isSame = (hit, other) => (hit.index === other.index &&
|
|
248
|
+
hit.point.isClose(other.point, GEOMETRIC_EPSILON));
|
|
249
|
+
const isNew = !others.some(([other1, other2]) => (isSame(hit1, other1) && isSame(hit2, other2) ||
|
|
250
|
+
isSame(hit1, other2) && isSame(hit2, other1)));
|
|
251
|
+
if (isNew) {
|
|
252
|
+
others.push([hit1, hit2]);
|
|
253
|
+
hits.push(hit1, hit2);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
for (let i = 0; i < count; i++) {
|
|
258
|
+
const curve1 = curves[i];
|
|
259
|
+
addHits(curve1, curve1, getCurveSelfIntersection(values[i], curve1.previous === curve1));
|
|
260
|
+
for (const j of collisions[i] ?? []) {
|
|
261
|
+
if (j > i) {
|
|
262
|
+
const curve2 = curves[j];
|
|
263
|
+
addHits(curve1, curve2, getCurveIntersections(values[i], values[j], {
|
|
264
|
+
sharesStart: curve1.previous === curve2,
|
|
265
|
+
sharesEnd: curve1.next === curve2,
|
|
266
|
+
tolerance: epsilon
|
|
267
|
+
}));
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
// In their order along the curves, as the first of the points that merge
|
|
272
|
+
// into a vertex gives it its position:
|
|
273
|
+
hits.sort((hit1, hit2) => hit1.index - hit2.index || hit1.time - hit2.time);
|
|
274
|
+
for (const { index, time, point } of hits) {
|
|
275
|
+
addCut(points, index, time, point, false);
|
|
276
|
+
}
|
|
277
|
+
// Curves whose control points lie along a line can reverse along it,
|
|
278
|
+
// and overlap themselves and the other curves along it. Cut them where
|
|
279
|
+
// they reverse, so that their pieces run in one direction, and the
|
|
280
|
+
// hot pixels of mergeVertices() cut the curves they overlap at the same
|
|
281
|
+
// vertices:
|
|
282
|
+
for (let i = 0, l = curves.length; i < l; i++) {
|
|
283
|
+
const times = getCurveReversals(values[i], epsilon);
|
|
284
|
+
for (let j = 0, k = times.length; j < k; j++) {
|
|
285
|
+
addCut(points, i, times[j], getCurvePoint(values[i], times[j]), false);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return collisions;
|
|
289
|
+
}
|
|
290
|
+
// Merge points closer than epsilon into vertices, through union-find
|
|
291
|
+
// on the points sorted by x. A vertex takes the position of a curve end
|
|
292
|
+
// if it has one, so the operands' own vertices stay where they are:
|
|
293
|
+
function mergeVertices(graph, points, collisions) {
|
|
294
|
+
const { curves, values, epsilon } = graph;
|
|
295
|
+
const { cuts, xs, ys } = points;
|
|
296
|
+
const count = xs.length;
|
|
297
|
+
const parents = (points.parents = new Array(count));
|
|
298
|
+
const order = new Array(count);
|
|
299
|
+
for (let i = 0; i < count; i++) {
|
|
300
|
+
parents[i] = order[i] = i;
|
|
301
|
+
}
|
|
302
|
+
order.sort((a, b) => xs[a] - xs[b]);
|
|
303
|
+
for (let i = 0; i < count; i++) {
|
|
304
|
+
const a = order[i];
|
|
305
|
+
for (let j = i + 1; j < count && xs[order[j]] - xs[a] <= epsilon; j++) {
|
|
306
|
+
const b = order[j];
|
|
307
|
+
const dx = xs[b] - xs[a];
|
|
308
|
+
const dy = ys[b] - ys[a];
|
|
309
|
+
if (dx * dx + dy * dy <= epsilon * epsilon) {
|
|
310
|
+
parents[getRoot(parents, a)] = getRoot(parents, b);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
createVertices(graph, points);
|
|
315
|
+
// The last curve that tested each vertex in getHotPixels(), to test
|
|
316
|
+
// each only once per curve:
|
|
317
|
+
let marks;
|
|
318
|
+
// Cut the curves where they pass within epsilon of a vertex, the
|
|
319
|
+
// hot pixels of snap rounding. Moving the pieces to their vertices
|
|
320
|
+
// can bring them within epsilon of further ones, or carry them across
|
|
321
|
+
// them, so the pieces of each cut are tested again, until none passes
|
|
322
|
+
// a vertex. As cuts only use existing vertices, this ends:
|
|
323
|
+
// The vertices that may cut curve i, as the ones within 2 epsilon of
|
|
324
|
+
// its bounds, since its pieces stay within epsilon of it. Each vertex
|
|
325
|
+
// lies on a curve that it cuts, so we find it among the cuts of the
|
|
326
|
+
// curves whose bounds come that close:
|
|
327
|
+
function getHotPixels(i) {
|
|
328
|
+
const v = values[i];
|
|
329
|
+
const left = min(v[0], v[2], v[4], v[6]) - 2 * epsilon;
|
|
330
|
+
const top = min(v[1], v[3], v[5], v[7]) - 2 * epsilon;
|
|
331
|
+
const right = max(v[0], v[2], v[4], v[6]) + 2 * epsilon;
|
|
332
|
+
const bottom = max(v[1], v[3], v[5], v[7]) + 2 * epsilon;
|
|
333
|
+
const indices = collisions[i];
|
|
334
|
+
const hotPixels = [];
|
|
335
|
+
for (let j = 0, k = indices.length; j < k; j++) {
|
|
336
|
+
const list = cuts[indices[j]];
|
|
337
|
+
for (let m = 0, n = list.length; m < n; m++) {
|
|
338
|
+
const index = getVertex(points, list[m].point);
|
|
339
|
+
if (marks[index] !== i) {
|
|
340
|
+
const vertex = graph.vertices[index];
|
|
341
|
+
marks[index] = i;
|
|
342
|
+
if (vertex.x >= left &&
|
|
343
|
+
vertex.x <= right &&
|
|
344
|
+
vertex.y >= top &&
|
|
345
|
+
vertex.y <= bottom) {
|
|
346
|
+
hotPixels.push(index);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return hotPixels.sort((a, b) => a - b);
|
|
352
|
+
}
|
|
353
|
+
// The pieces of a curve that a vertex may cut, as the ones that don't
|
|
354
|
+
// end in it, and whose bounds contain it:
|
|
355
|
+
function isNearPiece(piece, index, vertex) {
|
|
356
|
+
const [left, top, right, bottom] = piece.bounds;
|
|
357
|
+
const { x, y } = vertex;
|
|
358
|
+
return (getVertex(points, piece.cut1.point) !== index &&
|
|
359
|
+
getVertex(points, piece.cut2.point) !== index &&
|
|
360
|
+
x >= left &&
|
|
361
|
+
x <= right &&
|
|
362
|
+
y >= top &&
|
|
363
|
+
y <= bottom);
|
|
364
|
+
}
|
|
365
|
+
// Whether the cut's point moved to its vertex by more than epsilon in a
|
|
366
|
+
// merge. A piece whose ends moved less passes within epsilon of all the
|
|
367
|
+
// vertices that it moved over:
|
|
368
|
+
function isMoved({ point }) {
|
|
369
|
+
const vertex = graph.vertices[getVertex(points, point)];
|
|
370
|
+
const dx = vertex.x - xs[point];
|
|
371
|
+
const dy = vertex.y - ys[point];
|
|
372
|
+
return dx * dx + dy * dy > epsilon * epsilon;
|
|
373
|
+
}
|
|
374
|
+
// Whether the piece passed over the point as it moved to its vertices:
|
|
375
|
+
function isPassed({ values: v, part }, point) {
|
|
376
|
+
return (!!part &&
|
|
377
|
+
getSide(v, point, epsilon) * getSide(part, point, epsilon) < 0);
|
|
378
|
+
}
|
|
379
|
+
function addPiece(pieces, i, cut1, cut2) {
|
|
380
|
+
const v = getPiece(graph, points, i, cut1, cut2);
|
|
381
|
+
const part = isMoved(cut1) || isMoved(cut2)
|
|
382
|
+
? getCurvePart(values[i], cut1.time, cut2.time)
|
|
383
|
+
: null;
|
|
384
|
+
const p = part ?? v;
|
|
385
|
+
pieces.push({
|
|
386
|
+
values: v,
|
|
387
|
+
part,
|
|
388
|
+
bounds: [
|
|
389
|
+
min(v[0], v[2], v[4], v[6], p[0], p[2], p[4], p[6]) - epsilon,
|
|
390
|
+
min(v[1], v[3], v[5], v[7], p[1], p[3], p[5], p[7]) - epsilon,
|
|
391
|
+
max(v[0], v[2], v[4], v[6], p[0], p[2], p[4], p[6]) + epsilon,
|
|
392
|
+
max(v[1], v[3], v[5], v[7], p[1], p[3], p[5], p[7]) + epsilon
|
|
393
|
+
],
|
|
394
|
+
cut1,
|
|
395
|
+
cut2
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
function cutAtHotPixels(i) {
|
|
399
|
+
const candidates = getHotPixels(i);
|
|
400
|
+
if (candidates.length) {
|
|
401
|
+
const list = sortCuts(cuts[i]);
|
|
402
|
+
const pieces = [];
|
|
403
|
+
for (let j = 1, k = list.length; j < k; j++) {
|
|
404
|
+
addPiece(pieces, i, list[j - 1], list[j]);
|
|
405
|
+
}
|
|
406
|
+
while (pieces.length) {
|
|
407
|
+
const piece = pieces.pop();
|
|
408
|
+
for (let j = 0, k = candidates.length; j < k; j++) {
|
|
409
|
+
const index = candidates[j];
|
|
410
|
+
const vertex = graph.vertices[index];
|
|
411
|
+
if (isNearPiece(piece, index, vertex)) {
|
|
412
|
+
const point = new Point(vertex.x, vertex.y);
|
|
413
|
+
const t = getNearestCurveTime(piece.values, point, epsilon);
|
|
414
|
+
// Cut where the piece passes within epsilon of the vertex, or
|
|
415
|
+
// where it passed over it, as it moved to its vertices:
|
|
416
|
+
if (t > 0 &&
|
|
417
|
+
t < 1 && (point.isClose(getCurvePoint(piece.values, t), epsilon) ||
|
|
418
|
+
isPassed(piece, point))) {
|
|
419
|
+
const { cut1, cut2 } = piece;
|
|
420
|
+
const cut = {
|
|
421
|
+
time: curves[i].hasHandles
|
|
422
|
+
? cut1.time + (cut2.time - cut1.time) * t
|
|
423
|
+
: getNearestCurveTime(values[i], point, epsilon),
|
|
424
|
+
point: vertex.point
|
|
425
|
+
};
|
|
426
|
+
cuts[i].push(cut);
|
|
427
|
+
addPiece(pieces, i, cut1, cut);
|
|
428
|
+
addPiece(pieces, i, cut, cut2);
|
|
429
|
+
break;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
// Merge the vertices that pieces shorter than 4 epsilon join: As
|
|
437
|
+
// the hot pixels reach 2 epsilon, the order of the edges around such
|
|
438
|
+
// vertices can't be told apart. Merged vertices span up to about 5
|
|
439
|
+
// epsilon, the precision of the result. Returns true if it merged
|
|
440
|
+
// any, as moving vertices can bring pieces within epsilon of others:
|
|
441
|
+
function mergeShortPieces() {
|
|
442
|
+
const pieces = [];
|
|
443
|
+
let isMerged = false;
|
|
444
|
+
for (let i = 0, l = curves.length; i < l; i++) {
|
|
445
|
+
const list = sortCuts(cuts[i]);
|
|
446
|
+
for (let j = 1, k = list.length; j < k; j++) {
|
|
447
|
+
const cut1 = list[j - 1];
|
|
448
|
+
const cut2 = list[j];
|
|
449
|
+
if (getVertex(points, cut1.point) !== getVertex(points, cut2.point)) {
|
|
450
|
+
// The pieces are at least as long as their ends are
|
|
451
|
+
// apart, so we only measure them if close:
|
|
452
|
+
const v = getPiece(graph, points, i, cut1, cut2);
|
|
453
|
+
const dx = v[6] - v[0];
|
|
454
|
+
const dy = v[7] - v[1];
|
|
455
|
+
if (dx * dx + dy * dy < (maxPiece * epsilon) ** 2 &&
|
|
456
|
+
getCurveLength(v) < maxPiece * epsilon) {
|
|
457
|
+
pieces.push([cut1.point, cut2.point]);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
for (let i = 0, l = pieces.length; i < l; i++) {
|
|
463
|
+
const root1 = getRoot(parents, pieces[i][0]);
|
|
464
|
+
const root2 = getRoot(parents, pieces[i][1]);
|
|
465
|
+
if (root1 !== root2) {
|
|
466
|
+
parents[root1] = root2;
|
|
467
|
+
isMerged = true;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
if (isMerged) {
|
|
471
|
+
createVertices(graph, points);
|
|
472
|
+
}
|
|
473
|
+
return isMerged;
|
|
474
|
+
}
|
|
475
|
+
// Cut at the hot pixels and merge, until no vertices move any more,
|
|
476
|
+
// which ends as each merge leaves fewer of them:
|
|
477
|
+
do {
|
|
478
|
+
marks = new Array(graph.vertices.length);
|
|
479
|
+
for (let i = 0, l = curves.length; i < l; i++) {
|
|
480
|
+
cutAtHotPixels(i);
|
|
481
|
+
}
|
|
482
|
+
} while (mergeShortPieces());
|
|
483
|
+
}
|
|
484
|
+
// Create the edges between the vertices along each curve, with the
|
|
485
|
+
// change in winding of each operand when crossing them from right to
|
|
486
|
+
// left. Coincident pieces become one edge that adds up their changes:
|
|
487
|
+
function createEdges(graph, points) {
|
|
488
|
+
const { curves, edges, pieces, epsilon } = graph;
|
|
489
|
+
const { cuts } = points;
|
|
490
|
+
const edgesByVertex = [];
|
|
491
|
+
function getCoincidence(edge1, edge2) {
|
|
492
|
+
// Compare points along edge2 to edge1, and return the direction
|
|
493
|
+
// of edge2 relative to edge1 if they coincide, or 0:
|
|
494
|
+
const v1 = edge1.values;
|
|
495
|
+
const v2 = edge2.values;
|
|
496
|
+
const times = [];
|
|
497
|
+
for (let i = 1; i <= 3; i++) {
|
|
498
|
+
const point = getCurvePoint(v2, i / 4);
|
|
499
|
+
const time = getNearestCurveTime(v1, point, epsilon);
|
|
500
|
+
if (!point.isClose(getCurvePoint(v1, time), epsilon))
|
|
501
|
+
return 0;
|
|
502
|
+
times.push(time);
|
|
503
|
+
}
|
|
504
|
+
return times[0] < times[2] ? 1 : -1;
|
|
505
|
+
}
|
|
506
|
+
// Adds the edge, or merges it into one that it coincides with, and returns
|
|
507
|
+
// the index of the edge that it became:
|
|
508
|
+
function addEdge(edge) {
|
|
509
|
+
// Look up the edges between the same vertices by the first one:
|
|
510
|
+
const first = min(edge.from, edge.to);
|
|
511
|
+
const last = max(edge.from, edge.to);
|
|
512
|
+
const list = (edgesByVertex[first] ??= []);
|
|
513
|
+
for (let i = 0, l = list.length; i < l; i++) {
|
|
514
|
+
const other = edges[list[i]];
|
|
515
|
+
const direction = (max(other.from, other.to) === last &&
|
|
516
|
+
getCoincidence(other, edge));
|
|
517
|
+
if (direction) {
|
|
518
|
+
for (let j = 0; j < 2; j++) {
|
|
519
|
+
other.windings[j] += direction * edge.windings[j];
|
|
520
|
+
}
|
|
521
|
+
return list[i];
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
list.push(edges.length);
|
|
525
|
+
edges.push(edge);
|
|
526
|
+
return edges.length - 1;
|
|
527
|
+
}
|
|
528
|
+
for (let i = 0, l = curves.length; i < l; i++) {
|
|
529
|
+
const list = cuts[i]; // Sorted in mergeVertices().
|
|
530
|
+
const isStraight = !curves[i].hasHandles;
|
|
531
|
+
pieces[i] = [];
|
|
532
|
+
let prev = list[0];
|
|
533
|
+
for (let j = 1, k = list.length; j < k; j++) {
|
|
534
|
+
const cut = list[j];
|
|
535
|
+
const from = getVertex(points, prev.point);
|
|
536
|
+
const to = getVertex(points, cut.point);
|
|
537
|
+
const part = getPiece(graph, points, i, prev, cut);
|
|
538
|
+
// Drop pieces that start and end in the same vertex, unless
|
|
539
|
+
// they form a loop that is longer than the pieces that vertices
|
|
540
|
+
// merge, see mergeVertices():
|
|
541
|
+
if (from !== to || getCurveLength(part) >= maxPiece * epsilon) {
|
|
542
|
+
const windings = [0, 0];
|
|
543
|
+
windings[curves[i].operand] = 1;
|
|
544
|
+
const edge = addEdge({
|
|
545
|
+
values: part,
|
|
546
|
+
from,
|
|
547
|
+
to,
|
|
548
|
+
isStraight,
|
|
549
|
+
windings,
|
|
550
|
+
curve: i,
|
|
551
|
+
time1: prev.time,
|
|
552
|
+
time2: cut.time
|
|
553
|
+
});
|
|
554
|
+
pieces[i].push({
|
|
555
|
+
curve: i,
|
|
556
|
+
edge,
|
|
557
|
+
time1: prev.time,
|
|
558
|
+
time2: cut.time,
|
|
559
|
+
values: part
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
prev = cut;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
// Sort the half-edges leaving each vertex by angle. Edge i has the
|
|
567
|
+
// half-edges 2 * i, leaving from its start, and 2 * i + 1, leaving
|
|
568
|
+
// from its end. Their angles are measured to points a short distance
|
|
569
|
+
// along them, where tangent curves already separate, but no other
|
|
570
|
+
// vertex is reached yet:
|
|
571
|
+
function sortHalfEdges(graph) {
|
|
572
|
+
const { vertices, edges, halfEdges, epsilon } = graph;
|
|
573
|
+
// The point at the distance along the half-edge. Straight edges keep
|
|
574
|
+
// their handles at their ends, so their times don't run evenly:
|
|
575
|
+
function getPointAt(halfEdge, distance) {
|
|
576
|
+
const v = halfEdge.values;
|
|
577
|
+
const { edge } = halfEdge;
|
|
578
|
+
// The lengths are measured before, see below, and the distance lies
|
|
579
|
+
// within them:
|
|
580
|
+
const t = distance / edge.length;
|
|
581
|
+
return edge.isStraight
|
|
582
|
+
? new Point(v[0] + (v[6] - v[0]) * t, v[1] + (v[7] - v[1]) * t)
|
|
583
|
+
: getCurvePoint(v, getCurveTimeAt(v, distance));
|
|
584
|
+
}
|
|
585
|
+
// Half-edges that leave a vertex tangentially and can't be told apart
|
|
586
|
+
// where their angles are measured: Tell on which side of the first one
|
|
587
|
+
// the second one runs, half way along the shorter one, as they don't
|
|
588
|
+
// cross before they end, or return 0 if it doesn't run alongside the
|
|
589
|
+
// first one there:
|
|
590
|
+
function getOrder(halfEdge1, halfEdge2) {
|
|
591
|
+
const point = getPointAt(halfEdge2, min(halfEdge1.edge.length, halfEdge2.edge.length) / 2);
|
|
592
|
+
return -getSide(halfEdge1.values, point, epsilon);
|
|
593
|
+
}
|
|
594
|
+
// The range of angles of a half-edge's control polygon, which contains
|
|
595
|
+
// all directions in which it leaves its vertex, or null if the range
|
|
596
|
+
// isn't within a half-plane:
|
|
597
|
+
function getDirections(v) {
|
|
598
|
+
const angles = [];
|
|
599
|
+
for (let i = 0; i < 6; i += 2) {
|
|
600
|
+
const dx = v[i + 2] - v[i];
|
|
601
|
+
const dy = v[i + 3] - v[i + 1];
|
|
602
|
+
if (dx || dy) {
|
|
603
|
+
angles.push(Math.atan2(dy, dx));
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
const first = angles[0];
|
|
607
|
+
let from = 0;
|
|
608
|
+
let to = 0;
|
|
609
|
+
for (let i = 1, l = angles.length; i < l; i++) {
|
|
610
|
+
const difference = (((angles[i] - first + 3 * Math.PI) % (2 * Math.PI)) -
|
|
611
|
+
Math.PI);
|
|
612
|
+
from = min(from, difference);
|
|
613
|
+
to = max(to, difference);
|
|
614
|
+
}
|
|
615
|
+
return to - from < Math.PI
|
|
616
|
+
? {
|
|
617
|
+
start: (first + from + 2 * Math.PI) % (2 * Math.PI),
|
|
618
|
+
width: to - from
|
|
619
|
+
}
|
|
620
|
+
: null;
|
|
621
|
+
}
|
|
622
|
+
// Sort the half-edges by their ranges of directions and return true,
|
|
623
|
+
// if these are apart far enough that the points where their angles
|
|
624
|
+
// are measured below lie over 2 epsilon apart: These points lie within
|
|
625
|
+
// the ranges, at least half the shortest chord along the half-edges,
|
|
626
|
+
// and so at least cos(width / 2) times that away from the vertex:
|
|
627
|
+
function sortByDirections(outgoing) {
|
|
628
|
+
let distance = Infinity;
|
|
629
|
+
for (let j = 0, k = outgoing.length; j < k; j++) {
|
|
630
|
+
const halfEdge = halfEdges[outgoing[j]];
|
|
631
|
+
const v = halfEdge.values;
|
|
632
|
+
halfEdge.directions = getDirections(v);
|
|
633
|
+
if (!halfEdge.directions)
|
|
634
|
+
return false;
|
|
635
|
+
distance = min(distance, Math.sqrt((v[6] - v[0]) * (v[6] - v[0]) +
|
|
636
|
+
(v[7] - v[1]) * (v[7] - v[1])) / 2);
|
|
637
|
+
}
|
|
638
|
+
// All of them have their directions now:
|
|
639
|
+
const sorted = outgoing
|
|
640
|
+
.slice()
|
|
641
|
+
.sort((a, b) => (halfEdges[a].directions.start - halfEdges[b].directions.start));
|
|
642
|
+
let gap = Infinity;
|
|
643
|
+
let width = 0;
|
|
644
|
+
for (let j = 0, k = sorted.length; j < k; j++) {
|
|
645
|
+
const directions1 = halfEdges[sorted[j]].directions;
|
|
646
|
+
const directions2 = halfEdges[sorted[(j + 1) % k]].directions;
|
|
647
|
+
gap = min(gap, directions2.start -
|
|
648
|
+
directions1.start -
|
|
649
|
+
directions1.width +
|
|
650
|
+
(j + 1 < k ? 0 : 2 * Math.PI));
|
|
651
|
+
width = max(width, directions1.width);
|
|
652
|
+
}
|
|
653
|
+
if (gap > 0 && (distance * Math.cos(width / 2) * Math.sin(min(gap, Math.PI / 2)) >
|
|
654
|
+
2 * epsilon)) {
|
|
655
|
+
for (let j = 0, k = sorted.length; j < k; j++) {
|
|
656
|
+
outgoing[j] = sorted[j];
|
|
657
|
+
}
|
|
658
|
+
return true;
|
|
659
|
+
}
|
|
660
|
+
return false;
|
|
661
|
+
}
|
|
662
|
+
for (let i = 0, l = edges.length; i < l; i++) {
|
|
663
|
+
const edge = edges[i];
|
|
664
|
+
const v = edge.values;
|
|
665
|
+
edge.reversed = [v[6], v[7], v[4], v[5], v[2], v[3], v[0], v[1]];
|
|
666
|
+
// The other fields come in the steps after, see HalfEdge:
|
|
667
|
+
halfEdges.push({ edge, values: v, from: edge.from, to: edge.to }, { edge, values: edge.reversed, from: edge.to, to: edge.from });
|
|
668
|
+
vertices[edge.from].outgoing.push(2 * i);
|
|
669
|
+
vertices[edge.to].outgoing.push(2 * i + 1);
|
|
670
|
+
}
|
|
671
|
+
for (let i = 0, l = vertices.length; i < l; i++) {
|
|
672
|
+
const vertex = vertices[i];
|
|
673
|
+
const { outgoing } = vertex;
|
|
674
|
+
// Up to two half-edges are in the same cyclic order either way,
|
|
675
|
+
// and where their ranges of directions are apart far enough,
|
|
676
|
+
// these give the order already:
|
|
677
|
+
if (outgoing.length > 2 && !sortByDirections(outgoing)) {
|
|
678
|
+
let distance = Infinity;
|
|
679
|
+
for (let j = 0, k = outgoing.length; j < k; j++) {
|
|
680
|
+
const { edge } = halfEdges[outgoing[j]];
|
|
681
|
+
edge.length = edge.length || getCurveLength(edge.values);
|
|
682
|
+
distance = min(distance, edge.length / 2);
|
|
683
|
+
}
|
|
684
|
+
for (let j = 0, k = outgoing.length; j < k; j++) {
|
|
685
|
+
const halfEdge = halfEdges[outgoing[j]];
|
|
686
|
+
const point = getPointAt(halfEdge, distance);
|
|
687
|
+
halfEdge.point = point;
|
|
688
|
+
halfEdge.angle = Math.atan2(point.y - vertex.y, point.x - vertex.x);
|
|
689
|
+
}
|
|
690
|
+
// Start the order after the largest gap between the angles,
|
|
691
|
+
// so that half-edges which are hard to tell apart are not
|
|
692
|
+
// separated by the turn from pi to -pi:
|
|
693
|
+
outgoing.sort((a, b) => halfEdges[a].angle - halfEdges[b].angle);
|
|
694
|
+
let start = -Math.PI;
|
|
695
|
+
let gap = 0;
|
|
696
|
+
for (let j = 0, k = outgoing.length; j < k; j++) {
|
|
697
|
+
const { angle } = halfEdges[outgoing[j]];
|
|
698
|
+
const previous = halfEdges[outgoing[(j || k) - 1]].angle;
|
|
699
|
+
const difference = ((angle - previous + 2 * Math.PI) % (2 * Math.PI) ||
|
|
700
|
+
2 * Math.PI);
|
|
701
|
+
if (difference > gap) {
|
|
702
|
+
gap = difference;
|
|
703
|
+
start = angle;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
for (let j = 0, k = outgoing.length; j < k; j++) {
|
|
707
|
+
const halfEdge = halfEdges[outgoing[j]];
|
|
708
|
+
halfEdge.angle = (halfEdge.angle - start + 2 * Math.PI) % (2 * Math.PI);
|
|
709
|
+
}
|
|
710
|
+
outgoing.sort((a, b) => {
|
|
711
|
+
const halfEdge1 = halfEdges[a];
|
|
712
|
+
const halfEdge2 = halfEdges[b];
|
|
713
|
+
return ((halfEdge1.point.isClose(halfEdge2.point, epsilon) &&
|
|
714
|
+
getOrder(halfEdge1, halfEdge2)) ||
|
|
715
|
+
halfEdge1.angle - halfEdge2.angle);
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
for (let j = 0, k = outgoing.length; j < k; j++) {
|
|
719
|
+
halfEdges[outgoing[j]].position = j;
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
// Walking along a face with the face on the left, the next half-edge
|
|
723
|
+
// leaves the reached vertex right before the one we came back on, in
|
|
724
|
+
// counter-clockwise order:
|
|
725
|
+
for (let i = 0, l = halfEdges.length; i < l; i++) {
|
|
726
|
+
const { outgoing } = vertices[halfEdges[i].to];
|
|
727
|
+
const { position } = halfEdges[i ^ 1];
|
|
728
|
+
halfEdges[i].next = outgoing[(position || outgoing.length) - 1];
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
// Collect the faces, each on the left of its half-edges, and their
|
|
732
|
+
// signed areas, which are negative for the outside of each connected
|
|
733
|
+
// component of the graph:
|
|
734
|
+
function collectFaces(graph) {
|
|
735
|
+
const { halfEdges, faces } = graph;
|
|
736
|
+
for (let i = 0, l = halfEdges.length; i < l; i++) {
|
|
737
|
+
if (halfEdges[i].face === undefined) {
|
|
738
|
+
const face = {
|
|
739
|
+
halfEdges: [],
|
|
740
|
+
area: 0,
|
|
741
|
+
windings: null,
|
|
742
|
+
component: null
|
|
743
|
+
};
|
|
744
|
+
let index = i;
|
|
745
|
+
do {
|
|
746
|
+
const halfEdge = halfEdges[index];
|
|
747
|
+
halfEdge.face = face;
|
|
748
|
+
face.halfEdges.push(index);
|
|
749
|
+
face.area += getCurveArea(halfEdge.values);
|
|
750
|
+
index = halfEdge.next;
|
|
751
|
+
} while (index !== i);
|
|
752
|
+
faces.push(face);
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
// Propagate the windings from the outside of each connected
|
|
757
|
+
// component of the graph across its edges. A component that lies
|
|
758
|
+
// within others gets the windings of its outside from a ray cast
|
|
759
|
+
// against their edges:
|
|
760
|
+
function propagateWindings(graph) {
|
|
761
|
+
const { vertices, edges, halfEdges, faces } = graph;
|
|
762
|
+
const components = [];
|
|
763
|
+
const rays = [];
|
|
764
|
+
const componentBounds = [];
|
|
765
|
+
for (let i = 0, l = faces.length; i < l; i++) {
|
|
766
|
+
const face = faces[i];
|
|
767
|
+
if (!face.component) {
|
|
768
|
+
const component = {
|
|
769
|
+
edges: [],
|
|
770
|
+
bounds: [Infinity, Infinity, -Infinity, -Infinity],
|
|
771
|
+
outside: face
|
|
772
|
+
};
|
|
773
|
+
const list = [face];
|
|
774
|
+
face.component = component;
|
|
775
|
+
for (let j = 0; j < list.length; j++) {
|
|
776
|
+
const indices = list[j].halfEdges;
|
|
777
|
+
if (list[j].area < component.outside.area) {
|
|
778
|
+
component.outside = list[j];
|
|
779
|
+
}
|
|
780
|
+
for (let k = 0, n = indices.length; k < n; k++) {
|
|
781
|
+
const other = halfEdges[indices[k] ^ 1].face;
|
|
782
|
+
if (!other.component) {
|
|
783
|
+
other.component = component;
|
|
784
|
+
list.push(other);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
const vertex = vertices[halfEdges[component.outside.halfEdges[0]].from];
|
|
789
|
+
components.push(component);
|
|
790
|
+
rays.push([vertex.x, vertex.y, Infinity, vertex.y]);
|
|
791
|
+
componentBounds.push(component.bounds);
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
// Collect the edges and bounds of each component, to cast each ray
|
|
795
|
+
// only against the components whose bounds it reaches:
|
|
796
|
+
for (let i = 0, l = edges.length; i < l; i++) {
|
|
797
|
+
const edge = edges[i];
|
|
798
|
+
const v = edge.values;
|
|
799
|
+
// All faces belong to components now:
|
|
800
|
+
const component = halfEdges[2 * i].face.component;
|
|
801
|
+
const b = component.bounds;
|
|
802
|
+
component.edges.push(edge);
|
|
803
|
+
b[0] = min(b[0], v[0], v[2], v[4], v[6]);
|
|
804
|
+
b[1] = min(b[1], v[1], v[3], v[5], v[7]);
|
|
805
|
+
b[2] = max(b[2], v[0], v[2], v[4], v[6]);
|
|
806
|
+
b[3] = max(b[3], v[1], v[3], v[5], v[7]);
|
|
807
|
+
}
|
|
808
|
+
const rayCollisions = findBoundsCollisions(rays, componentBounds, 0, true);
|
|
809
|
+
for (let i = 0, l = components.length; i < l; i++) {
|
|
810
|
+
const { outside } = components[i];
|
|
811
|
+
const point = new Point(rays[i][0], rays[i][1]);
|
|
812
|
+
const indices = rayCollisions[i];
|
|
813
|
+
const windings = [0, 0];
|
|
814
|
+
for (let j = 0, k = indices.length; j < k; j++) {
|
|
815
|
+
if (indices[j] !== i) {
|
|
816
|
+
const list = components[indices[j]].edges;
|
|
817
|
+
for (let m = 0, n = list.length; m < n; m++) {
|
|
818
|
+
const edge = list[m];
|
|
819
|
+
// Without a tolerance, there always is a winding:
|
|
820
|
+
const winding = getCurveRayWinding(edge.values, point);
|
|
821
|
+
windings[0] += winding * edge.windings[0];
|
|
822
|
+
windings[1] += winding * edge.windings[1];
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
outside.windings = windings;
|
|
827
|
+
const queue = [outside];
|
|
828
|
+
for (let j = 0; j < queue.length; j++) {
|
|
829
|
+
const face = queue[j];
|
|
830
|
+
const indices = face.halfEdges;
|
|
831
|
+
for (let k = 0, n = indices.length; k < n; k++) {
|
|
832
|
+
const index = indices[k];
|
|
833
|
+
const other = halfEdges[index ^ 1].face;
|
|
834
|
+
if (!other.windings) {
|
|
835
|
+
// The face on the left of a half-edge has the higher
|
|
836
|
+
// winding when the half-edge runs along its edge:
|
|
837
|
+
const edgeWindings = halfEdges[index].edge.windings;
|
|
838
|
+
const sign = index & 1 ? -1 : 1;
|
|
839
|
+
// The faces in the queue have their windings:
|
|
840
|
+
const windings = face.windings;
|
|
841
|
+
other.windings = [
|
|
842
|
+
windings[0] - sign * edgeWindings[0],
|
|
843
|
+
windings[1] - sign * edgeWindings[1]
|
|
844
|
+
];
|
|
845
|
+
queue.push(other);
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
// The place where a curve is hit by another one, as the index of the curve
|
|
852
|
+
// with the time and the point on it. At the end of a curve, that is the
|
|
853
|
+
// start of the next one, where its segment lies:
|
|
854
|
+
function getHit(graph, curve, time) {
|
|
855
|
+
if (time >= 1 - CURVETIME_EPSILON && curve.next) {
|
|
856
|
+
curve = curve.next;
|
|
857
|
+
time = 0;
|
|
858
|
+
}
|
|
859
|
+
const { index } = curve;
|
|
860
|
+
return { index, time, point: getCurvePoint(graph.values[index], time) };
|
|
861
|
+
}
|
|
862
|
+
function addCut(points, index, time, point, isEnd) {
|
|
863
|
+
points.cuts[index].push({ time, point: points.xs.length });
|
|
864
|
+
points.xs.push(point.x);
|
|
865
|
+
points.ys.push(point.y);
|
|
866
|
+
points.isEnds.push(isEnd);
|
|
867
|
+
}
|
|
868
|
+
function sortCuts(list) {
|
|
869
|
+
return list.sort((a, b) => a.time - b.time);
|
|
870
|
+
}
|
|
871
|
+
function getRoot(parents, i) {
|
|
872
|
+
while (parents[i] !== i) {
|
|
873
|
+
i = parents[i] = parents[parents[i]];
|
|
874
|
+
}
|
|
875
|
+
return i;
|
|
876
|
+
}
|
|
877
|
+
function getVertex(points, point) {
|
|
878
|
+
return points.vertexIndices[getRoot(points.parents, point)];
|
|
879
|
+
}
|
|
880
|
+
// Create the vertices of the points as they are merged so far, each at the
|
|
881
|
+
// position of a curve end if it has one:
|
|
882
|
+
function createVertices(graph, points) {
|
|
883
|
+
const { xs, ys, isEnds, parents } = points;
|
|
884
|
+
const vertexIndices = {};
|
|
885
|
+
const vertices = [];
|
|
886
|
+
for (let i = 0, l = xs.length; i < l; i++) {
|
|
887
|
+
const root = getRoot(parents, i);
|
|
888
|
+
const index = vertexIndices[root];
|
|
889
|
+
if (index === undefined || isEnds[i] && !vertices[index].isEnd) {
|
|
890
|
+
vertices[(vertexIndices[root] ??= vertices.length)] = {
|
|
891
|
+
x: xs[i],
|
|
892
|
+
y: ys[i],
|
|
893
|
+
isEnd: isEnds[i],
|
|
894
|
+
point: i,
|
|
895
|
+
outgoing: []
|
|
896
|
+
};
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
points.vertexIndices = vertexIndices;
|
|
900
|
+
graph.vertices = vertices;
|
|
901
|
+
}
|
|
902
|
+
// The piece of curve i between two cuts, with its ends moved to their
|
|
903
|
+
// vertices, along with their handles:
|
|
904
|
+
function getPiece(graph, points, i, cut1, cut2) {
|
|
905
|
+
return getPart(graph.values[i], graph.curves[i].hasHandles, cut1.time, cut2.time, graph.vertices[getVertex(points, cut1.point)], graph.vertices[getVertex(points, cut2.point)]);
|
|
906
|
+
}
|
|
907
|
+
/*
|
|
908
|
+
* Returns the part of the curve values v between the curve times, with its
|
|
909
|
+
* ends moved to the given points, along with their handles. Straight
|
|
910
|
+
* curves remain straight.
|
|
911
|
+
*/
|
|
912
|
+
function getPart(v, hasHandles, time1, time2, point1, point2) {
|
|
913
|
+
let part;
|
|
914
|
+
if (hasHandles) {
|
|
915
|
+
part = [...getCurvePart(v, time1, time2)];
|
|
916
|
+
const dx1 = point1.x - part[0];
|
|
917
|
+
const dy1 = point1.y - part[1];
|
|
918
|
+
const dx2 = point2.x - part[6];
|
|
919
|
+
const dy2 = point2.y - part[7];
|
|
920
|
+
part[0] += dx1;
|
|
921
|
+
part[1] += dy1;
|
|
922
|
+
part[2] += dx1;
|
|
923
|
+
part[3] += dy1;
|
|
924
|
+
part[4] += dx2;
|
|
925
|
+
part[5] += dy2;
|
|
926
|
+
part[6] += dx2;
|
|
927
|
+
part[7] += dy2;
|
|
928
|
+
}
|
|
929
|
+
else {
|
|
930
|
+
part = [
|
|
931
|
+
point1.x,
|
|
932
|
+
point1.y,
|
|
933
|
+
point1.x,
|
|
934
|
+
point1.y,
|
|
935
|
+
point2.x,
|
|
936
|
+
point2.y,
|
|
937
|
+
point2.x,
|
|
938
|
+
point2.y
|
|
939
|
+
];
|
|
940
|
+
}
|
|
941
|
+
return part;
|
|
942
|
+
}
|
|
943
|
+
// The side of the curve that the point lies on, as the sign of its distance,
|
|
944
|
+
// or 0 beyond the curve's ends:
|
|
945
|
+
function getSide(v, point, epsilon) {
|
|
946
|
+
const t = getNearestCurveTime(v, point, epsilon);
|
|
947
|
+
return t > 0 && t < 1
|
|
948
|
+
? Math.sign(getCurveTangent(v, t).cross(point.subtract(getCurvePoint(v, t))))
|
|
949
|
+
: 0;
|
|
950
|
+
}
|