Skip to content

Commit 15ec453

Browse files
committed
Add multiple canvases demo, improve editor
1 parent d41fc1d commit 15ec453

File tree

10 files changed

+96
-31
lines changed

10 files changed

+96
-31
lines changed

demo/.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2+
"root": true,
23
"extends": "next/core-web-vitals"
34
}

demo/components/route/RouteLayer.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@ export const RouteLayer: FC<Props> = ({ route, index }) => {
1515
<Layer
1616
id={route.id}
1717
key={route.id}
18-
name={route.name}
1918
active={state.routeIndex === index}
2019
visible={!!state.image}
2120
>
2221
{route.items.map((item) => {
2322
const Item = Items[item.type];
24-
return <Item {...item} id={item.id} key={item.id} selected={state.selection === item.id} />;
23+
return (
24+
<Item
25+
{...item}
26+
id={item.id}
27+
key={item.id}
28+
selected={state.selection === item.id}
29+
/>
30+
);
2531
})}
2632
</Layer>
2733
);

demo/components/tools/Circle.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ export const Circle: FC = () => {
1515
if (state.scope) {
1616
circle.current = new state.scope.Path.Circle({
1717
...defaultProps,
18+
insert: true,
1819
center: event.point,
1920
radius: 10,
2021
});
22+
state.scope.project.activeLayer.addChild(circle.current);
2123
}
2224
},
2325
[state.scope]

demo/components/tools/Pen.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const Pen: FC = () => {
2222
if (!path.current) {
2323
path.current = new state.scope.Path({
2424
...defaultProps,
25+
insert: true,
2526
segments: [event.point],
2627
});
2728
} else {

demo/pages/editor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Head from "next/head";
33
import type { NextPage } from "next";
44

55
import { Provider, Paper } from "components";
6-
import styles from "../styles/Editor.module.css";
6+
import styles from "../styles/Styles.module.css";
77

88
const data = {
99
id: "37ag863f-8530-4716-512d-5jr2bd3be5d2",

demo/pages/index.tsx

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,34 @@ import Head from "next/head";
33
import type { NextPage } from "next";
44

55
import { Canvas, View, Layer, Rectangle } from "react-paper-renderer";
6-
import styles from "../styles/Home.module.css";
6+
import styles from "../styles/Styles.module.css";
7+
8+
function move(arr: any[], from: number, to: number) {
9+
return arr.reduce((prev, current, idx, self) => {
10+
if (from === to) prev.push(current);
11+
if (idx === from) return prev;
12+
if (from < to) prev.push(current);
13+
if (idx === to) prev.push(self[from]);
14+
if (from > to) prev.push(current);
15+
return prev;
16+
}, []);
17+
}
718

819
const Home: NextPage = () => {
9-
const [color, setColor] = useState("#000000");
20+
const [rects, setRects] = useState([
21+
{ id: 1, center: [100, 100], fillColor: "red", size: [50, 50] },
22+
{ id: 2, center: [120, 120], fillColor: "green", size: [50, 50] },
23+
{ id: 3, center: [140, 140], fillColor: "orange", size: [50, 50] },
24+
]);
25+
const [visible, setVisible] = useState(true);
26+
const [visible2, setVisible2] = useState(true);
27+
const [color, setColor] = useState("black");
28+
const [color2, setColor2] = useState("white");
1029
const onClick = () => {
11-
setColor(color === "#000000" ? "#ff0000" : "#000000");
30+
setColor(color === "black" ? "white" : "black");
31+
};
32+
const onClick2 = () => {
33+
setColor2(color2 === "black" ? "white" : "black");
1234
};
1335
return (
1436
<>
@@ -18,18 +40,45 @@ const Home: NextPage = () => {
1840
<link rel="icon" href="/favicon.ico" />
1941
</Head>
2042
<div className={styles.container}>
21-
<Canvas width={800} height={600}>
22-
<View>
23-
<Layer>
24-
<Rectangle
25-
center={[200, 200]}
26-
fillColor={color}
27-
size={[100, 100]}
28-
onClick={onClick}
29-
/>
30-
</Layer>
31-
</View>
32-
</Canvas>
43+
<div>
44+
<button onClick={() => setVisible(!visible)}>canvas1</button>
45+
<button onClick={() => setVisible2(!visible2)}>canvas2</button>
46+
</div>
47+
{visible && (
48+
<Canvas className={styles.canvas} width={400} height={300}>
49+
<View>
50+
<Layer>
51+
<Rectangle
52+
center={[75, 75]}
53+
fillColor={color}
54+
size={[50, 50]}
55+
onClick={onClick}
56+
/>
57+
{rects.map((rect) => (
58+
<Rectangle
59+
{...rect}
60+
key={rect.id}
61+
onClick={() => setRects(move(rects, 0, 2))}
62+
/>
63+
))}
64+
</Layer>
65+
</View>
66+
</Canvas>
67+
)}
68+
{visible2 && (
69+
<Canvas className={styles.canvas} width={400} height={300}>
70+
<View>
71+
<Layer>
72+
<Rectangle
73+
center={[200, 100]}
74+
fillColor={color2}
75+
size={[100, 100]}
76+
onClick={onClick2}
77+
/>
78+
</Layer>
79+
</View>
80+
</Canvas>
81+
)}
3382
</div>
3483
</>
3584
);

demo/styles/Editor.module.css

Lines changed: 0 additions & 6 deletions
This file was deleted.

demo/styles/Home.module.css

Lines changed: 0 additions & 7 deletions
This file was deleted.

demo/styles/Styles.module.css

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.container {
2+
border: 2px solid blue;
3+
display: block;
4+
width: calc(100vw - 100px);
5+
height: calc(100vh - 100px);
6+
margin: 50px 0 0 50px;
7+
}
8+
9+
.editor {
10+
/*border: 2px solid blue;*/
11+
display: block;
12+
width: calc(100vw - 0px);
13+
height: calc(100vh - 0px);
14+
}
15+
16+
.canvas {
17+
border: 2px solid red;
18+
}

demo/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
},
2222
"include": [
2323
"next-env.d.ts",
24+
"../types/overrides.d.ts",
2425
"**/*.ts",
2526
"**/*.tsx",
2627
"../src/**/*.ts",

0 commit comments

Comments
 (0)