-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathApp.tsx
170 lines (167 loc) · 4.92 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import React, { useState } from "react";
import {
useBrush,
useMarker,
useAirbrush,
Artboard,
ArtboardRef,
useShadingBrush,
useEraser,
useWatercolor,
ToolHandlers,
} from "../src/";
import {
FaPencilAlt,
FaPaintBrush,
FaMarker,
FaEraser,
FaSprayCan,
FaDownload,
FaTrash,
FaUndo,
FaRedo,
FaGithub,
} from "react-icons/fa";
import { HexColorPicker } from "react-colorful";
import { IoMdWater } from "react-icons/io";
import { useHistory } from "../src/history";
import "./style.css";
import "react-colorful/dist/index.css";
import "react-responsive-modal/styles.css";
import "react-rangeslider/lib/index.css";
import Slider from "react-rangeslider";
import { Modal } from "react-responsive-modal";
import type { IconType } from "react-icons/lib";
export function App(): JSX.Element {
const [color, setColor] = useState("#531B93");
const [strokeWidth, setStrokeWidth] = useState(40);
const [colorOpen, setColorOpen] = useState(false);
const [sizeOpen, setSizeOpen] = useState(false);
const [artboardRef, setArtboardRef] = useState<ArtboardRef | null>();
const brush = useBrush({ color, strokeWidth });
const marker = useMarker({ color, strokeWidth });
const watercolor = useWatercolor({ color, strokeWidth });
const airbrush = useAirbrush({ color, strokeWidth });
const eraser = useEraser({ strokeWidth });
const shading = useShadingBrush({
color,
spreadFactor: (1 / 45) * strokeWidth,
distanceThreshold: 100,
});
const tools: Array<[ToolHandlers, IconType]> = [
[shading, FaPencilAlt],
[watercolor, IoMdWater],
[brush, FaPaintBrush],
[marker, FaMarker],
[airbrush, FaSprayCan],
[eraser, FaEraser],
];
const [currentTool, setCurrentTool] = useState(0);
const { undo, redo, history, canUndo, canRedo } = useHistory();
return (
<main>
<h1 id="name">
<a href="https://github.com/ascorbic/react-artboard">
react-artboard <FaGithub />
</a>
</h1>
<div id="topTools" className="toolbarSection"></div>
<div id="brushes" className="toolbarSection">
{tools.map(([tool, Icon], index) => (
<button
aria-label={tool.name}
key={tool.name}
title={tool.name}
style={{
backgroundColor: currentTool === index ? "#aaaaff" : "#eeeeee",
}}
onClick={() => setCurrentTool(index)}
>
{<Icon size={14} title={tool.name} />}
</button>
))}
<label>
Color:
<button
onClick={() => setColorOpen(true)}
style={{
backgroundColor: color,
width: 50,
border: "2px gray solid",
color: "transparent",
}}
>
Color
</button>
<Modal open={colorOpen} onClose={() => setColorOpen(false)}>
<div style={{ padding: 30 }}>
<HexColorPicker color={color} onChange={setColor} />
</div>
</Modal>
</label>
<label>
Size:
<button onClick={() => setSizeOpen(true)}>{strokeWidth}</button>
<Modal open={sizeOpen} onClose={() => setSizeOpen(false)}>
<div
style={{
width: 150,
padding: "30px 20px 10px 20px",
display: "flex",
flexDirection: "column",
}}
>
<Slider
min={5}
max={100}
value={strokeWidth}
onChange={setStrokeWidth}
/>
<div
style={{
flex: 1,
minHeight: 150,
justifyContent: "center",
flexDirection: "column",
display: "flex",
placeItems: "center",
}}
>
<div
style={{
width: strokeWidth,
height: strokeWidth,
backgroundColor: color,
borderRadius: strokeWidth,
}}
></div>
</div>
</div>
</Modal>
</label>
</div>
<div id="controls" className="toolbarSection">
<button onClick={undo} disabled={!canUndo}>
<FaUndo size={12} title="Undo" />
</button>
<button onClick={redo} disabled={!canRedo}>
<FaRedo title="Redo" />
</button>
<button onClick={() => artboardRef?.download()}>
<FaDownload title="Download" />
</button>
<button onClick={() => artboardRef?.clear()}>
<FaTrash title="Clear" />
</button>
</div>
<div id="artboard">
<Artboard
tool={tools[currentTool][0]}
ref={setArtboardRef}
history={history}
style={{ border: "1px gray solid" }}
/>
</div>
</main>
);
}