formatting and helpers

This commit is contained in:
Kurt Hutten
2021-08-13 06:38:15 +10:00
parent 7bd3cb44f8
commit 50e9ac61f8
5 changed files with 70 additions and 27 deletions

View File

@@ -0,0 +1,35 @@
export const canvasToBlob = async (
threeInstance,
{ width, height }: { width: number; height: number }
): Promise<Blob> => {
const updateCanvasSize = ({
width,
height,
}: {
width: number
height: number
}) => {
threeInstance.camera.aspect = width / height
threeInstance.camera.updateProjectionMatrix()
threeInstance.gl.setSize(width, height)
threeInstance.gl.render(
threeInstance.scene,
threeInstance.camera,
null,
false
)
}
const oldSize = threeInstance.size
updateCanvasSize({ width, height })
const imgBlobPromise: Promise<Blob> = new Promise((resolve, reject) => {
threeInstance.gl.domElement.toBlob(
(blob) => {
resolve(blob)
},
'image/jpeg',
1
)
})
updateCanvasSize(oldSize)
return imgBlobPromise
}