具有可调整大小的图像映射以及 three.js
Having a resizable image map along with three.js
我有一个用 HTML 制作的图像贴图和一个 three.js 对象。我对两者都很陌生,但我想让图像映射随着 window 调整大小(它已经这样做了)而调整大小。
我知道有一些线程稍微涵盖了这个。但是我似乎 运行 遇到了我应该在哪里插入 JavaScript 代码的麻烦,因为已经为 three.js.
我发现 JavaScript 的这一点对其他人来说似乎是一个解决方案,但我不知道该把它放在哪里。
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>
我不知道我是否明白这一点。如果您希望图像随着 window 调整大小而调整大小,那么您可以这样做:
//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;
现在在 window 调整大小功能中
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。
我有一个用 HTML 制作的图像贴图和一个 three.js 对象。我对两者都很陌生,但我想让图像映射随着 window 调整大小(它已经这样做了)而调整大小。
我知道有一些线程稍微涵盖了这个。但是我似乎 运行 遇到了我应该在哪里插入 JavaScript 代码的麻烦,因为已经为 three.js.
我发现 JavaScript 的这一点对其他人来说似乎是一个解决方案,但我不知道该把它放在哪里。
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>
我不知道我是否明白这一点。如果您希望图像随着 window 调整大小而调整大小,那么您可以这样做:
//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;
现在在 window 调整大小功能中
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。