move generate thumb

This commit is contained in:
casperlamboo 2017-11-23 14:36:38 +01:00
parent f5ba53e533
commit 655eaa0e65
2 changed files with 33 additions and 2 deletions

View File

@ -1,4 +1,2 @@
export const THUMBNAIL_WIDTH = 240 * 2; // multiply times 2 because retina
export const THUMBNAIL_HEIGHT = 200 * 2;
export const IMAGE_TYPE = 'image/jpeg'; export const IMAGE_TYPE = 'image/jpeg';
export const IMAGE_QUALITY = 1; // 0 - 1 export const IMAGE_QUALITY = 1; // 0 - 1

View File

@ -10,6 +10,8 @@ import { shapeToPoints } from '../shape/shapeToPoints.js';
import { SHAPE_TYPE_PROPERTIES } from '../constants/shapeTypeProperties.js'; import { SHAPE_TYPE_PROPERTIES } from '../constants/shapeTypeProperties.js';
import { LINE_WIDTH } from '../constants/exportConstants.js'; import { LINE_WIDTH } from '../constants/exportConstants.js';
import { bufferToBase64 } from '../utils/binaryUtils.js'; import { bufferToBase64 } from '../utils/binaryUtils.js';
import { IMAGE_TYPE, IMAGE_QUALIT } from 'doodle3d-core/constants/saveConstants.js';
import createScene from '../d3/createScene.js';
const THREE_BSP = ThreeBSP(THREE); const THREE_BSP = ThreeBSP(THREE);
@ -165,3 +167,34 @@ export async function createFile(state, type, options) {
throw new Error(`did not regonize type ${type}`); throw new Error(`did not regonize type ${type}`);
} }
} }
export function generateThumb(state, responseType = 'blob', width = THUMBNAIL_WIDTH, height = THUMBNAIL_HEIGHT) {
return new Promise((resolve) => {
const { render, renderer, setSize } = createScene(state);
setSize(width, height, 1.0);
render();
// possible to add encoder options for smaller file setSize
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob
switch (responseType) {
case 'base64':
const base64 = renderer.domElement.toDataURL(IMAGE_TYPE, IMAGE_QUALITY);
resolve(base64);
break;
case 'objectURL':
renderer.domElement.toCanvas((blob) => {
const objectURL = URL.createObjectURL(blob);
resolve(objectURL);
}, IMAGE_TYPE, IMAGE_QUALITY);
break;
default:
renderer.domElement.toBlob((blob) => {
resolve(blob);
}, IMAGE_TYPE, IMAGE_QUALITY);
break;
}
});
}