@canvas-commons/2d 0.2.0
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/LICENSE +22 -0
- package/editor/index.css +40 -0
- package/editor/index.js +525 -0
- package/editor/index.js.map +1 -0
- package/lib/index-OvpZ-GsA.d.ts +5673 -0
- package/lib/index-OvpZ-GsA.d.ts.map +1 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +10326 -0
- package/lib/index.js.map +1 -0
- package/lib/jsx-dev-runtime.d.ts +2 -0
- package/lib/jsx-dev-runtime.js +2 -0
- package/lib/jsx-runtime.d.ts +2 -0
- package/lib/jsx-runtime.js +28 -0
- package/lib/jsx-runtime.js.map +1 -0
- package/package.json +79 -0
- package/src/editor/NodeInspectorConfig.tsx +76 -0
- package/src/editor/PreviewOverlayConfig.tsx +65 -0
- package/src/editor/Provider.tsx +109 -0
- package/src/editor/SceneGraphTabConfig.tsx +87 -0
- package/src/editor/icons/CircleIcon.tsx +7 -0
- package/src/editor/icons/CodeIcon.tsx +8 -0
- package/src/editor/icons/CurveIcon.tsx +7 -0
- package/src/editor/icons/GridIcon.tsx +7 -0
- package/src/editor/icons/IconMap.ts +35 -0
- package/src/editor/icons/ImgIcon.tsx +8 -0
- package/src/editor/icons/LayoutIcon.tsx +9 -0
- package/src/editor/icons/LineIcon.tsx +7 -0
- package/src/editor/icons/NodeIcon.tsx +7 -0
- package/src/editor/icons/RayIcon.tsx +7 -0
- package/src/editor/icons/RectIcon.tsx +7 -0
- package/src/editor/icons/ShapeIcon.tsx +7 -0
- package/src/editor/icons/TxtIcon.tsx +8 -0
- package/src/editor/icons/VideoIcon.tsx +7 -0
- package/src/editor/icons/View2DIcon.tsx +10 -0
- package/src/editor/index.css +0 -0
- package/src/editor/index.ts +19 -0
- package/src/editor/shortcuts.ts +27 -0
- package/src/editor/tree/DetachedRoot.tsx +27 -0
- package/src/editor/tree/NodeElement.tsx +72 -0
- package/src/editor/tree/TreeElement.tsx +70 -0
- package/src/editor/tree/TreeRoot.tsx +10 -0
- package/src/editor/tree/ViewRoot.tsx +20 -0
- package/src/editor/tree/index.module.scss +45 -0
- package/src/editor/tree/index.ts +4 -0
- package/src/editor/tree/navigation.ts +145 -0
- package/src/editor/tsconfig.build.json +5 -0
- package/src/editor/tsconfig.json +12 -0
- package/src/editor/tsdoc.json +4 -0
- package/src/editor/utils/SignalSet.ts +37 -0
- package/src/editor/utils/index.ts +1 -0
- package/src/editor/vite-env.d.ts +1 -0
- package/src/lib/code/CodeCursor.ts +468 -0
- package/src/lib/code/CodeDiffer.ts +77 -0
- package/src/lib/code/CodeFragment.ts +96 -0
- package/src/lib/code/CodeHighlighter.ts +73 -0
- package/src/lib/code/CodeMetrics.ts +47 -0
- package/src/lib/code/CodeRange.test.ts +113 -0
- package/src/lib/code/CodeRange.ts +222 -0
- package/src/lib/code/CodeScope.ts +100 -0
- package/src/lib/code/CodeSelection.ts +28 -0
- package/src/lib/code/CodeSignal.ts +348 -0
- package/src/lib/code/CodeTokenizer.ts +54 -0
- package/src/lib/code/DefaultHighlightStyle.ts +98 -0
- package/src/lib/code/LezerHighlighter.ts +113 -0
- package/src/lib/code/diff.test.ts +311 -0
- package/src/lib/code/diff.ts +319 -0
- package/src/lib/code/extractRange.ts +125 -0
- package/src/lib/code/index.ts +13 -0
- package/src/lib/components/Bezier.ts +103 -0
- package/src/lib/components/Camera.ts +401 -0
- package/src/lib/components/Circle.ts +310 -0
- package/src/lib/components/Code.ts +532 -0
- package/src/lib/components/CubicBezier.ts +115 -0
- package/src/lib/components/Curve.ts +460 -0
- package/src/lib/components/Grid.ts +134 -0
- package/src/lib/components/Icon.ts +153 -0
- package/src/lib/components/Img.ts +328 -0
- package/src/lib/components/Knot.ts +156 -0
- package/src/lib/components/Latex.ts +537 -0
- package/src/lib/components/Layout.ts +1101 -0
- package/src/lib/components/Line.ts +394 -0
- package/src/lib/components/Node.ts +1941 -0
- package/src/lib/components/Path.ts +132 -0
- package/src/lib/components/Polygon.ts +266 -0
- package/src/lib/components/QuadBezier.ts +103 -0
- package/src/lib/components/Ray.ts +126 -0
- package/src/lib/components/Rect.ts +219 -0
- package/src/lib/components/SVG.ts +853 -0
- package/src/lib/components/Shape.ts +322 -0
- package/src/lib/components/Spline.ts +318 -0
- package/src/lib/components/Txt.test.tsx +81 -0
- package/src/lib/components/Txt.ts +204 -0
- package/src/lib/components/TxtLeaf.ts +210 -0
- package/src/lib/components/Video.ts +368 -0
- package/src/lib/components/View2D.ts +85 -0
- package/src/lib/components/__logs__/image-without-source.ts +18 -0
- package/src/lib/components/__logs__/line-without-points.ts +31 -0
- package/src/lib/components/__logs__/reactive-playback-rate.ts +22 -0
- package/src/lib/components/__logs__/spline-with-insufficient-knots.ts +25 -0
- package/src/lib/components/__tests__/Camera.test.tsx +73 -0
- package/src/lib/components/__tests__/children.test.tsx +142 -0
- package/src/lib/components/__tests__/clone.test.tsx +126 -0
- package/src/lib/components/__tests__/fontInheritance.test.tsx +102 -0
- package/src/lib/components/__tests__/generatorTest.ts +27 -0
- package/src/lib/components/__tests__/mockScene2D.ts +50 -0
- package/src/lib/components/__tests__/query.test.tsx +122 -0
- package/src/lib/components/__tests__/state.test.tsx +60 -0
- package/src/lib/components/index.ts +26 -0
- package/src/lib/components/types.ts +35 -0
- package/src/lib/curves/ArcSegment.ts +169 -0
- package/src/lib/curves/CircleSegment.ts +99 -0
- package/src/lib/curves/CubicBezierSegment.ts +80 -0
- package/src/lib/curves/CurveDrawingInfo.ts +11 -0
- package/src/lib/curves/CurvePoint.ts +15 -0
- package/src/lib/curves/CurveProfile.ts +28 -0
- package/src/lib/curves/KnotInfo.ts +10 -0
- package/src/lib/curves/LineSegment.ts +75 -0
- package/src/lib/curves/Polynomial.ts +355 -0
- package/src/lib/curves/Polynomial2D.ts +62 -0
- package/src/lib/curves/PolynomialSegment.ts +151 -0
- package/src/lib/curves/QuadBezierSegment.ts +66 -0
- package/src/lib/curves/Segment.ts +31 -0
- package/src/lib/curves/UniformPolynomialCurveSampler.ts +93 -0
- package/src/lib/curves/createCurveProfileLerp.ts +471 -0
- package/src/lib/curves/getBezierSplineProfile.ts +227 -0
- package/src/lib/curves/getCircleProfile.ts +86 -0
- package/src/lib/curves/getPathProfile.ts +177 -0
- package/src/lib/curves/getPointAtDistance.ts +21 -0
- package/src/lib/curves/getPolylineProfile.test.ts +21 -0
- package/src/lib/curves/getPolylineProfile.ts +88 -0
- package/src/lib/curves/getRectProfile.ts +138 -0
- package/src/lib/curves/index.ts +16 -0
- package/src/lib/decorators/canvasStyleSignal.ts +15 -0
- package/src/lib/decorators/colorSignal.ts +9 -0
- package/src/lib/decorators/compound.ts +85 -0
- package/src/lib/decorators/computed.ts +18 -0
- package/src/lib/decorators/defaultStyle.ts +15 -0
- package/src/lib/decorators/filtersSignal.ts +133 -0
- package/src/lib/decorators/index.ts +10 -0
- package/src/lib/decorators/initializers.ts +32 -0
- package/src/lib/decorators/nodeName.ts +13 -0
- package/src/lib/decorators/signal.test.ts +89 -0
- package/src/lib/decorators/signal.ts +348 -0
- package/src/lib/decorators/spacingSignal.ts +15 -0
- package/src/lib/decorators/transformSignals.test.ts +858 -0
- package/src/lib/decorators/transformSignals.ts +1633 -0
- package/src/lib/decorators/vector2Signal.ts +35 -0
- package/src/lib/globals.d.ts +2 -0
- package/src/lib/index.ts +9 -0
- package/src/lib/jsx-dev-runtime.ts +2 -0
- package/src/lib/jsx-runtime.ts +45 -0
- package/src/lib/morphers/PathMorpher.ts +8 -0
- package/src/lib/morphers/defaultMorpher.ts +43 -0
- package/src/lib/morphers/index.ts +4 -0
- package/src/lib/morphers/manimMorpher.ts +658 -0
- package/src/lib/parse-svg-path.d.ts +14 -0
- package/src/lib/partials/Filter.ts +185 -0
- package/src/lib/partials/Gradient.ts +103 -0
- package/src/lib/partials/Pattern.ts +35 -0
- package/src/lib/partials/RoughConfig.ts +180 -0
- package/src/lib/partials/ShaderConfig.ts +122 -0
- package/src/lib/partials/index.ts +5 -0
- package/src/lib/partials/types.ts +58 -0
- package/src/lib/scenes/Scene2D.ts +167 -0
- package/src/lib/scenes/index.ts +3 -0
- package/src/lib/scenes/makeScene2D.ts +19 -0
- package/src/lib/scenes/useScene2D.ts +6 -0
- package/src/lib/tsconfig.build.json +5 -0
- package/src/lib/tsconfig.json +11 -0
- package/src/lib/tsdoc.json +4 -0
- package/src/lib/utils/CanvasUtils.ts +304 -0
- package/src/lib/utils/PathDataBuilder.ts +320 -0
- package/src/lib/utils/diff.test.ts +453 -0
- package/src/lib/utils/diff.ts +148 -0
- package/src/lib/utils/index.ts +5 -0
- package/src/lib/utils/is.ts +11 -0
- package/src/lib/utils/makeSignalExtensions.ts +29 -0
- package/src/lib/utils/rough.ts +513 -0
- package/src/lib/utils/withDefaults.tsx +26 -0
- package/src/tsconfig.base.json +18 -0
- package/src/tsconfig.build.json +8 -0
- package/src/tsconfig.json +5 -0
- package/tsconfig.project.json +7 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ColorSignal,
|
|
3
|
+
DependencyContext,
|
|
4
|
+
PossibleColor,
|
|
5
|
+
SignalValue,
|
|
6
|
+
SimpleSignal,
|
|
7
|
+
TimingFunction,
|
|
8
|
+
isReactive,
|
|
9
|
+
threadable,
|
|
10
|
+
useLogger,
|
|
11
|
+
} from '@canvas-commons/core';
|
|
12
|
+
import {colorSignal, computed, initial, signal} from '../decorators';
|
|
13
|
+
import {SVG, SVGProps} from './SVG';
|
|
14
|
+
|
|
15
|
+
export interface IconProps extends Omit<SVGProps, 'svg'> {
|
|
16
|
+
/**
|
|
17
|
+
* {@inheritDoc Icon.icon}
|
|
18
|
+
*/
|
|
19
|
+
icon: SignalValue<string>;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* {@inheritDoc Icon.color}
|
|
23
|
+
*/
|
|
24
|
+
color?: SignalValue<PossibleColor>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const PLACEHOLDER_SVG =
|
|
28
|
+
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><rect width="24" height="24" fill="none"/></svg>';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* An Icon Component that provides easy access to over 150k icons.
|
|
32
|
+
* See https://icones.js.org/collection/all for all available Icons.
|
|
33
|
+
*/
|
|
34
|
+
export class Icon extends SVG {
|
|
35
|
+
private static iconSvgCache: Map<string, string> = new Map();
|
|
36
|
+
private static pendingFetches: Map<string, Promise<string>> = new Map();
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The identifier of the icon.
|
|
40
|
+
*
|
|
41
|
+
* @remarks
|
|
42
|
+
* You can find identifiers on [Icônes](https://icones.js.org).
|
|
43
|
+
* They can look like this:
|
|
44
|
+
* * `mdi:language-typescript`
|
|
45
|
+
* * `ph:anchor-simple-bold`
|
|
46
|
+
* * `ph:activity-bold`
|
|
47
|
+
*/
|
|
48
|
+
@signal()
|
|
49
|
+
declare public readonly icon: SimpleSignal<string, this>;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The color of the icon
|
|
53
|
+
*
|
|
54
|
+
* @remarks
|
|
55
|
+
* Provide the color in one of the following formats:
|
|
56
|
+
* * named color like `red`, `darkgray`, …
|
|
57
|
+
* * hexadecimal string with # like `#bada55`, `#141414`
|
|
58
|
+
* Value can be either RGB or RGBA: `#bada55`, `#bada55aa` (latter is partially transparent)
|
|
59
|
+
* The shorthand version (e.g. `#abc` for `#aabbcc` is also possible.)
|
|
60
|
+
*
|
|
61
|
+
* @defaultValue 'white'
|
|
62
|
+
*/
|
|
63
|
+
@initial('white')
|
|
64
|
+
@colorSignal()
|
|
65
|
+
declare public readonly color: ColorSignal<this>;
|
|
66
|
+
|
|
67
|
+
public constructor(props: IconProps) {
|
|
68
|
+
super({
|
|
69
|
+
...props,
|
|
70
|
+
svg: () => this.iconSvg(),
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
protected override collectAsyncResources(): void {
|
|
75
|
+
super.collectAsyncResources();
|
|
76
|
+
this.iconSvg();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
@computed()
|
|
80
|
+
protected iconSvg(): string {
|
|
81
|
+
const iconId = this.icon();
|
|
82
|
+
const color = this.color().hex();
|
|
83
|
+
const cacheKey = `${iconId}::${color}`;
|
|
84
|
+
|
|
85
|
+
const cached = Icon.iconSvgCache.get(cacheKey);
|
|
86
|
+
if (cached !== undefined) {
|
|
87
|
+
return cached;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (iconId === null || iconId === undefined) {
|
|
91
|
+
return PLACEHOLDER_SVG;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const fetchPromise = this.fetchIconSvg(iconId, color);
|
|
95
|
+
DependencyContext.collectPromise(fetchPromise);
|
|
96
|
+
|
|
97
|
+
return PLACEHOLDER_SVG;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private async fetchIconSvg(iconId: string, color: string): Promise<string> {
|
|
101
|
+
const cacheKey = `${iconId}::${color}`;
|
|
102
|
+
|
|
103
|
+
const cached = Icon.iconSvgCache.get(cacheKey);
|
|
104
|
+
if (cached !== undefined) {
|
|
105
|
+
return cached;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const pending = Icon.pendingFetches.get(cacheKey);
|
|
109
|
+
if (pending !== undefined) {
|
|
110
|
+
return pending;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const iconPath = iconId.replace(':', '/');
|
|
114
|
+
const encodedColor = encodeURIComponent(color);
|
|
115
|
+
const url = `https://api.iconify.design/${iconPath}.svg?color=${encodedColor}`;
|
|
116
|
+
|
|
117
|
+
const fetchPromise = fetch(url)
|
|
118
|
+
.then(response => {
|
|
119
|
+
if (!response.ok) {
|
|
120
|
+
throw new Error(`Failed to fetch icon: ${iconId}`);
|
|
121
|
+
}
|
|
122
|
+
return response.text();
|
|
123
|
+
})
|
|
124
|
+
.then(svg => {
|
|
125
|
+
Icon.iconSvgCache.set(cacheKey, svg);
|
|
126
|
+
Icon.pendingFetches.delete(cacheKey);
|
|
127
|
+
return svg;
|
|
128
|
+
})
|
|
129
|
+
.catch(error => {
|
|
130
|
+
Icon.pendingFetches.delete(cacheKey);
|
|
131
|
+
useLogger().error(`Error fetching icon ${iconId}: ${error}`);
|
|
132
|
+
return PLACEHOLDER_SVG;
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
Icon.pendingFetches.set(cacheKey, fetchPromise);
|
|
136
|
+
return fetchPromise;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
@threadable()
|
|
140
|
+
protected *tweenIcon(
|
|
141
|
+
value: SignalValue<string>,
|
|
142
|
+
time: number,
|
|
143
|
+
timingFunction: TimingFunction,
|
|
144
|
+
) {
|
|
145
|
+
const newIconId = isReactive(value) ? value() : value;
|
|
146
|
+
const color = this.color().hex();
|
|
147
|
+
|
|
148
|
+
const newSvg: string = yield this.fetchIconSvg(newIconId, color);
|
|
149
|
+
|
|
150
|
+
yield* this.svg(newSvg, time, timingFunction);
|
|
151
|
+
this.icon.context.setter(newIconId);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BBox,
|
|
3
|
+
Color,
|
|
4
|
+
DependencyContext,
|
|
5
|
+
DetailedError,
|
|
6
|
+
PossibleVector2,
|
|
7
|
+
SerializedVector2,
|
|
8
|
+
SignalValue,
|
|
9
|
+
SimpleSignal,
|
|
10
|
+
TimingFunction,
|
|
11
|
+
Vector2,
|
|
12
|
+
easeInOutCubic,
|
|
13
|
+
isReactive,
|
|
14
|
+
threadable,
|
|
15
|
+
tween,
|
|
16
|
+
useLogger,
|
|
17
|
+
viaProxy,
|
|
18
|
+
} from '@canvas-commons/core';
|
|
19
|
+
import {computed, initial, nodeName, signal} from '../decorators';
|
|
20
|
+
import {DesiredLength} from '../partials';
|
|
21
|
+
import {drawImage} from '../utils';
|
|
22
|
+
import {Rect, RectProps} from './Rect';
|
|
23
|
+
import imageWithoutSource from './__logs__/image-without-source';
|
|
24
|
+
|
|
25
|
+
export interface ImgProps extends RectProps {
|
|
26
|
+
/**
|
|
27
|
+
* {@inheritDoc Img.src}
|
|
28
|
+
*/
|
|
29
|
+
src?: SignalValue<string | null>;
|
|
30
|
+
/**
|
|
31
|
+
* {@inheritDoc Img.alpha}
|
|
32
|
+
*/
|
|
33
|
+
alpha?: SignalValue<number>;
|
|
34
|
+
/**
|
|
35
|
+
* {@inheritDoc Img.smoothing}
|
|
36
|
+
*/
|
|
37
|
+
smoothing?: SignalValue<boolean>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A node for displaying images.
|
|
42
|
+
*
|
|
43
|
+
* @preview
|
|
44
|
+
* ```tsx editor
|
|
45
|
+
* import {Img} from '@canvas-commons/2d';
|
|
46
|
+
* import {all, waitFor} from '@canvas-commons/core';
|
|
47
|
+
* import {createRef} from '@canvas-commons/core';
|
|
48
|
+
* import {makeScene2D} from '@canvas-commons/2d';
|
|
49
|
+
*
|
|
50
|
+
* export default makeScene2D(function* (view) {
|
|
51
|
+
* const ref = createRef<Img>();
|
|
52
|
+
* yield view.add(
|
|
53
|
+
* <Img
|
|
54
|
+
* ref={ref}
|
|
55
|
+
* src="https://images.unsplash.com/photo-1679218407381-a6f1660d60e9"
|
|
56
|
+
* width={300}
|
|
57
|
+
* radius={20}
|
|
58
|
+
* />,
|
|
59
|
+
* );
|
|
60
|
+
*
|
|
61
|
+
* // set the background using the color sampled from the image:
|
|
62
|
+
* ref().fill(ref().getColorAtPoint(0));
|
|
63
|
+
*
|
|
64
|
+
* yield* all(
|
|
65
|
+
* ref().size([100, 100], 1).to([300, null], 1),
|
|
66
|
+
* ref().radius(50, 1).to(20, 1),
|
|
67
|
+
* ref().alpha(0, 1).to(1, 1),
|
|
68
|
+
* );
|
|
69
|
+
* yield* waitFor(0.5);
|
|
70
|
+
* });
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
@nodeName('Img')
|
|
74
|
+
export class Img extends Rect {
|
|
75
|
+
private static pool: Record<string, HTMLImageElement> = {};
|
|
76
|
+
|
|
77
|
+
static {
|
|
78
|
+
if (import.meta.hot) {
|
|
79
|
+
import.meta.hot.on('canvas-commons:assets', ({urls}) => {
|
|
80
|
+
for (const url of urls) {
|
|
81
|
+
if (Img.pool[url]) {
|
|
82
|
+
delete Img.pool[url];
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The source of this image.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* Using a local image:
|
|
94
|
+
* ```tsx
|
|
95
|
+
* import image from './example.png';
|
|
96
|
+
* // ...
|
|
97
|
+
* view.add(<Img src={image} />)
|
|
98
|
+
* ```
|
|
99
|
+
* Loading an image from the internet:
|
|
100
|
+
* ```tsx
|
|
101
|
+
* view.add(<Img src="https://example.com/image.png" />)
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
@signal()
|
|
105
|
+
declare public readonly src: SimpleSignal<string, this>;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The alpha value of this image.
|
|
109
|
+
*
|
|
110
|
+
* @remarks
|
|
111
|
+
* Unlike opacity, the alpha value affects only the image itself, leaving the
|
|
112
|
+
* fill, stroke, and children intact.
|
|
113
|
+
*/
|
|
114
|
+
@initial(1)
|
|
115
|
+
@signal()
|
|
116
|
+
declare public readonly alpha: SimpleSignal<number, this>;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Whether the image should be smoothed.
|
|
120
|
+
*
|
|
121
|
+
* @remarks
|
|
122
|
+
* When disabled, the image will be scaled using the nearest neighbor
|
|
123
|
+
* interpolation with no smoothing. The resulting image will appear pixelated.
|
|
124
|
+
*
|
|
125
|
+
* @defaultValue true
|
|
126
|
+
*/
|
|
127
|
+
@initial(true)
|
|
128
|
+
@signal()
|
|
129
|
+
declare public readonly smoothing: SimpleSignal<boolean, this>;
|
|
130
|
+
|
|
131
|
+
public constructor(props: ImgProps) {
|
|
132
|
+
super(props);
|
|
133
|
+
if (!('src' in props)) {
|
|
134
|
+
useLogger().warn({
|
|
135
|
+
message: 'No source specified for the image',
|
|
136
|
+
remarks: imageWithoutSource,
|
|
137
|
+
inspect: this.key,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
protected override desiredSize(): SerializedVector2<DesiredLength> {
|
|
143
|
+
const custom = super.desiredSize();
|
|
144
|
+
if (custom.x === null && custom.y === null) {
|
|
145
|
+
const image = this.image();
|
|
146
|
+
return {
|
|
147
|
+
x: image.naturalWidth,
|
|
148
|
+
y: image.naturalHeight,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return custom;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
@computed()
|
|
156
|
+
protected image(): HTMLImageElement {
|
|
157
|
+
const rawSrc = this.src();
|
|
158
|
+
let src = '';
|
|
159
|
+
let key = '';
|
|
160
|
+
if (rawSrc) {
|
|
161
|
+
key = viaProxy(rawSrc);
|
|
162
|
+
const url = new URL(key, window.location.origin);
|
|
163
|
+
if (url.origin === window.location.origin) {
|
|
164
|
+
const hash = this.view().assetHash();
|
|
165
|
+
url.searchParams.set('asset-hash', hash);
|
|
166
|
+
}
|
|
167
|
+
src = url.toString();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
let image = Img.pool[key];
|
|
171
|
+
if (!image) {
|
|
172
|
+
image = document.createElement('img');
|
|
173
|
+
image.crossOrigin = 'anonymous';
|
|
174
|
+
image.src = src;
|
|
175
|
+
Img.pool[key] = image;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (!image.complete) {
|
|
179
|
+
DependencyContext.collectPromise(
|
|
180
|
+
new Promise((resolve, reject) => {
|
|
181
|
+
image.addEventListener('load', resolve);
|
|
182
|
+
image.addEventListener('error', () =>
|
|
183
|
+
reject(
|
|
184
|
+
new DetailedError({
|
|
185
|
+
message: `Failed to load an image`,
|
|
186
|
+
remarks: `\
|
|
187
|
+
The <code>src</code> property was set to:
|
|
188
|
+
<pre><code>${rawSrc}</code></pre>
|
|
189
|
+
...which resolved to the following url:
|
|
190
|
+
<pre><code>${src}</code></pre>
|
|
191
|
+
Make sure that source is correct and that the image exists.<br/>
|
|
192
|
+
<a target='_blank' href='https://canvascommons.io/docs/media#images'>Learn more</a>
|
|
193
|
+
about working with images.`,
|
|
194
|
+
inspect: this.key,
|
|
195
|
+
}),
|
|
196
|
+
),
|
|
197
|
+
);
|
|
198
|
+
}),
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return image;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
@computed()
|
|
206
|
+
protected imageCanvas(): CanvasRenderingContext2D {
|
|
207
|
+
const canvas = document
|
|
208
|
+
.createElement('canvas')
|
|
209
|
+
.getContext('2d', {willReadFrequently: true});
|
|
210
|
+
if (!canvas) {
|
|
211
|
+
throw new Error('Could not create an image canvas');
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return canvas;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
@computed()
|
|
218
|
+
protected filledImageCanvas() {
|
|
219
|
+
const context = this.imageCanvas();
|
|
220
|
+
const image = this.image();
|
|
221
|
+
context.canvas.width = image.naturalWidth;
|
|
222
|
+
context.canvas.height = image.naturalHeight;
|
|
223
|
+
context.imageSmoothingEnabled = this.smoothing();
|
|
224
|
+
context.drawImage(image, 0, 0);
|
|
225
|
+
|
|
226
|
+
return context;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
protected override draw(context: CanvasRenderingContext2D) {
|
|
230
|
+
this.drawShape(context);
|
|
231
|
+
const alpha = this.alpha();
|
|
232
|
+
if (alpha > 0) {
|
|
233
|
+
const box = BBox.fromSizeCentered(this.computedSize());
|
|
234
|
+
context.save();
|
|
235
|
+
context.clip(this.getPath());
|
|
236
|
+
if (alpha < 1) {
|
|
237
|
+
context.globalAlpha *= alpha;
|
|
238
|
+
}
|
|
239
|
+
context.imageSmoothingEnabled = this.smoothing();
|
|
240
|
+
drawImage(context, this.image(), box);
|
|
241
|
+
context.restore();
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (this.clip()) {
|
|
245
|
+
context.clip(this.getPath());
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
this.drawChildren(context);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
protected override applyFlex() {
|
|
252
|
+
super.applyFlex();
|
|
253
|
+
const image = this.image();
|
|
254
|
+
this.element.style.aspectRatio = (
|
|
255
|
+
this.ratio() ?? image.naturalWidth / image.naturalHeight
|
|
256
|
+
).toString();
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Get color of the image at the given position.
|
|
261
|
+
*
|
|
262
|
+
* @param position - The position in local space at which to sample the color.
|
|
263
|
+
*/
|
|
264
|
+
public getColorAtPoint(position: PossibleVector2): Color {
|
|
265
|
+
const size = this.computedSize();
|
|
266
|
+
const naturalSize = this.naturalSize();
|
|
267
|
+
|
|
268
|
+
const pixelPosition = new Vector2(position)
|
|
269
|
+
.add(this.computedSize().scale(0.5))
|
|
270
|
+
.mul(naturalSize.div(size).safe);
|
|
271
|
+
|
|
272
|
+
return this.getPixelColor(pixelPosition);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* The natural size of this image.
|
|
277
|
+
*
|
|
278
|
+
* @remarks
|
|
279
|
+
* The natural size is the size of the source image unaffected by the size
|
|
280
|
+
* and scale properties.
|
|
281
|
+
*/
|
|
282
|
+
@computed()
|
|
283
|
+
public naturalSize() {
|
|
284
|
+
const image = this.image();
|
|
285
|
+
return new Vector2(image.naturalWidth, image.naturalHeight);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Get color of the image at the given pixel.
|
|
290
|
+
*
|
|
291
|
+
* @param position - The pixel's position.
|
|
292
|
+
*/
|
|
293
|
+
public getPixelColor(position: PossibleVector2): Color {
|
|
294
|
+
const context = this.filledImageCanvas();
|
|
295
|
+
const vector = new Vector2(position);
|
|
296
|
+
const data = context.getImageData(vector.x, vector.y, 1, 1).data;
|
|
297
|
+
|
|
298
|
+
return new Color({
|
|
299
|
+
r: data[0],
|
|
300
|
+
g: data[1],
|
|
301
|
+
b: data[2],
|
|
302
|
+
a: data[3] / 255,
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
@threadable()
|
|
307
|
+
protected *tweenSrc(
|
|
308
|
+
value: SignalValue<string>,
|
|
309
|
+
time: number,
|
|
310
|
+
timingFunction: TimingFunction = easeInOutCubic,
|
|
311
|
+
) {
|
|
312
|
+
const newSrc = isReactive(value) ? value() : value;
|
|
313
|
+
const currentOpacity = this.opacity();
|
|
314
|
+
const halfTime = time / 2;
|
|
315
|
+
yield* tween(halfTime, v => {
|
|
316
|
+
this.opacity(currentOpacity * (1 - timingFunction(v)));
|
|
317
|
+
});
|
|
318
|
+
this.src.context.setter(newSrc);
|
|
319
|
+
yield* tween(halfTime, v => {
|
|
320
|
+
this.opacity(currentOpacity * timingFunction(v));
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
protected override collectAsyncResources() {
|
|
325
|
+
super.collectAsyncResources();
|
|
326
|
+
this.image();
|
|
327
|
+
}
|
|
328
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PossibleVector2,
|
|
3
|
+
Signal,
|
|
4
|
+
SignalValue,
|
|
5
|
+
Vector2,
|
|
6
|
+
Vector2Signal,
|
|
7
|
+
} from '@canvas-commons/core';
|
|
8
|
+
import {KnotInfo} from '../curves';
|
|
9
|
+
import {
|
|
10
|
+
cloneable,
|
|
11
|
+
compound,
|
|
12
|
+
computed,
|
|
13
|
+
initial,
|
|
14
|
+
parser,
|
|
15
|
+
signal,
|
|
16
|
+
wrapper,
|
|
17
|
+
} from '../decorators';
|
|
18
|
+
import {Node, NodeProps} from './Node';
|
|
19
|
+
|
|
20
|
+
export interface KnotProps extends NodeProps {
|
|
21
|
+
/**
|
|
22
|
+
* {@inheritDoc Knot.startHandle}
|
|
23
|
+
*/
|
|
24
|
+
startHandle?: SignalValue<PossibleVector2>;
|
|
25
|
+
/**
|
|
26
|
+
* {@inheritDoc Knot.endHandle}
|
|
27
|
+
*/
|
|
28
|
+
endHandle?: SignalValue<PossibleVector2>;
|
|
29
|
+
/**
|
|
30
|
+
* {@inheritDoc Knot.auto}
|
|
31
|
+
*/
|
|
32
|
+
auto?: SignalValue<PossibleKnotAuto>;
|
|
33
|
+
startHandleAuto?: SignalValue<number>;
|
|
34
|
+
endHandleAuto?: SignalValue<number>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type KnotAuto = {startHandle: number; endHandle: number};
|
|
38
|
+
export type PossibleKnotAuto = KnotAuto | number | [number, number];
|
|
39
|
+
export type KnotAutoSignal<TOwner> = Signal<
|
|
40
|
+
PossibleKnotAuto,
|
|
41
|
+
KnotAuto,
|
|
42
|
+
TOwner
|
|
43
|
+
> & {
|
|
44
|
+
endHandle: Signal<number, number, TOwner>;
|
|
45
|
+
startHandle: Signal<number, number, TOwner>;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A node representing a knot of a {@link Spline}.
|
|
50
|
+
*/
|
|
51
|
+
export class Knot extends Node {
|
|
52
|
+
/**
|
|
53
|
+
* The position of the knot's start handle. The position is provided relative
|
|
54
|
+
* to the knot's position.
|
|
55
|
+
*
|
|
56
|
+
* @remarks
|
|
57
|
+
* By default, the position of the start handle will be the mirrored position
|
|
58
|
+
* of the {@link endHandle}.
|
|
59
|
+
*
|
|
60
|
+
* If neither an end handle nor a start handle is provided, the positions of
|
|
61
|
+
* the handles gets calculated automatically to create smooth curve through
|
|
62
|
+
* the knot. The smoothness of the resulting curve can be controlled via the
|
|
63
|
+
* {@link Spline.smoothness} property.
|
|
64
|
+
*
|
|
65
|
+
* It is also possible to blend between a user-defined position and the
|
|
66
|
+
* auto-calculated position by using the {@link auto} property.
|
|
67
|
+
*
|
|
68
|
+
* @defaultValue Mirrored position of the endHandle.
|
|
69
|
+
*/
|
|
70
|
+
@wrapper(Vector2)
|
|
71
|
+
@signal()
|
|
72
|
+
declare public readonly startHandle: Vector2Signal<this>;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The position of the knot's end handle. The position is provided relative
|
|
76
|
+
* to the knot's position.
|
|
77
|
+
*
|
|
78
|
+
* @remarks
|
|
79
|
+
* By default, the position of the end handle will be the mirrored position
|
|
80
|
+
* of the {@link startHandle}.
|
|
81
|
+
*
|
|
82
|
+
* If neither an end handle nor a start handle is provided, the positions of
|
|
83
|
+
* the handles gets calculated automatically to create smooth curve through
|
|
84
|
+
* the knot. The smoothness of the resulting curve can be controlled via the
|
|
85
|
+
* {@link Spline.smoothness} property.
|
|
86
|
+
*
|
|
87
|
+
* It is also possible to blend between a user-defined position and the
|
|
88
|
+
* auto-calculated position by using the {@link auto} property.
|
|
89
|
+
*
|
|
90
|
+
* @defaultValue Mirrored position of the startHandle.
|
|
91
|
+
*/
|
|
92
|
+
@wrapper(Vector2)
|
|
93
|
+
@signal()
|
|
94
|
+
declare public readonly endHandle: Vector2Signal<this>;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* How much to blend between the user-provided handles and the auto-calculated
|
|
98
|
+
* handles.
|
|
99
|
+
*
|
|
100
|
+
* @remarks
|
|
101
|
+
* This property has no effect if no explicit handles are provided for the
|
|
102
|
+
* knot.
|
|
103
|
+
*
|
|
104
|
+
* @defaultValue 0
|
|
105
|
+
*/
|
|
106
|
+
@cloneable(false)
|
|
107
|
+
@initial(() => ({startHandle: 0, endHandle: 0}))
|
|
108
|
+
@parser((value: PossibleKnotAuto) => {
|
|
109
|
+
if (typeof value === 'object' && !Array.isArray(value)) {
|
|
110
|
+
return value;
|
|
111
|
+
}
|
|
112
|
+
if (typeof value === 'number') {
|
|
113
|
+
value = [value, value];
|
|
114
|
+
}
|
|
115
|
+
return {startHandle: value[0], endHandle: value[1]};
|
|
116
|
+
})
|
|
117
|
+
@compound({startHandle: 'startHandleAuto', endHandle: 'endHandleAuto'})
|
|
118
|
+
declare public readonly auto: KnotAutoSignal<this>;
|
|
119
|
+
public get startHandleAuto() {
|
|
120
|
+
return this.auto.startHandle;
|
|
121
|
+
}
|
|
122
|
+
public get endHandleAuto() {
|
|
123
|
+
return this.auto.endHandle;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
public constructor(props: KnotProps) {
|
|
127
|
+
super(
|
|
128
|
+
props.startHandle === undefined && props.endHandle === undefined
|
|
129
|
+
? {auto: 1, ...props}
|
|
130
|
+
: props,
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
@computed()
|
|
135
|
+
public points(): KnotInfo {
|
|
136
|
+
const hasExplicitHandles =
|
|
137
|
+
!this.startHandle.isInitial() || !this.endHandle.isInitial();
|
|
138
|
+
const startHandle = hasExplicitHandles ? this.startHandle() : Vector2.zero;
|
|
139
|
+
const endHandle = hasExplicitHandles ? this.endHandle() : Vector2.zero;
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
position: this.position(),
|
|
143
|
+
startHandle: startHandle.transformAsPoint(this.localToParent()),
|
|
144
|
+
endHandle: endHandle.transformAsPoint(this.localToParent()),
|
|
145
|
+
auto: {start: this.startHandleAuto(), end: this.endHandleAuto()},
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
private getDefaultEndHandle() {
|
|
150
|
+
return this.startHandle().flipped;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
private getDefaultStartHandle() {
|
|
154
|
+
return this.endHandle().flipped;
|
|
155
|
+
}
|
|
156
|
+
}
|