如何制作可点击的CSS3DObject

时间:2014-02-03 15:52:11

标签: javascript html css three.js

我正在使用三个JS CSS3DRenderer - 试图让CSS3DObject更新它的位置.z点击。这是我的代码:

var element = document.createElement( "div" );
element.style.width = "90px";
element.style.height = "120px";
element.style.backgroundColor = "#5C6167";
//
var object = new THREE.CSS3DObject( element );
object.position.x = 0;
object.position.y = 0;
object.position.z = 100;
object.addEventListener( 'click', function ( event ) {
new TWEEN.Tween( object.position ).to( {z: 200}, 500 ).easing( TWEEN.Easing.Quadratic.Out).start();
});
scene.add( object );

无论出于何种原因,CSS3DObject都不会发生点击事件。请指导。 :)

2 个答案:

答案 0 :(得分:4)

这是从CSS3DObject处理click事件并访问该元素的父对象的一种方法:

var object = new THREE.CSS3DObject( element );

element.parent = object;

object.element.onclick = function() { this.parent.position.y += 10; };

three.js r.65

答案 1 :(得分:-1)

您是否尝试过mousedown和mouseup事件? 看我的样本:

var mouseDown = false,
    mouseX = 0,
    mouseY = 0;

function onMouseDown(evt) {
    evt.preventDefault();

    mouseDown = true;
    mouseX = evt.clientX;
    mouseY = evt.clientY;
}

function onMouseUp(evt) {
    evt.preventDefault();

    mouseDown = false;
}

function addMouseHandler(canvas) {
    canvas.addEventListener('mousedown', onMouseDown, false);
    canvas.addEventListener('mouseup', onMouseUp, false);
}

$(document).ready(function () {
    var canvas= document.getElementById("canvas");

    addMouseHandler(canvas);

});