Add SketchDataToJSON

This commit is contained in:
casperlamboo 2017-11-06 13:52:17 +01:00
parent 9bab3b3314
commit fa97872848
2 changed files with 61 additions and 1 deletions

View File

@ -0,0 +1,59 @@
import { vectorArrayToBase64, imageToBase64 } from '../utils/binaryUtils.js';
export function sketchDataToJSON({ objectsById, spaces }) {
const data = {
spaces: []
};
function createShapeData(id) {
const shape = { ...objectsById[id] };
delete shape.UID;
delete shape.space;
switch (shape.type) {
case 'POLYGON':
case 'BRUSH':
case 'FREE_HAND': {
shape.points = vectorArrayToBase64(shape.points);
break;
}
case 'COMPOUND_PATH': {
shape.points = vectorArrayToBase64(shape.points);
shape.holes = shape.holes.map(vectorArrayToBase64);
break;
}
case 'IMAGE_GUIDE': {
shape.imageData = imageToBase64(shape.imageData);
break;
}
default: {
break;
}
}
return shape;
}
for (const spaceId in spaces) {
const space = {
matrix: {
metadata: { type: 'Matrix4', library: 'three.js' },
elements: Array.from(spaces[spaceId].matrix.elements)
},
objects: spaces[spaceId].objectIds.map(createShapeData)
};
if (spaceId === 'world') {
data.spaces.unshift(space);
} else {
data.spaces.push(space);
}
}
return {
data: JSON.stringify(data),
appVersion: VERSION
};
}

View File

@ -1,6 +1,7 @@
import docToFile from './docToFile.js';
import * as shapeToPoints from './shapeToPoints.js';
import JSONToSketchData from './JSONToSketchData.js';
import SketchDataToJSON from './SketchDataToJSON.js';
import * as shapeDataUtils from './shapeDataUtils.js';
export { docToFile, shapeToPoints, JSONToSketchData, shapeDataUtils };
export { docToFile, shapeToPoints, JSONToSketchData, SketchDataToJSON, shapeDataUtils };