具有可调整大小的图像映射以及three.js

时间:2015-07-28 01:40:07

标签: javascript html css three.js imagemap

我有一个用HTML和一个three.js对象制作的图像映射。我对两者都很新,但是我希望在窗口调整大小时调整图像映射(已经调整了)。

我知道有一些线程可以解决这个问题。但是我似乎遇到了麻烦,我应该在哪里插入JavaScript代码,因为已经为three.js编写了东西。

我发现这一点似乎是其他人的解决方案,但我不知道在哪里放。

function mapRebuild(scaleValue) {
    var map = $("map"); // select your map or for all map you can choose $("map").each(function() { map = $(this);.....})
    map.find("area").each(function() { // select all areas
        var coords = $(this).attr("coords"); // extract coords
            coords = coords.split(","); // split to array
        var scaledCoords = "";
        for (var coord in coords) { // rebuild all coords with scaleValue
              scaledCoords += Math.floor(coords[coord] * scaleValue) + ",";
            }
        scaledCoords = scaledCoords.slice(0, -1); // last coma delete
        $(this).attr("coords", scaledCoords); // set new coords
        });
    }

我的网站是aurnab.info,它在Safari和Firefox上运行得最好。

代码看起来像这样。理想情况下,我想在某处插入上述脚本。

<!DOCTYPE html>
<html lang="en">
	<head>
		
		<title>aurnab saleh</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
		
			body {
				background:url("obj/me/homefinal.jpg");
				background-size:100%;
				margin: 0;
				width:1280px;
				height:800px;
				overflow:hidden;
				background-repeat: no-repeat;
				background-position:fixed;
				
				}
			
			canvas {
				position:absolute;
				top:0;
				left:0;
				z-index:-1;
				}
		
			
		
			
			
			
		</style>
	</head>
	

	<body>

		
		<script src="../build/three.min.js"></script>

		<script src="js/loaders/DDSLoader.js"></script>
		<script src="js/loaders/MTLLoader.js"></script>
		<script src="js/loaders/OBJMTLLoader.js"></script>

		<script src="js/Detector.js"></script>
		<script src="js/libs/stats.min.js"></script>
		
		
		
		<img src="obj/me/homefinal.jpg" width="1280" height="800" usemap="#Map" border="0" style="opacity:0"/>
<map name="Map" id="Map">
    <area shape="rect" coords="650,365,713,422" href="http://soundcloud.com/bilderberg-group" target="_blank" alt="soundcloud" />
  <area shape="rect" coords="650,465,711,522" href="http://djgunk.tumblr.com" target="_blank" alt="tumblr" />
  <area shape="rect" coords="648,565,710,619" href="http://vimeo.com/aurnab" target="_blank" alt="vimeo" />
  <area shape="rect" coords="647,665,711,723" href="email.html" target="_blank" alt="gmail" />
