mirror of
https://github.com/aljazceru/rabbit.git
synced 2025-12-18 14:34:25 +01:00
initial
This commit is contained in:
52
src/hooks/useResizedImage.ts
Normal file
52
src/hooks/useResizedImage.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import type { Accessor } from 'solid-js';
|
||||
import { createSignal, createEffect } from 'solid-js';
|
||||
|
||||
export type UseResizedImageProps = {
|
||||
imageUrl: Accessor<string | undefined>;
|
||||
width: number;
|
||||
height: number;
|
||||
encoderOption?: number;
|
||||
};
|
||||
|
||||
const useResizedImage = ({
|
||||
imageUrl,
|
||||
width,
|
||||
height,
|
||||
encoderOption,
|
||||
}: UseResizedImageProps): Accessor<string | undefined> => {
|
||||
const [resizedImage, setResizedImage] = createSignal<string | undefined>(undefined);
|
||||
|
||||
createEffect(() => {
|
||||
const sourceUrl = imageUrl();
|
||||
|
||||
if (sourceUrl == null) return;
|
||||
|
||||
const img = document.createElement('img');
|
||||
img.addEventListener('load', () => {
|
||||
const ratio = img.naturalWidth / img.naturalHeight;
|
||||
const rw = height * ratio;
|
||||
const rh = width / ratio;
|
||||
const dw = Math.min(rw, width);
|
||||
const dh = Math.min(rh, height);
|
||||
|
||||
const canvas = document.createElement('canvas');
|
||||
canvas.height = dh;
|
||||
canvas.width = dw;
|
||||
|
||||
const ctx = canvas.getContext('2d');
|
||||
|
||||
if (ctx == null) throw new Error('failed to obtain context');
|
||||
|
||||
ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, dw, dh);
|
||||
|
||||
const dataUrl = canvas.toDataURL('image/jpeg', encoderOption);
|
||||
|
||||
setResizedImage(dataUrl);
|
||||
});
|
||||
img.src = sourceUrl;
|
||||
});
|
||||
|
||||
return resizedImage;
|
||||
};
|
||||
|
||||
export default useResizedImage;
|
||||
Reference in New Issue
Block a user