Skip to content

Instantly share code, notes, and snippets.

@ShaMan123
Last active March 20, 2022 11:03
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ShaMan123/6c5c4ca2cc720a2700848a2deb6addcd to your computer and use it in GitHub Desktop.
function migrateObject(obj) {
const clipPath = obj?.clipPath;
if (clipPath && clipPath.type === 'group' && clipPath.eraser === true) {
clipPath.type = 'eraser';
delete clipPath.eraser;
const rect = clipPath.objects.shift();
obj.clipPath = rect.clipPath;
obj.eraser = clipPath;
}
}
function traverseTree(target) {
target.objects?.forEach(obj => {
migrateObject(obj);
migrateObject(obj.clipPath);
traverseTree(obj);
});
}
/**
* call this method
* @param {Object} canvasJSON
* @returns {Object} version 2 compatible json
*/
function migrate(canvasJSON) {
traverseTree(canvasJSON);
['backgroundImage', 'overlayImage'].forEach(key => {
if (canvasJSON[key]) {
migrateObject(canvasJSON[key]);
traverseTree(canvasJSON[key]);
}
});
return canvasJSON;
}
@ShaMan123
Copy link
Author

ShaMan123 commented Jan 14, 2022

Call migrate with the json payload.
It returns a migrated payload.
It's safe to run on v2 payload if you're not sure if your payload is up to date or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment