How to move a script into a div container

Viewed 119

So im working on a website for a client and want to move a script that i've built in codepen into a div container in html elementor, everytime i add it into the website (elementor) it places the window at the bottom of the page and not into the container.

Update

Trying to move JS into a Div container to add to specific part of website, would a document.getElementById work? and how should i go about it??

I've tried to add it within the web builder via wordpress but when i attach the JS via elementor, it takes the script and applies / views it on the bottom of the page under the footer, I just want the script to be viewable in a specific section.

HTML

<script src="https://rawcdn.githack.com/mrdoob/three.js/d9f87fb1a2c5db1ea0e2feda9bd42b39b5bedc41/build/three.min.js"></script>
<!-- OrbitControls.js -->
<script src="https://rawcdn.githack.com/mrdoob/three.js/d9f87fb1a2c5db1ea0e2feda9bd42b39b5bedc41/examples/js/controls/OrbitControls.js"></script>
<!-- DRACOLoader.js -->
<script src="https://rawcdn.githack.com/mrdoob/three.js/d9f87fb1a2c5db1ea0e2feda9bd42b39b5bedc41/examples/js/loaders/DRACOLoader.js"></script>
<!-- GLTFLoader.js -->
<script src="https://rawcdn.githack.com/mrdoob/three.js/d9f87fb1a2c5db1ea0e2feda9bd42b39b5bedc41/examples/js/loaders/GLTFLoader.js"></script>

<div id="container">
  *debug* add js element to this container
  
</div>

JS

let gltf = null;
let mixer = null;
let clock = new THREE.Clock();
let controls;
let camera;

init();
animate();
  
function init() {
    width = window.innerWidth;
    height = window.innerHeight;
    
    scene = new THREE.Scene();
    
  let light = new THREE.PointLight( 0xffffcc, 20, 200 );
light.position.set( 4, 30, 80 );
scene.add( light );

let light2 = new THREE.AmbientLight( 0x20202A, 20, 100 );
light2.position.set( 30, -10, 30 );
scene.add( light2 );

  
    camera = new THREE.PerspectiveCamera( 60, width / height, 0.01, 10000 );
    camera.position.set(0, 3, 10);

    let geometry = new THREE.BoxGeometry(100, 5, 100);
    let material = new THREE.MeshLambertMaterial({
        color: "#707070"
    });
    
 
    let manager = new THREE.LoadingManager();
    manager.onProgress = function ( item, loaded, total ) {
        console.log( item, loaded, total );
    };

    let loader = new THREE.GLTFLoader();
    loader.setCrossOrigin( 'anonymous' ); 
    loader.setDRACOLoader( new THREE.DRACOLoader() );
    
    let scale = 0.01;
    let url = "https://8ad.studio/wp-content/uploads/3D%20Assets/8AD_LOGO.gltf";
    
    loader.load(url, function (data) {
        gltf = data;
        let object = gltf.scene;
        object.scale.set(scale, scale, scale);
        //object.position.y = -5;
        //object.position.x = 4;
        object.castShadow = true;
        object.receiveShadow = true;

        let animations = gltf.animations;
        if ( animations && animations.length ) {
            mixer = new THREE.AnimationMixer( object );
            for ( let i = 0; i < animations.length; i ++ ) {
                let animation = animations[ i ];
                mixer.clipAction( animation ).play();
            }
        }
        scene.add(object);
    });

    

    renderer = new THREE.WebGLRenderer();
    renderer.setClearColor( 0xffffff );
    renderer.shadowMap.enabled = true;
    
    controls = new THREE.OrbitControls( camera, renderer.domElement );
    controls.rotateSpeed = 0.3;
controls.zoomSpeed = 0.9;

controls.minDistance = 12;
controls.maxDistance = 12;

controls.minPolarAngle = 0; // radians
controls.maxPolarAngle = Math.PI /2; // radians

controls.enableDamping = true;
controls.dampingFactor = 0.05;

    renderer.setSize( width, height );
    renderer.gammaOutput = true;
    document.body.appendChild( renderer.domElement );
}

function animate() {
    requestAnimationFrame( animate );
    if (mixer) mixer.update(clock.getDelta());
    controls.update();
    render();
}

function render() {
    renderer.render( scene, camera );
}



1 Answers

If you're wanting to make the threejs canvas appear inside a div, you have to change where the canvas appears in the DOM. This is done in Threejs using the renderer.domElement

// change this:
document.body.appendChild( renderer.domElement );
// to this:
document.getElementById('container').appendChild( renderer.domElement );

Check my codepen for an example. Traditionally this might be used if you're showing a 3d model of a product to customers, and you want to embed the canvas in a container in the DOM. But now the position of your models are out of whack. You'll just need to configure the camera position/zoom

Related