2

I am working on this project... that is suppose to work in a browser and on iPhone as well.

So I am dealing with the events issue right now, which JavaScript events can I use for iPhone?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
BorisD
  • 2,231
  • 5
  • 25
  • 35

1 Answers1

4

It depends on what functionality you need, but try this on your phone. It should render some a cube structure similar to these: three.js cubes
(source: lifesine.eu)

It should be possible to touch and drag. This is based on the old cube drag sample that comes with three.js and here are the events used:

document.addEventListener( 'mousedown', onDocumentMouseDown, false );
document.addEventListener( 'touchstart', onDocumentTouchStart, false );
document.addEventListener( 'touchmove', onDocumentTouchMove, false );

And here are the listeners:

function onDocumentMouseDown( event ) {

                event.preventDefault();

                document.addEventListener( 'mousemove', onDocumentMouseMove, false );
                document.addEventListener( 'mouseup', onDocumentMouseUp, false );
                document.addEventListener( 'mouseout', onDocumentMouseOut, false );

                mouseXOnMouseDown = event.clientX - windowHalfX;
                targetRotationOnMouseDown = targetRotation;
            }

            function onDocumentMouseMove( event ) {

                mouseX = event.clientX - windowHalfX;
                mouseY = event.clientY - windowHalfY;

                targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
            }

            function onDocumentMouseUp( event ) {

                document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
                document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
                document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
            }

            function onDocumentMouseOut( event ) {

                document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
                document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
                document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
            }

            function onDocumentTouchStart( event ) {

                if ( event.touches.length == 1 ) {

                    event.preventDefault();

                    mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
                    targetRotationOnMouseDown = targetRotation;

                }
            }

            function onDocumentTouchMove( event ) {

                if ( event.touches.length == 1 ) {

                    event.preventDefault();

                    mouseX = event.touches[ 0 ].pageX - windowHalfX;
                    targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;

                }
            }

Note that there are a few variables used, which might not be obvious, like targetRotation, targetRotationOnMouseDown, etc. Feel free to use the source code from that link, but be aware that I coded that last year, so, some of the three.js code might be slightly different(maybe materials and such), but the events part should still work if you paste it in your code.

HTH

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
George Profenza
  • 50,687
  • 19
  • 144
  • 218
  • Thanks for the help again dude!...I tried your demo,and sad but true, it's not working on iphone...I mean the only rotate that works is (y). And second thing is...my problems is bigger...not only that I need to rotate the "camera"...I've made draggable objects on canvas – BorisD Nov 29 '11 at 15:05
  • See this question : http://stackoverflow.com/questions/8307535/three-js-project-on-iphone-events-issue-selectdrag-object – BorisD Nov 29 '11 at 15:52
  • Technically, if you can rotate on Y, the touch events work, which **answers your question**("Wich javascript events can I use for iphone") ;). It works only on Y because that's how I've coded, there's nothing else to handle it otherwise(like dragging). Can't commit to other question at the moment. Was looking at your [other question](http://stackoverflow.com/questions/8292486/three-js-how-to-detect-what-shape-was-selected-after-drag), and spotted a few issues, but as I said, no time at the moment. will try later during the week – George Profenza Nov 29 '11 at 23:43