-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
85 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ export default bernankez({ | |
unocss: true, | ||
astro: true, | ||
formatters: true, | ||
vue: true, | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,42 @@ | ||
import { useBackmoji, useImageLoader, useImageRenderer } from "@backmoji/react"; | ||
import ReactLogo from "/react.svg?url"; | ||
import { useEffect, useRef } from "react"; | ||
import { useLatest } from "ahooks"; | ||
import { useResizeObserver } from "../hooks/useResizeObserver"; | ||
|
||
export function WithReact() { | ||
const canvasRef = useRef<HTMLCanvasElement>(null); | ||
|
||
const img = useImageLoader(ReactLogo); | ||
const renderer = useImageRenderer(img); | ||
const backmojiResult = useBackmoji(renderer, canvasRef, { | ||
height: 150, | ||
rowGap: 15, | ||
columnGap: 30, | ||
}); | ||
const { render, canvas, setSize } = useLatest(backmojiResult).current; | ||
const divRef = useResizeObserver<HTMLDivElement>((entries) => { | ||
for (const entry of entries) { | ||
const { width, height } = entry.contentRect; | ||
setSize?.(width, height); | ||
render?.(); | ||
} | ||
}); | ||
|
||
useEffect(() => { | ||
if (canvas) { | ||
setSize?.(divRef.current?.clientWidth, divRef.current?.clientHeight); | ||
render?.(); | ||
|
||
return () => { | ||
canvas.remove(); | ||
}; | ||
} | ||
}, [canvas]); | ||
|
||
return ( | ||
<div ref={divRef}> | ||
<canvas ref={canvasRef}></canvas> | ||
</div> | ||
); | ||
} |
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,33 @@ | ||
import { useLatest } from "ahooks"; | ||
import { useEffect, useRef, useState } from "react"; | ||
|
||
export function useResizeObserver<T extends HTMLElement>( | ||
callback: ResizeObserverCallback, | ||
options?: ResizeObserverOptions, | ||
): React.RefObject<T> { | ||
const ref = useRef<T>(null); | ||
const callbackRef = useLatest(callback); | ||
|
||
useEffect(() => { | ||
const { current } = ref; | ||
|
||
if (!current) { | ||
return; | ||
} | ||
|
||
const cb: ResizeObserverCallback = (entries, observer) => { | ||
return callbackRef.current(entries, observer); | ||
}; | ||
|
||
const observer = new ResizeObserver(cb); | ||
observer.observe(current, options); | ||
|
||
return () => { | ||
if (current) { | ||
observer.unobserve(current); | ||
} | ||
}; | ||
}, [options]); | ||
|
||
return ref; | ||
} |
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