only create floor when physics is activated and account for viewport position/scaling
This commit is contained in:
parent
badd9bc013
commit
cb02547248
|
|
@ -19,25 +19,18 @@ export function useCanvas() {
|
||||||
const info = await gatherElementsInfo();
|
const info = await gatherElementsInfo();
|
||||||
setElementsInfo(info);
|
setElementsInfo(info);
|
||||||
setIsCanvasEnabled(true);
|
setIsCanvasEnabled(true);
|
||||||
document.getElementById('toggle-physics')?.classList.remove('hidden');
|
document.body.classList.add('canvas-mode');
|
||||||
} else {
|
} else {
|
||||||
setElementsInfo([]);
|
setElementsInfo([]);
|
||||||
setIsCanvasEnabled(false);
|
setIsCanvasEnabled(false);
|
||||||
document.getElementById('toggle-physics')?.classList.add('hidden');
|
document.body.classList.remove('canvas-mode');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// const enableCanvas = async () => {
|
|
||||||
// const info = await gatherElementsInfo();
|
|
||||||
// setElementsInfo(info);
|
|
||||||
// setIsCanvasEnabled(true);
|
|
||||||
// };
|
|
||||||
|
|
||||||
window.addEventListener('toggleCanvasEvent', toggleCanvas);
|
window.addEventListener('toggleCanvasEvent', toggleCanvas);
|
||||||
// window.addEventListener('togglePhysicsEvent', enableCanvas);
|
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
window.removeEventListener('toggleCanvasEvent', toggleCanvas);
|
window.removeEventListener('toggleCanvasEvent', toggleCanvas);
|
||||||
// window.removeEventListener('togglePhysicsEvent', enableCanvas);
|
|
||||||
};
|
};
|
||||||
}, [isCanvasEnabled]);
|
}, [isCanvasEnabled]);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { TLUnknownShape, useEditor } from "@tldraw/tldraw";
|
import { Editor, TLUnknownShape, createShapeId, useEditor } from "@tldraw/tldraw";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { usePhysicsSimulation } from "./simulation";
|
import { usePhysicsSimulation } from "./simulation";
|
||||||
|
|
||||||
|
|
@ -16,13 +16,11 @@ export const SimController = ({ shapes }: { shapes: TLUnknownShape[] }) => {
|
||||||
const togglePhysics = () => {
|
const togglePhysics = () => {
|
||||||
setIsPhysicsActive((currentIsPhysicsActive) => {
|
setIsPhysicsActive((currentIsPhysicsActive) => {
|
||||||
if (currentIsPhysicsActive) {
|
if (currentIsPhysicsActive) {
|
||||||
console.log('destroy');
|
|
||||||
destroy();
|
destroy();
|
||||||
return false;
|
return false;
|
||||||
} else {
|
|
||||||
console.log('activate');
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
createFloor(editor);
|
||||||
|
return true;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -36,8 +34,6 @@ export const SimController = ({ shapes }: { shapes: TLUnknownShape[] }) => {
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isPhysicsActive) {
|
if (isPhysicsActive) {
|
||||||
console.log('adding shapes');
|
|
||||||
|
|
||||||
addShapes(editor.getCurrentPageShapes()); // Activate physics simulation
|
addShapes(editor.getCurrentPageShapes()); // Activate physics simulation
|
||||||
} else {
|
} else {
|
||||||
destroy(); // Deactivate physics simulation
|
destroy(); // Deactivate physics simulation
|
||||||
|
|
@ -46,3 +42,25 @@ export const SimController = ({ shapes }: { shapes: TLUnknownShape[] }) => {
|
||||||
|
|
||||||
return (<></>);
|
return (<></>);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function createFloor(editor: Editor) {
|
||||||
|
|
||||||
|
const viewBounds = editor.getViewportPageBounds();
|
||||||
|
|
||||||
|
editor.createShape({
|
||||||
|
id: createShapeId(),
|
||||||
|
type: 'geo',
|
||||||
|
x: viewBounds.minX,
|
||||||
|
y: viewBounds.maxY,
|
||||||
|
props: {
|
||||||
|
w: viewBounds.width,
|
||||||
|
h: 50,
|
||||||
|
color: 'grey',
|
||||||
|
fill: 'solid'
|
||||||
|
},
|
||||||
|
meta: {
|
||||||
|
fixed: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,24 +12,5 @@ export function createShapes(elementsInfo: any) {
|
||||||
html: element.html,
|
html: element.html,
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const extend = 150;
|
|
||||||
|
|
||||||
shapes.push({
|
|
||||||
id: createShapeId(),
|
|
||||||
type: 'geo',
|
|
||||||
x: -extend,
|
|
||||||
y: window.innerHeight,
|
|
||||||
props: {
|
|
||||||
w: window.innerWidth + (extend * 2),
|
|
||||||
h: 50,
|
|
||||||
color: 'grey',
|
|
||||||
fill: 'solid'
|
|
||||||
},
|
|
||||||
meta: {
|
|
||||||
fixed: true
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return shapes;
|
return shapes;
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue