PROWAREtech

articles » current » javascript » three-js » make-objects-appear-to-watch-the-mouse-cursor

ThreeJS: Make Objects Appear to Watch the Mouse Cursor or Touch Point

How to make mesh objects appear to watch the moving mouse cursor or point of touch on mobile devices in THREE.js.

This code moves mesh objects on their x-axis and y-axis (rotation) making it appear that the objects are watching the user as they move around. A word of caution, the ability to watch a touch point on a mobile device disables the ability to scroll on the same device with one finger. Two or more fingers must be used to scroll.

See also Make Objects Appear to Follow the Mouse Cursor.

Dowload the three.min.js file used in this example here: THREEJS.zip


<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Objects Follow Cursor Example</title>
	<style>
		body {
			margin: 0;
			padding: 0;
			height: 200vh;
			background-color: #cc9966;
		}
		canvas {
			display: block;
			width: 100%;
			height: 100vh;
		}
	</style>
</head>
<body>
	<canvas></canvas>
	<script src="/js/three.min.js"></script>
	<script type="text/javascript">

(function () { // NOTE: box appears to follow the mouse cursor example

	var windowSize = function (withScrollBar) {
		var wid = 0;
		var hei = 0;
		if (typeof window.innerWidth != "undefined") {
			wid = window.innerWidth;
			hei = window.innerHeight;
		}
		else {
			if (document.documentElement.clientWidth == 0) {
				wid = document.body.clientWidth;
				hei = document.body.clientHeight;
			}
			else {
				wid = document.documentElement.clientWidth;
				hei = document.documentElement.clientHeight;
			}
		}
		return { width: wid - (withScrollBar ? (wid - document.body.offsetWidth + 1) : 0), height: hei };
	};

	var size = windowSize(true);

	// NOTE: issue these statements when resizing the window
	// camera.aspect = size.width / size.height;
	// camera.updateProjectionMatrix();
	// renderer.setPixelRatio(window.devicePixelRatio);
	// renderer.setSize(size.width, size.height);




	var canvas = document.getElementsByTagName("canvas")[0];

	// NOTE: create the scene to place objects in
	var scene = new THREE.Scene();
	scene.background = new THREE.Color(0x6699CC); // NOTE: make the background blue
	scene.matrixWorldAutoUpdate = true;



	
	// NOTE: create the camera with 75 degree field of view; this is how the scene is viewed by the user
	var camera = new THREE.PerspectiveCamera(75, size.width / size.height, 1, 5000);

	// NOTE: position the camera in space a bit
	camera.position.z = 2;


	var renderer = new THREE.WebGLRenderer({
		canvas: canvas
	});
	renderer.shadowMap.enabled = true;
	renderer.setPixelRatio(window.devicePixelRatio);
	renderer.setSize(size.width, size.height);
	renderer.render(scene, camera);


	var smiley = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjMAAAIzCAMAAADCjGjwAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNiAoV2luZG93cykiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6RTBGQTFFQzZDNzJGMTFFQ0JCRDFFMTc4M0YzRjYzNkUiIHhtcE1NOkRvY3VtZW50SUQ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Geometry = new THREE.BoxGeometry(1, 1, 1);
	var boxMaterial = new THREE.MeshLambertMaterial({ color: 0xeeeeee });
	var cubeSides = [ boxMaterial, boxMaterial, boxMaterial, boxMaterial, new THREE.MeshLambertMaterial({ map: new THREE.TextureLoader().load(smiley) }), boxMaterial ];
	for(var i = 0; i < boxes.length; i++) {
		boxes[i] = new THREE.Mesh(boxGeometry, cubeSides);
		boxes[i].position.x = 1 * i * (i % 2 == 0 ? -1 : 1);
		boxes[i].position.y = 2 * i * (i % 2 == 0 ? 1 : -1) * 0.25;
		boxes[i].position.z = -2;
		boxes[i].receiveShadow = true;
		scene.add(boxes[i]);
	}



	var light = new THREE.PointLight(0xffffff, 1.2, 10000);
	light.position.set(0,0,50);
	scene.add(light);



	var handler = function (element, type, func) {
		if (element.addEventListener) {
			element.addEventListener(type, func, false);
		} else if (window.attachEvent) {
			element.attachEvent("on" + type, func);
		} else {
			element["on" + type] = func;
		}
	};


	// NOTE: this function will set the camera to follow the box
	handler(canvas, "mousemove", function () {
		var offX = 0;
		var offY = 0;
		if (typeof window.pageXOffset != "undefined") {
			offX = window.pageXOffset;
			offY = window.pageYOffset;
		}
		else {
			if (document.documentElement.scrollTop == 0) {
				offX = document.body.scrollLeft;
				offY = document.body.scrollTop;
			}
			else {
				offX = document.documentElement.scrollLeft;
				offY = document.documentElement.scrollTop;
			}
		}
		var x, y;
		if (typeof event.pageX != "undefined") {
			x = event.pageX;
			y = event.pageY;
		}
		else {
			x = event.clientX;
			y = event.clientY;
		}
		x -= offX;
		y -= offY;
		if (x < 0) {
			x = 0;
		}
		if (y < 0) {
			y = 0;
		}
		var y2 = y - size.height / 2;
		var x2 = x - size.width / 2;
		for (var i = 0; i < boxes.length; i++) {
			boxes[i].rotation.x = Math.PI / 2 * y2 / (size.height / 2);
			boxes[i].rotation.y = Math.PI / 2 * x2 / (size.width / 2);
		}
	});

	// NOTE: this allows interaction with a mobile device (touch - it will prevent the device from scrolling with one finger!)
	handler(canvas, "touchmove", function () {
		if(event.touches.length > 1) { return; }
		event.preventDefault();
		var offX = 0;
		var offY = 0;
		if (typeof window.pageXOffset != "undefined") {
			offX = window.pageXOffset;
			offY = window.pageYOffset;
		}
		else {
			if (document.documentElement.scrollTop == 0) {
				offX = document.body.scrollLeft;
				offY = document.body.scrollTop;
			}
			else {
				offX = document.documentElement.scrollLeft;
				offY = document.documentElement.scrollTop;
			}
		}
		var touch = event.changedTouches[0], x = touch.pageX, y = touch.pageY;
		x -= offX;
		y -= offY;
		if (x < 0) {
			x = 0;
		}
		if (y < 0) {
			y = 0;
		}
		var y2 = y - size.height / 2;
		var x2 = x - size.width / 2;
		for (var i = 0; i < boxes.length; i++) {
			boxes[i].rotation.x = Math.PI / 2 * y2 / (size.height / 2);
			boxes[i].rotation.y = Math.PI / 2 * x2 / (size.width / 2);
		}
	});

	handler(canvas, "mouseout", function () {
		for (var i = 0; i < boxes.length; i++) {
			boxes[i].rotation.x = boxes[i].rotation.y = 0;
		}
	});


	// NOTE: MUST HAVE AN ANIMATE FUNCTION
	var animate = function () {

		renderer.render(scene, camera);
		requestAnimationFrame(animate);

	};
	animate();

})();

	</script>
</body>
</html>

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE