Forum Discussion
Nathan_Hilliard
2 years agoCommunity Member
Drag and Drop Cables Simulation
Update: A recent SL update changed something with how the screen position was calculated. If the original project file shows misaligned cables, try the updated version also included below.
Thi...
Snorre
15 days agoCommunity Member
I made a simplified version, that has straight lines instead of cables.
It just requires a js trigger that runs on timeline start, on the slide that has draggables. Not adjustments needed:
const current = {down: false}
const svgNS = "http://www.w3.org/2000/svg";
const draggables = Array.from(document.querySelectorAll('.draggable'));
const origSvg = document.querySelector('.slide').querySelector('svg');
draggables.forEach((el) => {
let dmId = el.dataset.modelId;
let obj = object(dmId);
console.log(obj);
obj.line_startX = parseInt(obj.x)+parseInt(obj.centerX)
obj.line_startY = parseInt(obj.y)+parseInt(obj.centerY)
obj.line = document.createElementNS(svgNS,'line');
obj.line.id = `line_for_drag_${dmId}`;
obj.line.setAttribute('x1', obj.line_startX)
obj.line.setAttribute('x2', obj.line_startX);
obj.line.setAttribute('y1', obj.line_startY);
obj.line.setAttribute('y2', obj.line_startY);
obj.line.setAttribute('stroke', el.querySelector('path').getAttribute('fill'));
obj.line.setAttribute('stroke-width', 5);
el.addEventListener('mousedown', (e)=>{
let drag = e.target.closest('.draggable')
current.down = true;
current.obj = object(drag.dataset.modelId);
console.log(obj)
current.obj.dragging = true;
current.origPointerX = pointerX();
current.origPointerY = pointerY();
current.origX2 = parseInt(current.obj.line.getAttribute('x2'))
current.origY2 = parseInt(current.obj.line.getAttribute('y2'));
if(!origSvg.contains(current.obj.line)) {
origSvg.appendChild(current.obj.line)
};
requestAnimationFrame(setLineEnd)
});
el.addEventListener('mouseup', (e)=>{
current.down = false;
setTimeout(()=>{
current.obj.line.setAttribute('x2', parseInt(current.obj.x)+parseInt(current.obj.centerX));
current.obj.line.setAttribute('y2', parseInt(current.obj.y)+parseInt(current.obj.centerY));
},5)
});
})
function setLineEnd() {
if(!current.down) {return;} else {
current.obj.line.setAttribute('x2', current.origX2+(pointerX()-current.origPointerX));
current.obj.line.setAttribute('y2', current.origY2+(pointerY()-current.origPointerY));
requestAnimationFrame(setLineEnd)
}
}