拥有一个可调整大小的图像映射以及three.js
Having a resizable image map along with three.js
我有一个在HTML和three.js对象中制作的图像映射。我对这两个都很新,但我想让图像映射作为窗口调整大小(它已经这样做了)。
我知道有些线程可以覆盖这一点,但是我似乎遇到了麻烦,我应该在哪里插入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
});
}
我的网站是aurab .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>
我不知道我是否讲清楚了。如果你想让图片和窗口一样调整大小,你可以这样做:
//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对代码进行一些更改。
相关文章:
- 如何在映射数组中添加换行符
- ng映射方向备选方案
- 无法通过数组映射绑定
- ReactJS映射:如何仅在url变量不为空时呈现html链接
- 使用Scala Play Framework视图中的键检索映射值
- 在Three.js中导出网格会提高性能吗
- 如何在three.js上添加两个向量
- 淘汰映射;不起作用
- Three.js Points Rotation
- 无法读取属性'材料'未定义的Three.js
- Potree/Three.js测量问题
- 映射数组ES6时考虑空值
- Three.JS OBJ模型未显示
- Lodash映射并返回唯一
- 有没有一种方法可以获得three.js的最小/lite版本
- JS和three.JS,在浏览器中进行参数测试
- Three.js将映射分配给object.children[0]会更改整个object's地图
- 使用three.js进行纹理映射
- 拥有一个可调整大小的图像映射以及three.js
- 将iframe映射为纹理-three.js