model-viewer and cursor moverment

922 views Asked by At

I am using model-viewer for a 3D-Model and I want to move the 3D-Model according to cursor movement. I am not sure how I can connect the HTML variable in attribute "camera-controls" so that I can change them in js. So far I have written this in HTML:

<div class="hero">
 <model-viewer src="{% static "images/file_name.glb" %}" alt="3d model"
        loading="eager" camera-controls disable-zoom 
        reveal="auto" auto-rotate rotation-per-second="60deg"
        id="model">
 </model-viewer>
</div>
2

There are 2 answers

0
Elina_syr On BEST ANSWER

I managed to solve it with the following code:

const modelViewer = document.getElementById('model'); 

document.onmousemove = handleMouseMove;
    function handleMouseMove(event) {
        var eventDoc, doc, body;

        event = event || window.event; // IE-ism

        // If pageX/Y aren't available and clientX/Y are,
        // calculate pageX/Y - logic taken from jQuery.
        // (This is to support old IE)
        if (event.pageX == null && event.clientX != null) {
            eventDoc = (event.target && event.target.ownerDocument) || document;
            doc = eventDoc.documentElement;
            body = eventDoc.body;

            event.pageX = event.clientX +
              (doc && doc.scrollLeft || body && body.scrollLeft || 0) -
              (doc && doc.clientLeft || body && body.clientLeft || 0);
            event.pageY = event.clientY +
              (doc && doc.scrollTop  || body && body.scrollTop  || 0) -
              (doc && doc.clientTop  || body && body.clientTop  || 0 );
        }

        // Use event.pageX / event.pageY here
        x = - (event.pageX / window.innerWidth * 100 - 50)
        if(media_change.matches){
          x = - (event.pageX / window.innerWidth * 100 - 30)
        }
        y = - (event.pageY / window.innerHeight * 50) + 120
    }
1
user23020435 On
const modelViewer = $('#modelViewer');

console.log('modelViewer',modelViewer);

document.onmousemove = handleMouseMove;

function handleMouseMove(event) {
    var eventDoc, doc, body;

    event = event || window.event; // IE-ism

    // If pageX/Y aren't available and clientX/Y are,
    // calculate pageX/Y - logic taken from jQuery.
    // (This is to support old IE)
    if (event.pageX == null && event.clientX != null) {
        eventDoc = (event.target && event.target.ownerDocument) || document;
        doc = eventDoc.documentElement;
        body = eventDoc.body;
        event.pageX = event.clientX +
          (doc && doc.scrollLeft || body && body.scrollLeft || 0) -
          (doc && doc.clientLeft || body && body.clientLeft || 0);
        event.pageY = event.clientY +
          (doc && doc.scrollTop  || body && body.scrollTop  || 0) -
          (doc && doc.clientTop  || body && body.clientTop  || 0 );
    }
    var innerWidth = (window.innerWidth);
    var innerHeight = (window.innerHeight);
    // Use event.pageX / event.pageY here
    x = - (event.pageX / (innerWidth) * 100 - 100);
    // if(media_change.matches){
    //   x = - (event.pageX / window.innerWidth * 100 - 30)
    // }
    if(event.pageY > 550){
      y = - (event.pageY / innerHeight * 50) + 25; // change 25 to 50 and check
    } else {
      y = - (event.pageY / innerHeight * 50) + 90;  // change it to 120 & check
    }
    
    console.log('pageX',event.pageX);
    console.log('pageY',event.pageY);
    console.log('X',x);
    console.log('Y',y);
    var currPos = x+"deg "+y+"deg "+ "auto";
    modelViewer.attr('camera-orbit',currPos);
}