Skip to content

Commit 6f3642a

Browse files
committed
chore: canvas eraser
1 parent be9c2dd commit 6f3642a

File tree

7 files changed

+59
-15
lines changed

7 files changed

+59
-15
lines changed

src/components/OmbCard/index.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
> div {
77
cursor: pointer;
88
user-select: none;
9+
-webkit-user-select: none;
910
}
1011
}
1112
}

src/views/canvas/paper/canvas.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ export class Brush {
5555
this.ctx?.clearRect(0, 0, this.canvas?.width || 0, this.canvas?.height || 0);
5656
}
5757

58-
eraser = () => {
58+
eraser = (isEraser: boolean) => {
5959
if (!this.ctx) return;
60-
this.ctx.globalCompositeOperation = 'destination-out';
60+
this.ctx.globalCompositeOperation = isEraser ? 'destination-out' : 'source-over';
6161
}
6262

6363
run = (method: string, value: any) => {

src/views/canvas/paper/components/ColorPicker/index.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
border: solid 1px #e8eaed;
5959
cursor: pointer;
6060
user-select: none;
61+
-webkit-user-select: none;
6162
transition: 100ms ease;
6263
}
6364

src/views/canvas/paper/components/ToolPalette/index.scss

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
border-top-right-radius: 8px;
1111
border-bottom-right-radius: 8px;
1212
cursor: auto;
13+
user-select: none;
14+
-webkit-user-select: none;
1315

1416
.color-btn {
1517
width: 30px;
@@ -36,7 +38,20 @@
3638
}
3739

3840
.eraser-btn {
39-
width: 100px;
41+
transform: translateX(-40px);
42+
transition: transform 300ms ease-in-out;
43+
filter: grayscale(1);
44+
padding: 3px 0;
45+
46+
svg {
47+
width: 100px;
48+
height: 32px;
49+
}
50+
51+
&.active {
52+
transform: translateX(-10px);
53+
filter: grayscale(0);
54+
}
4055
}
4156
}
4257

src/views/canvas/paper/components/ToolPalette/index.tsx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useEffect, useState } from 'react';
2+
import clsx from 'clsx';
23

34
import useI18n from '@/hooks/useI18n';
45
import { setCSS } from '@/utils/color';
@@ -11,19 +12,35 @@ import ColorPicker from '../ColorPicker';
1112
import './index.scss';
1213

1314
interface ToolPaletteProps {
15+
onSave: () => void;
16+
onEraser: (isEraser: boolean) => void;
1417
onChange: (key: string, value?: any) => void;
1518
}
1619

17-
const ToolPalette: React.FC<ToolPaletteProps> = ({ onChange }) => {
20+
const ToolPalette: React.FC<ToolPaletteProps> = ({
21+
onChange,
22+
onSave,
23+
onEraser,
24+
}) => {
1825
const t = useI18n(['common']);
1926
const [color, setColor] = useState('#000000');
2027
const [size, setSize] = useState(1);
2128
const [opacity, setOpacity] = useState(100);
29+
const [isEraser, setEraser] = useState(false);
2230

2331
const handleChange = (key: string, value?: any) => {
2432
onChange && onChange(key, value);
2533
};
2634

35+
const handleEraser = () => {
36+
setEraser(!isEraser);
37+
onEraser && onEraser(!isEraser);
38+
};
39+
40+
const handleSave = () => {
41+
onSave && onSave();
42+
};
43+
2744
const handleColor = (c: string) => {
2845
setColor(c);
2946
handleChange('color', c);
@@ -105,10 +122,13 @@ const ToolPalette: React.FC<ToolPaletteProps> = ({ onChange }) => {
105122
</button>
106123
</Popover>
107124
<div className="omb-tool-line" />
108-
<div className="eraser-btn" onClick={() => handleChange('eraser')}>
125+
<div
126+
className={clsx('eraser-btn', { active: isEraser })}
127+
onClick={handleEraser}
128+
>
109129
<EraserIcon />
110130
</div>
111-
<button onClick={() => handleChange('save')}>save</button>
131+
<button onClick={handleSave}>save</button>
112132
</div>
113133
);
114134
};

src/views/canvas/paper/index.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
.canvas_paper_screen {
22
height: 100vh;
33
overflow: hidden;
4+
user-select: none;
5+
-webkit-user-select: none;
46

57
.omb-canvas-paper {
68
cursor: url('./icons/cursor.svg') 3 3, auto;

src/views/canvas/paper/index.tsx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,26 @@ export default function CanvasPaperView(props: any) {
3030
}, []);
3131

3232
const handleToolbar = (key: string, val: any) => {
33-
if (key === 'save') {
34-
const image = InitBrush.save();
35-
saveCanvas(filePath, image);
36-
return;
37-
}
38-
if (key === 'eraser') {
39-
InitBrush.eraser();
40-
}
4133
InitBrush.run(key, val);
4234
};
4335

36+
const handleSave = () => {
37+
const image = InitBrush.save();
38+
saveCanvas(filePath, image);
39+
};
40+
41+
const handleEraser = (isEraser: boolean) => {
42+
InitBrush.eraser(isEraser);
43+
};
44+
4445
return (
4546
<div className="omb-canvas-paper">
4647
<GoBack />
47-
<ToolPalette onChange={handleToolbar} />
48+
<ToolPalette
49+
onChange={handleToolbar}
50+
onEraser={handleEraser}
51+
onSave={handleSave}
52+
/>
4853
<canvas ref={canvasRef} className="select-none" />
4954
</div>
5055
);

0 commit comments

Comments
 (0)