@meta2d/core
Version:
@meta2d/core: Powerful, Beautiful, Simple, Open - Web-Based 2D At Its Best .
112 lines • 3.49 kB
JavaScript
import { getMeta2dData, s8, debounce } from '../utils';
export function sceneContainer(pen, ctx) {
if (!pen.onDestroy) {
pen.onAdd = add;
pen.onDestroy = destory;
pen.onMove = move;
pen.onBeforeValue = beforeValue;
pen.onMouseDown = onMousedown;
pen.onMouseUp = onMouseUp;
}
const path = !ctx ? new Path2D() : ctx;
if (path instanceof Path2D)
return path;
}
async function add(pen) {
if (!pen.sceneId) {
pen.sceneId = s8();
}
}
const getContainerPens = async (pen, vId) => {
const store = pen.calculative.canvas.store;
const origin = store.data.origin;
const scale = store.data.scale;
const _vId = vId || pen.vId;
const data = await getMeta2dData(store, _vId);
if (data && data.pens?.length) {
const c_scale = data.scale;
const c_origin = data.origin;
data.pens.forEach((cpen) => {
if (!cpen.parentId) {
cpen.width = cpen.width / c_scale;
cpen.height = cpen.height / c_scale;
cpen.x = (cpen.x - c_origin.x) / c_scale + (pen.x - origin.x) / scale;
cpen.y = (cpen.y - c_origin.y) / c_scale + (pen.y - origin.y) / scale;
}
if (!cpen.p_sceneId) {
cpen.p_sceneId = pen.sceneId;
if (cpen.name !== 'sceneContainer') {
cpen.p_vId = _vId;
}
}
});
const meta2d = pen.calculative.canvas.parent;
meta2d.addPens(data.pens, false, true);
}
moveToLast(pen);
};
const deleteContainerPens = (pen) => {
const meta2d = pen.calculative.canvas.parent;
let pens = collectContainerPens(pen);
pens.forEach((cpen) => {
meta2d.canvas.delForce(cpen);
});
meta2d.canvas.canvasTemplate.init();
meta2d.canvas.canvasImage.init();
meta2d.canvas.canvasImageBottom.init();
meta2d.render();
};
const collectContainerPens = (pen) => {
let pens = [];
const meta2d = pen.calculative.canvas.parent;
meta2d.store.data.pens.forEach((cpen) => {
if (cpen.p_sceneId &&
pen.sceneId &&
cpen.p_sceneId === pen.sceneId &&
cpen.id !== pen.id) {
cpen.calculative.locked = 0;
cpen.locked = 0;
pens.push(cpen);
}
});
return pens;
};
const translateContainerPens = async (pen) => {
if (pen.calculative.drag && pen.calculative.active && !(pen.locked > 1)) {
pen.calculative.drag = false;
deleteContainerPens(pen);
getContainerPens(pen);
}
};
function destory(pen) {
deleteContainerPens(pen);
}
function beforeValue(pen, value) {
if (value.vId) {
deleteContainerPens(pen);
getContainerPens(pen, value.vId);
}
if (value.x !== undefined || value.y !== undefined) {
deleteContainerPens(pen);
getContainerPens(pen);
}
return value;
}
function move(pen) {
debounce(translateContainerPens, 500, pen);
}
function onMousedown(pen, e) {
pen.calculative.drag = true;
}
function onMouseUp(pen, e) {
pen.calculative.drag = false;
}
function moveToLast(pen) {
let array = pen.calculative.canvas.store.data.pens;
let index = array.findIndex((item) => item.id === pen.id);
if (index < 0 || index >= array.length)
return array;
const element = array.splice(index, 1)[0];
array.push(element);
}
//# sourceMappingURL=sceneContainer.js.map