mirror of
https://github.com/Doodle3D/Doodle3D-Core.git
synced 2024-11-13 01:37:55 +01:00
Add SketchDataToJSON
This commit is contained in:
parent
9bab3b3314
commit
fa97872848
59
src/shape/SketchDataToJSON.js
Normal file
59
src/shape/SketchDataToJSON.js
Normal 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
|
||||
};
|
||||
}
|
@ -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 };
|
||||
|
Loading…
Reference in New Issue
Block a user