File tree Expand file tree Collapse file tree 3 files changed +76
-0
lines changed
Expand file tree Collapse file tree 3 files changed +76
-0
lines changed Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import CameraOpenButton from "@/components/CameraOpenButton";
66import ImageCanvas from '@/components/ImageCanvas' ;
77import ImageSelectButton from '@/components/ImageSelectButton' ;
88import ShareButton from '@/components/ShareButton' ;
9+ import SaveButton from '@/components/SaveButton' ;
910import { useHomePage } from '@/pages/HomePage/useHomePage' ;
1011
1112const 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
You can’t perform that action at this time.
0 commit comments