generated from Pettor/template-web-component-react
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from Pettor/feature/save-image
feat: add save image button to drawer
- Loading branch information
Showing
13 changed files
with
114 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { ViewportExtended } from "~/components/library/editor/viewport/ViewportExtended"; | ||
|
||
export async function createImage(viewport: ViewportExtended): Promise<HTMLImageElement> { | ||
const app = viewport.app; | ||
|
||
const generatedImage = app.renderer.generateTexture(viewport, { | ||
region: app.screen, | ||
resolution: 2, | ||
}); | ||
|
||
const image = app.renderer.plugins.extract.image(generatedImage, "image/png", 1.0); | ||
generatedImage.destroy(); | ||
return image; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { ReactElement } from "react"; | ||
import { Provider } from "jotai"; | ||
|
||
export interface EditorContextProps { | ||
children: ReactElement; | ||
} | ||
|
||
export function EditorContext({ children }: EditorContextProps): ReactElement { | ||
return <Provider>{children}</Provider>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { useAtomValue } from "jotai"; | ||
import { useNavigate } from "react-router-dom"; | ||
import { viewportAtom } from "~/components/library/editor/atoms/viewport/ViewportAtoms"; | ||
import type { AppDrawerProps } from "~/components/library/editor/drawer/AppDrawer"; | ||
import { useThemeSwitcher } from "~/components/library/theme/UseThemeSwitcher"; | ||
import { createImage } from "~/libs/functions/CreateImage"; | ||
|
||
export function useEditorPage(): Omit<AppDrawerProps, "onClose" | "open"> { | ||
const navigate = useNavigate(); | ||
const themeSwitchProps = useThemeSwitcher(); | ||
const viewport = useAtomValue(viewportAtom); | ||
|
||
function handleOnNewImage(): void { | ||
navigate(`/`); | ||
} | ||
|
||
async function handleOnSaveImage(): Promise<void> { | ||
if (!viewport) { | ||
console.error("Viewport is not available"); | ||
return; | ||
} | ||
|
||
const imagesrc = await createImage(viewport); | ||
const createEl = document.createElement("a"); | ||
|
||
createEl.href = imagesrc.src; | ||
createEl.download = "image.png"; | ||
createEl.click(); | ||
createEl.remove(); | ||
} | ||
|
||
return { | ||
onNewImage: handleOnNewImage, | ||
onSaveImage: handleOnSaveImage, | ||
themeSwitchProps, | ||
}; | ||
} |