-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.tsx
163 lines (143 loc) · 3.83 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
import { h } from "preact"
import { useRef, useCallback } from "preact/hooks"
import { Sticker, Stickerbook } from "./lib"
import { addSticker, exportStickerbook } from "./lib/helpers"
import { useStickerbook } from "./lib/use-stickerbook"
import backgroundImage from "./background.jpg"
import backgroundImage2 from "./background-2.png"
import frameImage from "./frame.png"
import foregroundImage from "./foreground.png"
import foregroundImage2 from "./foreground-2.png"
import stickerImage from "./sticker.png"
const CANVAS_SIZE = {
width: 500,
height: 500,
}
const GIPHY_API_URL = new URL("https://api.giphy.com/v1/stickers/random")
GIPHY_API_URL.search = new URLSearchParams({
api_key: "a8s5d1Dw5bitDbTPHgVHVfaYv0cRAQf8",
}).toString()
export function App() {
const {
stickers,
setStickers,
backgrounds,
foregrounds,
frames,
onReorderSticker,
onDeleteSticker,
onPositionSticker,
onScaleSticker,
onRotateSticker,
} = useStickerbook({
initialStickers: [
{
id: "my-id-1",
image: stickerImage,
order: 0,
},
],
initialFrames: [
{
image: frameImage,
},
],
initialBackgrounds: [
{
image: backgroundImage,
type: "scene",
},
{
image: backgroundImage2,
type: "scene",
},
],
initialForegrounds: [
{
image: foregroundImage,
},
{
image: foregroundImage2,
},
],
})
const downloadRef = useRef<HTMLAnchorElement | null>(null)
const onAddSticker = useCallback(async () => {
try {
const res = await fetch(GIPHY_API_URL).then((res) => res.json())
const {
data: {
images: {
preview_gif: { url },
},
caption,
},
} = res
setStickers((stickers) =>
addSticker({ stickers, sticker: { image: url, alt: caption } })
)
} catch (err) {
console.error(err)
}
}, [setStickers])
// Download
const onClickDownload = async (e: Event) => {
e.preventDefault()
const downloadLink = downloadRef.current
if (!downloadLink) return
const newUrl = await exportStickerbook<"image">({
outputWidth: CANVAS_SIZE.width,
outputHeight: CANVAS_SIZE.height,
stickers,
backgrounds,
frames,
foregrounds,
format: "image",
})
downloadLink.href = newUrl
downloadLink.click()
}
return (
<>
<button onClick={onAddSticker}>Add random sticker from GIPHY</button>
<button onClick={onClickDownload}>Download</button>
<a ref={downloadRef} hidden={true} href="#" download="Stickerbook.png" />
<div
style={Object.entries(CANVAS_SIZE).reduce((acc, [key, val]) => {
return { ...acc, [`max-${key}`]: `${val}px` }
}, {})}
>
<Stickerbook
outputWidth={CANVAS_SIZE.width}
outputHeight={CANVAS_SIZE.height}
backgrounds={backgrounds}
frames={frames}
foregrounds={foregrounds}
>
{stickers.map((sticker) => (
<Sticker
key={sticker.id}
initialPosition={sticker.position}
initialRotation={sticker.rotation}
initialScale={sticker.scale}
onReorder={onReorderSticker}
onDelete={onDeleteSticker}
onPosition={onPositionSticker}
onScale={onScaleSticker}
onRotate={onRotateSticker}
disableRotation={sticker.disableRotation}
{...sticker}
/>
))}
</Stickerbook>
</div>
<a href="https://giphy.com/" target="_blank" rel="noreferrer">
<img
src="https://media.giphy.com/media/3o6gbbuLW76jkt8vIc/giphy.gif"
alt="Powered By GIPHY"
width="100"
/>
</a>
</>
)
}