@blinkorb/rcx 0.0.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/.eslintignore +4 -0
- package/.eslintrc.json +286 -0
- package/.gitattributes +2 -0
- package/.github/CODEOWNERS +1 -0
- package/.github/workflows/ci.yml +19 -0
- package/.nvmrc +1 -0
- package/.prettierignore +28 -0
- package/.prettierrc.json +4 -0
- package/demo/index.html +29 -0
- package/demo/index.tsx +316 -0
- package/demo/tsconfig.json +12 -0
- package/jest.config.ts +21 -0
- package/package.json +80 -0
- package/scripts/prep-package.js +29 -0
- package/src/components/canvas/context.ts +6 -0
- package/src/components/canvas/index.ts +98 -0
- package/src/components/index.ts +5 -0
- package/src/components/paths/arc-to.ts +66 -0
- package/src/components/paths/clip.ts +32 -0
- package/src/components/paths/index.ts +5 -0
- package/src/components/paths/line.ts +53 -0
- package/src/components/paths/path.ts +59 -0
- package/src/components/paths/point.ts +24 -0
- package/src/components/shapes/circle.tsx +32 -0
- package/src/components/shapes/ellipse.ts +75 -0
- package/src/components/shapes/index.ts +3 -0
- package/src/components/shapes/rectangle.ts +45 -0
- package/src/components/text/index.ts +1 -0
- package/src/components/text/text.ts +137 -0
- package/src/components/transform/index.ts +3 -0
- package/src/components/transform/rotate.ts +26 -0
- package/src/components/transform/scale.ts +34 -0
- package/src/components/transform/translate.ts +27 -0
- package/src/context/create-context.ts +49 -0
- package/src/context/index.ts +1 -0
- package/src/hooks/index.ts +8 -0
- package/src/hooks/use-canvas-context.ts +11 -0
- package/src/hooks/use-linear-gradient.ts +39 -0
- package/src/hooks/use-loop.ts +11 -0
- package/src/hooks/use-on.ts +18 -0
- package/src/hooks/use-radial-gradient.ts +45 -0
- package/src/hooks/use-render.ts +14 -0
- package/src/hooks/use-state.ts +9 -0
- package/src/hooks/use-window-size.ts +24 -0
- package/src/index.ts +6 -0
- package/src/internal/emitter.ts +39 -0
- package/src/internal/global.ts +5 -0
- package/src/internal/hooks.ts +32 -0
- package/src/internal/reactive.test.ts +20 -0
- package/src/internal/reactive.ts +20 -0
- package/src/jsx-runtime.ts +21 -0
- package/src/render.ts +299 -0
- package/src/types.ts +151 -0
- package/src/utils/apply-fill-and-stroke-style.ts +33 -0
- package/src/utils/get-recommended-pixel-ratio.ts +2 -0
- package/src/utils/index.ts +8 -0
- package/src/utils/is-own-property-of.ts +6 -0
- package/src/utils/is-valid-fill-or-stroke-style.ts +5 -0
- package/src/utils/is-valid-stroke-cap.ts +10 -0
- package/src/utils/is-valid-stroke-join.ts +10 -0
- package/src/utils/resolve-styles.ts +21 -0
- package/src/utils/type-guards.ts +4 -0
- package/src/utils/with-px.ts +4 -0
- package/tsb.config.ts +11 -0
- package/tsconfig.dist.json +13 -0
- package/tsconfig.json +25 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"sourceMap": true,
|
|
5
|
+
"paths": {
|
|
6
|
+
"@blinkorb/rcx": ["../src/index.ts"],
|
|
7
|
+
"@blinkorb/rcx/canvas": ["../src/components/canvas/index.ts"],
|
|
8
|
+
"@blinkorb/rcx/*": ["../src/*"]
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
"include": ["./*", "../src/*"]
|
|
12
|
+
}
|
package/jest.config.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Config } from 'jest';
|
|
2
|
+
|
|
3
|
+
const config = {
|
|
4
|
+
preset: 'ts-jest/presets/default-esm',
|
|
5
|
+
testMatch: ['<rootDir>/src/**/*.(spec|test).{js,jsx,ts,tsx}'],
|
|
6
|
+
collectCoverageFrom: ['src/**/*.(js|jsx|ts|tsx)'],
|
|
7
|
+
// coverageThreshold: {
|
|
8
|
+
// global: {
|
|
9
|
+
// branches: 100,
|
|
10
|
+
// functions: 100,
|
|
11
|
+
// lines: 100,
|
|
12
|
+
// statements: 100,
|
|
13
|
+
// },
|
|
14
|
+
// },
|
|
15
|
+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
|
|
16
|
+
moduleNameMapper: {
|
|
17
|
+
'(.+)\\.js$': '$1',
|
|
18
|
+
},
|
|
19
|
+
} satisfies Config;
|
|
20
|
+
|
|
21
|
+
export default config;
|
package/package.json
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@blinkorb/rcx",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Reactive JSX-based library for creating HTML5 canvas applications",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "./src/index.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./src/index.ts"
|
|
13
|
+
},
|
|
14
|
+
"./canvas": {
|
|
15
|
+
"import": "./src/components/canvas/index.ts"
|
|
16
|
+
},
|
|
17
|
+
"./components": {
|
|
18
|
+
"import": "./src/components/index.ts"
|
|
19
|
+
},
|
|
20
|
+
"./context": {
|
|
21
|
+
"import": "./src/context/index.ts"
|
|
22
|
+
},
|
|
23
|
+
"./hooks": {
|
|
24
|
+
"import": "./src/hooks/index.ts"
|
|
25
|
+
},
|
|
26
|
+
"./utils": {
|
|
27
|
+
"import": "./src/utils/index.ts"
|
|
28
|
+
},
|
|
29
|
+
"./jsx-runtime": {
|
|
30
|
+
"import": "./src/jsx-runtime.ts"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"start": "npm run dev",
|
|
35
|
+
"dev": "tsb serve",
|
|
36
|
+
"dist": "npm run build",
|
|
37
|
+
"build": "rm -rf dist && tsc -p tsconfig.dist.json --rewriteRelativeImportExtensions && node scripts/prep-package.js",
|
|
38
|
+
"typecheck": "tsc --noEmit",
|
|
39
|
+
"format-check": "prettier --check *",
|
|
40
|
+
"format": "prettier --write *",
|
|
41
|
+
"lint": "eslint '**/*.{ts,tsx,js,jsx}'",
|
|
42
|
+
"tests": "jest --coverage --runInBand",
|
|
43
|
+
"test": "npm run format-check && npm run typecheck && npm run lint && npm run tests",
|
|
44
|
+
"prepublishOnly": "npm run test && npm run dist"
|
|
45
|
+
},
|
|
46
|
+
"repository": {
|
|
47
|
+
"type": "git",
|
|
48
|
+
"url": "git+https://github.com/blinkorb/rcx.git"
|
|
49
|
+
},
|
|
50
|
+
"keywords": [
|
|
51
|
+
"canvas",
|
|
52
|
+
"jsx",
|
|
53
|
+
"view",
|
|
54
|
+
"library",
|
|
55
|
+
"reactive",
|
|
56
|
+
"html5",
|
|
57
|
+
"game"
|
|
58
|
+
],
|
|
59
|
+
"author": "BlinkOrb",
|
|
60
|
+
"license": "MIT",
|
|
61
|
+
"bugs": {
|
|
62
|
+
"url": "https://github.com/blinkorb/rcx/issues"
|
|
63
|
+
},
|
|
64
|
+
"homepage": "https://github.com/blinkorb/rcx#readme",
|
|
65
|
+
"devDependencies": {
|
|
66
|
+
"@jakesidsmith/tsb": "^3.0.2",
|
|
67
|
+
"@types/jest": "^29.5.12",
|
|
68
|
+
"@typescript-eslint/eslint-plugin": "^7.17.0",
|
|
69
|
+
"@typescript-eslint/parser": "^7.17.0",
|
|
70
|
+
"eslint": "^8.57.0",
|
|
71
|
+
"eslint-config-prettier": "^9.1.0",
|
|
72
|
+
"eslint-plugin-prettier": "^5.2.1",
|
|
73
|
+
"eslint-plugin-simple-import-sort": "^12.1.1",
|
|
74
|
+
"jest": "^29.7.0",
|
|
75
|
+
"prettier": "^3.3.3",
|
|
76
|
+
"ts-jest": "^29.2.4",
|
|
77
|
+
"ts-node": "^10.9.2",
|
|
78
|
+
"typescript": "^5.7.3"
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { writeFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, resolve } from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
|
|
5
|
+
import packageJson from '../package.json' with { type: 'json' };
|
|
6
|
+
|
|
7
|
+
const cwd = dirname(fileURLToPath(import.meta.url));
|
|
8
|
+
|
|
9
|
+
const modifiedJson = {
|
|
10
|
+
...packageJson,
|
|
11
|
+
main: packageJson.main.replace('src/', '').replace('.ts', '.js'),
|
|
12
|
+
exports: Object.fromEntries(
|
|
13
|
+
Object.entries(packageJson.exports).map(([key, imports]) => [
|
|
14
|
+
key,
|
|
15
|
+
Object.fromEntries(
|
|
16
|
+
Object.entries(imports).map(([path, actual]) => [
|
|
17
|
+
path,
|
|
18
|
+
actual.replace('src/', '').replace('.ts', '.js'),
|
|
19
|
+
])
|
|
20
|
+
),
|
|
21
|
+
])
|
|
22
|
+
),
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
writeFileSync(
|
|
26
|
+
resolve(cwd, '../dist/package.json'),
|
|
27
|
+
JSON.stringify(modifiedJson, null, 2),
|
|
28
|
+
'utf-8'
|
|
29
|
+
);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { createContext } from '../../context/create-context.ts';
|
|
2
|
+
import type { RCXCanvasContext, RCXRenderingContext } from '../../types.ts';
|
|
3
|
+
|
|
4
|
+
export const canvasContext = createContext<RCXCanvasContext>();
|
|
5
|
+
|
|
6
|
+
export const renderingContext = createContext<RCXRenderingContext>();
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { useOnMount } from '../../hooks/use-on.ts';
|
|
2
|
+
import { useRenderBeforeChildren } from '../../hooks/use-render.ts';
|
|
3
|
+
import { useReactive, useUnreactive } from '../../hooks/use-state.ts';
|
|
4
|
+
import type { RCXComponent, RCXPropsWithChildren } from '../../types.ts';
|
|
5
|
+
import { getRecommendedPixelRatio } from '../../utils/get-recommended-pixel-ratio.ts';
|
|
6
|
+
import { canvasContext, renderingContext } from './context.ts';
|
|
7
|
+
|
|
8
|
+
export type CanvasProps = RCXPropsWithChildren<{
|
|
9
|
+
width?: number | 'auto';
|
|
10
|
+
height?: number | 'auto';
|
|
11
|
+
pixelRatio?: number | 'auto';
|
|
12
|
+
}>;
|
|
13
|
+
|
|
14
|
+
const getValueOrAuto = (
|
|
15
|
+
value: undefined | number | 'auto',
|
|
16
|
+
autoValue: number
|
|
17
|
+
) => {
|
|
18
|
+
if (typeof value === 'number') {
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return autoValue;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const Canvas: RCXComponent<CanvasProps> = (props) => {
|
|
26
|
+
const renderingContextStateRoot = renderingContext.useInject();
|
|
27
|
+
|
|
28
|
+
if (!renderingContextStateRoot) {
|
|
29
|
+
throw new Error('Canvas was rendered outside of an application');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const initialCanvasSize =
|
|
33
|
+
renderingContextStateRoot.canvas.getBoundingClientRect();
|
|
34
|
+
const canvasSize = useReactive({
|
|
35
|
+
width: initialCanvasSize.width,
|
|
36
|
+
height: initialCanvasSize.height,
|
|
37
|
+
});
|
|
38
|
+
const resizeObserver = useUnreactive(
|
|
39
|
+
new ResizeObserver((entries) => {
|
|
40
|
+
const canvasEntry = entries[0];
|
|
41
|
+
if (typeof canvasEntry === 'undefined') {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const rect = canvasEntry.target.getBoundingClientRect();
|
|
46
|
+
canvasSize.width = rect.width;
|
|
47
|
+
canvasSize.height = rect.height;
|
|
48
|
+
})
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
useOnMount(() => {
|
|
52
|
+
resizeObserver.observe(renderingContextStateRoot.canvas);
|
|
53
|
+
|
|
54
|
+
return () => {
|
|
55
|
+
resizeObserver.disconnect();
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
useRenderBeforeChildren((renderingContextState) => {
|
|
60
|
+
const pixelRatio = getValueOrAuto(
|
|
61
|
+
props.pixelRatio,
|
|
62
|
+
getRecommendedPixelRatio()
|
|
63
|
+
);
|
|
64
|
+
const rect = renderingContextState.canvas.getBoundingClientRect();
|
|
65
|
+
const width =
|
|
66
|
+
getValueOrAuto(props.width, rect.width * pixelRatio) / pixelRatio;
|
|
67
|
+
const height =
|
|
68
|
+
getValueOrAuto(props.height, rect.height * pixelRatio) / pixelRatio;
|
|
69
|
+
|
|
70
|
+
renderingContextState.canvas.width = width * pixelRatio;
|
|
71
|
+
renderingContextState.canvas.height = height * pixelRatio;
|
|
72
|
+
renderingContextState.ctx2d.scale(pixelRatio, pixelRatio);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const pixelRatio = getValueOrAuto(
|
|
76
|
+
props.pixelRatio,
|
|
77
|
+
getRecommendedPixelRatio()
|
|
78
|
+
);
|
|
79
|
+
const rect = renderingContextStateRoot.canvas.getBoundingClientRect();
|
|
80
|
+
const width =
|
|
81
|
+
getValueOrAuto(props.width, rect.width * pixelRatio) / pixelRatio;
|
|
82
|
+
const height =
|
|
83
|
+
getValueOrAuto(props.height, rect.height * pixelRatio) / pixelRatio;
|
|
84
|
+
|
|
85
|
+
canvasContext.useProvide({
|
|
86
|
+
...renderingContextStateRoot,
|
|
87
|
+
props,
|
|
88
|
+
width,
|
|
89
|
+
height,
|
|
90
|
+
pixelRatio,
|
|
91
|
+
actualWidth: width * pixelRatio,
|
|
92
|
+
actualHeight: height * pixelRatio,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
return props.children;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
Canvas.displayName = 'Canvas';
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useRenderAfterChildren,
|
|
3
|
+
useRenderBeforeChildren,
|
|
4
|
+
} from '../../hooks/use-render.ts';
|
|
5
|
+
import type {
|
|
6
|
+
RCXComponent,
|
|
7
|
+
RCXPropsWithChildren,
|
|
8
|
+
RCXShapeStyle,
|
|
9
|
+
RCXStyleProp,
|
|
10
|
+
} from '../../types.ts';
|
|
11
|
+
import { applyFillAndStrokeStyles } from '../../utils/apply-fill-and-stroke-style.ts';
|
|
12
|
+
import { resolveStyles } from '../../utils/resolve-styles.ts';
|
|
13
|
+
|
|
14
|
+
export type ArcToProps = RCXPropsWithChildren<{
|
|
15
|
+
startControlX: number;
|
|
16
|
+
startControlY: number;
|
|
17
|
+
endControlX: number;
|
|
18
|
+
endControlY: number;
|
|
19
|
+
radius: number;
|
|
20
|
+
style?: RCXStyleProp<RCXShapeStyle>;
|
|
21
|
+
beginPath?: boolean;
|
|
22
|
+
closePath?: boolean;
|
|
23
|
+
}>;
|
|
24
|
+
|
|
25
|
+
export const ArcTo: RCXComponent<ArcToProps> = (props) => {
|
|
26
|
+
useRenderBeforeChildren((renderingContext) => {
|
|
27
|
+
const {
|
|
28
|
+
startControlX,
|
|
29
|
+
startControlY,
|
|
30
|
+
endControlX,
|
|
31
|
+
endControlY,
|
|
32
|
+
radius,
|
|
33
|
+
beginPath = false,
|
|
34
|
+
} = props;
|
|
35
|
+
|
|
36
|
+
renderingContext.ctx2d.save();
|
|
37
|
+
|
|
38
|
+
if (beginPath) {
|
|
39
|
+
renderingContext.ctx2d.beginPath();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
renderingContext.ctx2d.arcTo(
|
|
43
|
+
startControlX,
|
|
44
|
+
startControlY,
|
|
45
|
+
endControlX,
|
|
46
|
+
endControlY,
|
|
47
|
+
radius
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
useRenderAfterChildren((renderingContext) => {
|
|
52
|
+
const { closePath = false } = props;
|
|
53
|
+
|
|
54
|
+
if (closePath) {
|
|
55
|
+
renderingContext.ctx2d.closePath();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
applyFillAndStrokeStyles(renderingContext, resolveStyles(props.style));
|
|
59
|
+
|
|
60
|
+
renderingContext.ctx2d.restore();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
return props.children;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
ArcTo.displayName = 'ArcTo';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useRenderAfterChildren,
|
|
3
|
+
useRenderBeforeChildren,
|
|
4
|
+
} from '../../hooks/use-render.ts';
|
|
5
|
+
import type { RCXComponent, RCXPropsWithChildren } from '../../types.ts';
|
|
6
|
+
|
|
7
|
+
export type ClipProps = RCXPropsWithChildren<{
|
|
8
|
+
path?: Path2D;
|
|
9
|
+
fillRule?: CanvasFillRule;
|
|
10
|
+
}>;
|
|
11
|
+
|
|
12
|
+
export const Clip: RCXComponent<ClipProps> = (props) => {
|
|
13
|
+
useRenderBeforeChildren((renderingContext) => {
|
|
14
|
+
const { path, fillRule } = props;
|
|
15
|
+
|
|
16
|
+
renderingContext.ctx2d.save();
|
|
17
|
+
|
|
18
|
+
if (typeof path === 'undefined') {
|
|
19
|
+
renderingContext.ctx2d.clip(fillRule);
|
|
20
|
+
} else {
|
|
21
|
+
renderingContext.ctx2d.clip(path, fillRule);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
useRenderAfterChildren((renderingContext) => {
|
|
26
|
+
renderingContext.ctx2d.restore();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
return props.children;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
Clip.displayName = 'Clip';
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useRenderAfterChildren,
|
|
3
|
+
useRenderBeforeChildren,
|
|
4
|
+
} from '../../hooks/use-render.ts';
|
|
5
|
+
import type {
|
|
6
|
+
RCXComponent,
|
|
7
|
+
RCXLineStyle,
|
|
8
|
+
RCXPropsWithChildren,
|
|
9
|
+
RCXStyleProp,
|
|
10
|
+
} from '../../types.ts';
|
|
11
|
+
import { applyFillAndStrokeStyles } from '../../utils/apply-fill-and-stroke-style.ts';
|
|
12
|
+
import { resolveStyles } from '../../utils/resolve-styles.ts';
|
|
13
|
+
|
|
14
|
+
export type LineProps = RCXPropsWithChildren<{
|
|
15
|
+
startX: number;
|
|
16
|
+
startY: number;
|
|
17
|
+
endX: number;
|
|
18
|
+
endY: number;
|
|
19
|
+
beginPath?: boolean;
|
|
20
|
+
closePath?: boolean;
|
|
21
|
+
style?: RCXStyleProp<RCXLineStyle>;
|
|
22
|
+
}>;
|
|
23
|
+
|
|
24
|
+
export const Line: RCXComponent<LineProps> = (props) => {
|
|
25
|
+
useRenderBeforeChildren((renderingContext) => {
|
|
26
|
+
const { startX, startY, endX, endY, beginPath = true } = props;
|
|
27
|
+
|
|
28
|
+
renderingContext.ctx2d.save();
|
|
29
|
+
|
|
30
|
+
if (beginPath) {
|
|
31
|
+
renderingContext.ctx2d.beginPath();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
renderingContext.ctx2d.moveTo(startX, startY);
|
|
35
|
+
renderingContext.ctx2d.lineTo(endX, endY);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
useRenderAfterChildren((renderingContext) => {
|
|
39
|
+
const { closePath = false } = props;
|
|
40
|
+
|
|
41
|
+
if (closePath) {
|
|
42
|
+
renderingContext.ctx2d.closePath();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
applyFillAndStrokeStyles(renderingContext, resolveStyles(props.style));
|
|
46
|
+
|
|
47
|
+
renderingContext.ctx2d.restore();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
return props.children;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
Line.displayName = 'Line';
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useRenderAfterChildren,
|
|
3
|
+
useRenderBeforeChildren,
|
|
4
|
+
} from '../../hooks/use-render.ts';
|
|
5
|
+
import type {
|
|
6
|
+
RCXComponent,
|
|
7
|
+
RCXPoint,
|
|
8
|
+
RCXPropsWithChildren,
|
|
9
|
+
RCXShapeStyle,
|
|
10
|
+
RCXStyleProp,
|
|
11
|
+
} from '../../types.ts';
|
|
12
|
+
import { applyFillAndStrokeStyles } from '../../utils/apply-fill-and-stroke-style.ts';
|
|
13
|
+
import { resolveStyles } from '../../utils/resolve-styles.ts';
|
|
14
|
+
import { isArray } from '../../utils/type-guards.ts';
|
|
15
|
+
|
|
16
|
+
export type PathProps = RCXPropsWithChildren<{
|
|
17
|
+
points?: readonly RCXPoint[];
|
|
18
|
+
beginPath?: boolean;
|
|
19
|
+
closePath?: boolean;
|
|
20
|
+
style?: RCXStyleProp<RCXShapeStyle>;
|
|
21
|
+
}>;
|
|
22
|
+
|
|
23
|
+
export const Path: RCXComponent<PathProps> = (props) => {
|
|
24
|
+
useRenderBeforeChildren((renderingContext) => {
|
|
25
|
+
const { points, beginPath = true } = props;
|
|
26
|
+
|
|
27
|
+
renderingContext.ctx2d.save();
|
|
28
|
+
|
|
29
|
+
if (beginPath) {
|
|
30
|
+
renderingContext.ctx2d.beginPath();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
points?.forEach((point, index) => {
|
|
34
|
+
const [x, y] = isArray(point) ? point : [point.x, point.y];
|
|
35
|
+
|
|
36
|
+
if (index === 0) {
|
|
37
|
+
renderingContext.ctx2d.moveTo(x, y);
|
|
38
|
+
} else {
|
|
39
|
+
renderingContext.ctx2d.lineTo(x, y);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
useRenderAfterChildren((renderingContext) => {
|
|
45
|
+
const { closePath = false } = props;
|
|
46
|
+
|
|
47
|
+
if (closePath) {
|
|
48
|
+
renderingContext.ctx2d.closePath();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
applyFillAndStrokeStyles(renderingContext, resolveStyles(props.style));
|
|
52
|
+
|
|
53
|
+
renderingContext.ctx2d.restore();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
return props.children;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
Path.displayName = 'Path';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { useRenderBeforeChildren } from '../../hooks/use-render.ts';
|
|
2
|
+
import type { RCXComponent, RCXPropsWithChildren } from '../../types.ts';
|
|
3
|
+
|
|
4
|
+
export type PointProps = RCXPropsWithChildren<{
|
|
5
|
+
x: number;
|
|
6
|
+
y: number;
|
|
7
|
+
lineTo?: boolean;
|
|
8
|
+
}>;
|
|
9
|
+
|
|
10
|
+
export const Point: RCXComponent<PointProps> = (props) => {
|
|
11
|
+
useRenderBeforeChildren((renderingContext) => {
|
|
12
|
+
const { x, y, lineTo = true } = props;
|
|
13
|
+
|
|
14
|
+
if (lineTo) {
|
|
15
|
+
renderingContext.ctx2d.lineTo(x, y);
|
|
16
|
+
} else {
|
|
17
|
+
renderingContext.ctx2d.moveTo(x, y);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
return props.children;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
Point.displayName = 'Point';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
RCXComponent,
|
|
3
|
+
RCXPropsWithChildren,
|
|
4
|
+
RCXShapeStyle,
|
|
5
|
+
RCXStyleProp,
|
|
6
|
+
} from '../../types.ts';
|
|
7
|
+
import { Ellipse } from './ellipse.ts';
|
|
8
|
+
|
|
9
|
+
export type CircleProps = RCXPropsWithChildren<{
|
|
10
|
+
x: number;
|
|
11
|
+
y: number;
|
|
12
|
+
radius: number;
|
|
13
|
+
rotation?: number;
|
|
14
|
+
startAngle?: number;
|
|
15
|
+
endAngle?: number;
|
|
16
|
+
counterClockwise?: boolean;
|
|
17
|
+
beginPath?: boolean;
|
|
18
|
+
closePath?: boolean;
|
|
19
|
+
style?: RCXStyleProp<RCXShapeStyle>;
|
|
20
|
+
}>;
|
|
21
|
+
|
|
22
|
+
export const Circle: RCXComponent<CircleProps> = (props) => {
|
|
23
|
+
const { radius, ...rest } = props;
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<Ellipse {...rest} radiusX={radius} radiusY={radius}>
|
|
27
|
+
{props.children}
|
|
28
|
+
</Ellipse>
|
|
29
|
+
);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
Circle.displayName = 'Circle';
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useRenderAfterChildren,
|
|
3
|
+
useRenderBeforeChildren,
|
|
4
|
+
} from '../../hooks/use-render.ts';
|
|
5
|
+
import type {
|
|
6
|
+
RCXComponent,
|
|
7
|
+
RCXPropsWithChildren,
|
|
8
|
+
RCXShapeStyle,
|
|
9
|
+
RCXStyleProp,
|
|
10
|
+
} from '../../types.ts';
|
|
11
|
+
import { applyFillAndStrokeStyles } from '../../utils/apply-fill-and-stroke-style.ts';
|
|
12
|
+
import { resolveStyles } from '../../utils/resolve-styles.ts';
|
|
13
|
+
|
|
14
|
+
export type EllipseProps = RCXPropsWithChildren<{
|
|
15
|
+
x: number;
|
|
16
|
+
y: number;
|
|
17
|
+
radiusX: number;
|
|
18
|
+
radiusY: number;
|
|
19
|
+
rotation?: number;
|
|
20
|
+
startAngle?: number;
|
|
21
|
+
endAngle?: number;
|
|
22
|
+
counterClockwise?: boolean;
|
|
23
|
+
beginPath?: boolean;
|
|
24
|
+
closePath?: boolean;
|
|
25
|
+
style?: RCXStyleProp<RCXShapeStyle>;
|
|
26
|
+
}>;
|
|
27
|
+
|
|
28
|
+
export const Ellipse: RCXComponent<EllipseProps> = (props) => {
|
|
29
|
+
useRenderBeforeChildren((renderingContext) => {
|
|
30
|
+
const {
|
|
31
|
+
x,
|
|
32
|
+
y,
|
|
33
|
+
radiusX,
|
|
34
|
+
radiusY,
|
|
35
|
+
rotation = 0,
|
|
36
|
+
startAngle = 0,
|
|
37
|
+
endAngle = Math.PI * 2,
|
|
38
|
+
counterClockwise = false,
|
|
39
|
+
beginPath = true,
|
|
40
|
+
} = props;
|
|
41
|
+
|
|
42
|
+
renderingContext.ctx2d.save();
|
|
43
|
+
|
|
44
|
+
if (beginPath) {
|
|
45
|
+
renderingContext.ctx2d.beginPath();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
renderingContext.ctx2d.ellipse(
|
|
49
|
+
x,
|
|
50
|
+
y,
|
|
51
|
+
radiusX,
|
|
52
|
+
radiusY,
|
|
53
|
+
rotation,
|
|
54
|
+
startAngle,
|
|
55
|
+
endAngle,
|
|
56
|
+
counterClockwise
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
useRenderAfterChildren((renderingContext) => {
|
|
61
|
+
const { closePath = true } = props;
|
|
62
|
+
|
|
63
|
+
if (closePath) {
|
|
64
|
+
renderingContext.ctx2d.closePath();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
applyFillAndStrokeStyles(renderingContext, resolveStyles(props.style));
|
|
68
|
+
|
|
69
|
+
renderingContext.ctx2d.restore();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
return props.children;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
Ellipse.displayName = 'Ellipse';
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useRenderAfterChildren,
|
|
3
|
+
useRenderBeforeChildren,
|
|
4
|
+
} from '../../hooks/use-render.ts';
|
|
5
|
+
import type {
|
|
6
|
+
RCXComponent,
|
|
7
|
+
RCXPropsWithChildren,
|
|
8
|
+
RCXShapeStyle,
|
|
9
|
+
RCXStyleProp,
|
|
10
|
+
} from '../../types.ts';
|
|
11
|
+
import { applyFillAndStrokeStyles } from '../../utils/apply-fill-and-stroke-style.ts';
|
|
12
|
+
import { resolveStyles } from '../../utils/resolve-styles.ts';
|
|
13
|
+
|
|
14
|
+
export type RectangleProps = RCXPropsWithChildren<{
|
|
15
|
+
x: number;
|
|
16
|
+
y: number;
|
|
17
|
+
width: number;
|
|
18
|
+
height: number;
|
|
19
|
+
beginPath?: boolean;
|
|
20
|
+
style?: RCXStyleProp<RCXShapeStyle>;
|
|
21
|
+
}>;
|
|
22
|
+
|
|
23
|
+
export const Rectangle: RCXComponent<RectangleProps> = (props) => {
|
|
24
|
+
useRenderBeforeChildren((renderingContext) => {
|
|
25
|
+
const { x, y, width, height, beginPath = true } = props;
|
|
26
|
+
|
|
27
|
+
renderingContext.ctx2d.save();
|
|
28
|
+
|
|
29
|
+
if (beginPath) {
|
|
30
|
+
renderingContext.ctx2d.beginPath();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
renderingContext.ctx2d.rect(x, y, width, height);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
useRenderAfterChildren((renderingContext) => {
|
|
37
|
+
applyFillAndStrokeStyles(renderingContext, resolveStyles(props.style));
|
|
38
|
+
|
|
39
|
+
renderingContext.ctx2d.restore();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
return props.children;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
Rectangle.displayName = 'Rectangle';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './text.ts';
|