</map>

			
		<script>

			var container, stats;

			var camera, scene, renderer;

			var mouseX = 0, mouseY = 0;

			var windowHalfX = window.innerWidth / 2;
			var windowHalfY = window.innerHeight / 2;


			init();
			animate();


			function init() {
				
				container = document.createElement( 'div' );
				document.body.appendChild( container );
				
				

				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
				
				//*zoom in or out: #up = out, #down = in
				camera.position.z = 250;
				
				// scene

				scene = new THREE.Scene();

				var ambient = new THREE.AmbientLight( 0x000000 );
				
				scene.add( ambient );
				
					var light = new THREE.HemisphereLight( 0xff0000, 10, 0);
				light.position.set( 10, 100, 10 );
				scene.add( light );
				
				var light = new THREE.HemisphereLight( 0xff0000, 10, 0);
				light.position.set( 50, 50, 50 );
				scene.add( light );
			
				var light = new THREE.HemisphereLight( 0xff0000, 10, 0);
				light.position.set( 50, 50, 50 );
				scene.add( light );
					var directionalLight = new THREE.DirectionalLight( 0xffeedd );
				directionalLight.position.set( 0, -5, 2 ).normalize();
				scene.add( directionalLight );
				var directionalLight = new THREE.DirectionalLight( 0xffeedd );
				directionalLight.position.set( -10, -1, -1 ).normalize();
				scene.add( directionalLight );
				
				var directionalLight = new THREE.DirectionalLight( 0xffeedd );
				directionalLight.position.set( 10, 3, 3 ).normalize();
				scene.add( directionalLight );
				
				

				// model

				THREE.Loader.Handlers.add( /\.dds$/i, new THREE.DDSLoader() );

				var loader = new THREE.OBJMTLLoader();
				loader.load( 'obj/me/malehead.obj', 'obj/me/malehead.mtl', function ( object ) {

					//*move entire object up or down on y axis****
					object.position.y =  60;
					object.rotation.y =  12.7;
					object.rotation.x =  .3;
					
					scene.add( object );

				} );

				//

				
	renderer = new THREE.WebGLRenderer( { alpha: true } );
				renderer.setClearColor( 0x000000, 0 );
				renderer.setSize( window.innerWidth, window.innerHeight );
				container.appendChild( renderer.domElement );
				
				document.addEventListener( 'mousemove', onDocumentMouseMove, false );

				//

				window.addEventListener( 'resize', onWindowResize, false );

		
			}
			

			function onWindowResize() {

				windowHalfX = window.innerWidth / 3;
				windowHalfY = window.innerHeight / 3;

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			function onDocumentMouseMove( event ) {
				//* look left or right
				mouseX = ( event.clientX - windowHalfX ) / 10;
				//* look up his nostrils or not
				mouseY = ( event.clientY - windowHalfY ) / -2;

			}

			//

			function animate() {

				requestAnimationFrame( animate );
				render();

			}

			function render() {

				camera.position.x += (- mouseX - camera.position.x ) * .08;
				camera.position.y += ( - mouseY - camera.position.y ) * .008;

				camera.lookAt( scene.position );

				renderer.render( scene, camera );

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

1 个答案:

答案 0 :(得分:0)

我不知道我是否清楚这一点。如果您希望在窗口调整大小时调整图像大小,则可以执行以下操作:

//apply an id to the image to use it in the javascript
<img id = "yourImageID" src="obj/me/homefinal.jpg" usemap="#Map"  style="opacity:0;width:1280;height:800;border:0;"/>

//set an id to all the areas (i do it in only one for example)
<area id = "area1" shape="rect" coords="650,365,713,422" href="http://soundcloud.com/bilderberg-group" target="_blank" alt="soundcloud" />

//in the script define a variable for the image tag and the area tag.
var image = document.getElementById("yourImageID");
var area1 = document.getElementByID("area1");
var coordsarea1=[];//table to store the map coordinates
coordsArea1.push(650); 
coordsArea1.push(365);
coordsArea1.push(713);
coordsArea1.push(422);

var windowWidth = window.innerWidth;//store the window Width to calculate the percentage after
var windowHeight = window.innerHeight;

现在在窗口调整大小功能

function onWindowResize() {

  var percentX = windowWidth / window.innerWidth;//calculate the percentage of width change
  var percentY = windowHeight / window.innerHeight;//same for height change
  var windowWidth = window.innerWidth;//redefine the value of the width
  var windowHeight = window.innerHeight;//same for height

  image.style.width = window.innerWidth * percentX;//change the image width based on the window.innerWidth percentage change
  image.style.height = window.innerHeight * percentY;

  coordsArea1[0]*=percentX;//change the coords based on the percentage that the window  has changed
  coordsArea1[1]*=percentY;
  coordsArea1[2]*=percentX;
  coordsArea1[3]*=percentY;
  area1.coords = coordsArea1[0] + "," + coordsArea1[1] + "," + coordsArea1[2] + "," + coordsArea1[3];//not sure if is correct way to assign the values to the coords attribute

            windowHalfX = window.innerWidth / 3; // shouldn't this be window.innerWidth/2?
            windowHalfY = window.innerHeight / 3; //same as above?

            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();

            renderer.setSize( window.innerWidth, window.innerHeight );

        }

您应该注意手动更改宽度和高度会使图像失真。相反,您可以通过稍微更改代码来执行width = 1280和height = auto。