I was just wondering if using this type of constraint would work when using the Physic options.
One of the problems with physics is setting boundaries to stop the object falling out of the scene.
Another is when you have drag set on the object also. You can drag it out of the scene.
So all we need to do is listen for any changes to the element and run the constraining code; for example.
We have a container set up and a drag object.
We change the constraint/boundary function to :
var element = hypeDocument.getElementById('drag')
var vContainer = hypeDocument.getElementById('container');
//use this line for left constrain
if (hypeDocument.getElementProperty(element, 'left') < hypeDocument.getElementProperty(vContainer, 'left')) {
hypeDocument.setElementProperty(element, 'left', hypeDocument.getElementProperty(vContainer, 'left'), 0, 'linear');
};
//use this line for right constrain
if((hypeDocument.getElementProperty(element, 'left') + hypeDocument.getElementProperty(element, 'width')) > (hypeDocument.getElementProperty(vContainer, 'left') + hypeDocument.getElementProperty(vContainer, 'width'))){
hypeDocument.setElementProperty(element, 'left', (hypeDocument.getElementProperty(vContainer, 'left')+hypeDocument.getElementProperty(vContainer, 'width')-hypeDocument.getElementProperty(element, 'width')), 0, 'linear');
};
//use this line for top constrain
if(hypeDocument.getElementProperty(element, 'top') < hypeDocument.getElementProperty(vContainer, 'top')){
hypeDocument.setElementProperty(element, 'top', hypeDocument.getElementProperty(vContainer, 'top'), 0, 'linear');
};
//use this line for bottom constrain
if((hypeDocument.getElementProperty(element, 'top') + hypeDocument.getElementProperty(element, 'height')) > (hypeDocument.getElementProperty(vContainer, 'top') + hypeDocument.getElementProperty(vContainer, 'height'))){
hypeDocument.setElementProperty(element, 'top', (hypeDocument.getElementProperty(vContainer, 'top')+hypeDocument.getElementProperty(vContainer, 'height')-hypeDocument.getElementProperty(element, 'height')), 0, 'linear');
};
And do not call it on the ondrag action. The on drag action is only set to Control the elements position
Now we set the drag element to have full Physic dynamics.
We then add a new function that runs on scene load.
The function sets up an Mutation Observer , MutationObserver will watch for changes in the drag elements ‘style’ ( we can limit to only detect changes to style in the observer )
// create an observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
hypeDocument.functions().constrain(hypeDocument, element, event)
});
});
// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true ,attributeFilter:['style'] };
// pass in the target node, as well as the observer options
observer.observe(drag, config);
// later, you can stop observing
//observer.disconnect();
We do not need to know what the change is. The MutationObserver will run a callback on any change.
So all we do is get it to run the constraints function.
The effect is you will have a boundary that the element with Physic cannot break out of and you cannot drag out of, even with physics.
I have not had a massive play with this but in my own brief observations the are some caveats that would need to be addressed. Like bounce. This gets broken I think since we are making the element adhere
to the boundary when it tries to go past it.
Drag_&_Physics_constrains.hypetemplate.zip (16.4 KB)