Skip to content

Commit 381973c

Browse files
committed
test save button
1 parent 6e8362f commit 381973c

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Button, Box, SxProps, Theme } from '@mui/material';
2+
import SaveIcon from '@mui/icons-material/Save';
3+
import { useSaveButton } from '@/components/SaveButton/useSaveButton';
4+
5+
type Props = {
6+
text: string;
7+
canvas?: HTMLCanvasElement;
8+
sx?: SxProps<Theme>;
9+
};
10+
11+
const SaveButton = ({ text, canvas, sx }: Props) => {
12+
const { onButtonClick } = useSaveButton(canvas);
13+
14+
return (
15+
<Box sx={{ width: "100%" }}>
16+
<Button
17+
variant="outlined"
18+
color="primary"
19+
startIcon={<SaveIcon />}
20+
onClick={onButtonClick}
21+
sx={{
22+
textTransform: 'none',
23+
borderRadius: 0,
24+
boxShadow: 0,
25+
padding: '8px 16px',
26+
fontFamily: 'Instrument Serif',
27+
fontWeight: 400,
28+
letterSpacing: '2px',
29+
width: "100%",
30+
...sx,
31+
}}
32+
>
33+
{text}
34+
</Button>
35+
</Box>
36+
);
37+
};
38+
39+
export default SaveButton;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export const useSaveButton = (canvas?: HTMLCanvasElement) => {
2+
const onButtonClick = async () => {
3+
if (canvas) {
4+
try {
5+
// Convert canvas to blob
6+
const blob = await new Promise<Blob>((resolve) => {
7+
canvas.toBlob((blob) => { if (blob) resolve(blob); }, 'image/png');
8+
});
9+
10+
// Create a download link
11+
const url = URL.createObjectURL(blob);
12+
const link = document.createElement('a');
13+
link.href = url;
14+
link.download = 'hitbox.png';
15+
16+
// Trigger download
17+
document.body.appendChild(link);
18+
link.click();
19+
20+
// Cleanup
21+
document.body.removeChild(link);
22+
URL.revokeObjectURL(url);
23+
} catch (error) {
24+
console.error('Failed to save image:', error);
25+
alert('画像の保存に失敗しました。');
26+
}
27+
}
28+
};
29+
30+
return {
31+
onButtonClick,
32+
};
33+
};

src/pages/HomePage/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import CameraOpenButton from "@/components/CameraOpenButton";
66
import ImageCanvas from '@/components/ImageCanvas';
77
import ImageSelectButton from '@/components/ImageSelectButton';
88
import ShareButton from '@/components/ShareButton';
9+
import SaveButton from '@/components/SaveButton';
910
import { useHomePage } from '@/pages/HomePage/useHomePage';
1011

1112
const HomePage = () => {
@@ -33,6 +34,9 @@ const HomePage = () => {
3334
<Box sx={{ display: "flex", gap: 2 }}>
3435
<ShareButton text="Share" canvas={canvas}/>
3536
</Box>
37+
<Box sx={{ display: "flex", gap: 2 }}>
38+
<SaveButton text="Save" canvas={canvas}/>
39+
</Box>
3640
</Box>
3741
</Box>
3842

0 commit comments

Comments
 (0)