聚合物和 three.js
Polymer and three.js
我正在尝试 运行 three.js 演示 http://jsfiddle.net/hfj7gm6t/ 进入我的聚合物应用程序,但该应用程序显示错误:
com-model-viewer.html:33 Uncaught TypeError: Cannot read property 'myview' of undefined
<link rel='import' href='../../../bower_components/polymer/polymer.html'>
<dom-module id='com-model-viewer'>
<template>
<ul id='myview'>
</ul>
</template>
<script>
Polymer({
is:'com-model-viewer',
ready: function(){
console.log(this);
var camera, scene, renderer;
var geometry, material, mesh;
init();
animate();
function init(){
console.log();
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000);
camera.position.z = 1000;
scene = new THREE.Scene();
geometry = new THREE.BoxGeometry(200, 200, 200);
material = new THREE.MeshBasicMaterial({
color: 0xff0000,
wireframe: true
});
mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
Polymer.dom( this.$.myview ).appendChild(node);
}
function animate(){
requestAnimationFrame(animate);
mesh.rotation.x += 0.01;
mesh.rotation.y += 0.02;
renderer.render(scene, camera);
}
}
});
</script>
<script src='../../../node_modules/three/three.min.js'></script>
</dom-module>
我猜你函数 init
中的 this
不是你要找的。 this
指向函数而不是聚合物对象。
尝试将原始 this
变量传递给 init
或将 init
重写为 es6 箭头函数。
我正在尝试 运行 three.js 演示 http://jsfiddle.net/hfj7gm6t/ 进入我的聚合物应用程序,但该应用程序显示错误:
com-model-viewer.html:33 Uncaught TypeError: Cannot read property 'myview' of undefined
<link rel='import' href='../../../bower_components/polymer/polymer.html'>
<dom-module id='com-model-viewer'>
<template>
<ul id='myview'>
</ul>
</template>
<script>
Polymer({
is:'com-model-viewer',
ready: function(){
console.log(this);
var camera, scene, renderer;
var geometry, material, mesh;
init();
animate();
function init(){
console.log();
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000);
camera.position.z = 1000;
scene = new THREE.Scene();
geometry = new THREE.BoxGeometry(200, 200, 200);
material = new THREE.MeshBasicMaterial({
color: 0xff0000,
wireframe: true
});
mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
Polymer.dom( this.$.myview ).appendChild(node);
}
function animate(){
requestAnimationFrame(animate);
mesh.rotation.x += 0.01;
mesh.rotation.y += 0.02;
renderer.render(scene, camera);
}
}
});
</script>
<script src='../../../node_modules/three/three.min.js'></script>
</dom-module>
我猜你函数 init
中的 this
不是你要找的。 this
指向函数而不是聚合物对象。
尝试将原始 this
变量传递给 init
或将 init
重写为 es6 箭头函数。