3

我正在学习如何将 Three.js 集成到 Polymer 的 Lit-Element。我当前的问题是我需要引用一个 div 元素来附加 Three 的 Renderer 元素。通常是这样完成的:

box = document.getElementById("box")
box.appendChild(renderer.domElement)

不幸的是,我无法找到如何从 constructor()/firstUpdate() 引用到渲染函数中声明的 div。你会怎么做?

这是我目前最好的结果: 渲染器元素偏离目标

这是获得此结果的代码: HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

    <box-test></box-test>

    <script type="module" src="src/components/box-test.js" crossorigin></script>
</body>
</html>

Javascript:

import { LitElement, html } from '@polymer/lit-element'; 

import * as THREE from 'three/build/three.module';



class BoxTest extends LitElement  {

    constructor() {
      super();
      var scene = new THREE.Scene();
      var camera = new THREE.PerspectiveCamera(75, 1, 0.1, 1000);

      var renderer = new THREE.WebGLRenderer();
      renderer.setSize(300, 300);

      //box = document.getElementById("box");
      document.body.appendChild(renderer.domElement);



      var geometry = new THREE.BoxGeometry(1, 1, 1);
      var material = new THREE.MeshBasicMaterial({ color: 0x00ff00, wireframe: true });
      var cube = new THREE.Mesh(geometry, material);
      scene.add(cube);

      camera.position.z = 5;

      var animate = function () {
        requestAnimationFrame(animate);

        cube.rotation.x += 0.01;
        cube.rotation.y += 0.01;

        renderer.render(scene, camera);
      };

      animate();
    }


  render() {
    return html`
    <style>
      #box { border: 1px solid red; height: 310px; width: 310px;}
    </style>

    <section>
    The webgl animation must be in the red box
    <div id="box">
    </div>

    </section>
    `
  }

}

window.customElements.define('box-test', BoxTest);

任何建议都将受到欢迎。

4

1 回答 1

5

在您的constructor函数中,您可以通过将其设置为保留变量以供以后在其他函数中使用this

this.renderer = renderer

然后在firstUpdated功能上你可以做

firstUpdated () {
  let box = this.shadowRoot.getElementById('box')
  box.appendChild(this.renderer.domElement)
}

示例代码:

<script type='module'>
  import { LitElement, html } from '@polymer/lit-element'
  import * as THREE from 'three/build/three.module'

  class BoxTest extends LitElement {
    constructor () {
      super()

      var scene = new THREE.Scene()
      var camera = new THREE.PerspectiveCamera(75, 1, 0.1, 1000)
      var geometry = new THREE.BoxGeometry(1, 1, 1)
      var material = new THREE.MeshBasicMaterial({ color: 0x00ff00, wireframe: true })
      var cube = new THREE.Mesh(geometry, material)

      scene.add(cube)
      camera.position.z = 5

      ;(function animate () {
        requestAnimationFrame(animate)
        cube.rotation.x += 0.01
        cube.rotation.y += 0.01
        renderer.render(scene, camera)
      }())

      var renderer = new THREE.WebGLRenderer()
      renderer.setSize(300, 300)
      this.renderer = renderer
    }

    firstUpdated () {
      let box = this.shadowRoot.getElementById('box')
      box.appendChild(this.renderer.domElement)
    }

    render () {
      return html`
        <style>
          #box { border: 1px solid red; height: 310px; width: 310px;}
        </style>

        <section>
          The webgl animation must be in the red box
          <div id="box"></div>
        </section>
      `
    }
  }

  window.customElements.define('box-test', BoxTest)
</script>
于 2018-11-15T08:38:09.460 回答