我目前正在使用角度2和打字稿来玩离子2。现在我正在尝试在html页面中编写一些脚本(或将其包含在文件中)。
我知道,javascript库应该包含在index.html文件中,该文件位于www根文件夹中。但是如果我想在单个html页面中插入脚本呢?
例如,我需要在页面上有一个浮动立方体。我通过在root html文件中包含three.js(3D库)来做正确的事,但是在页面html文件中包含一个floatingcube.js文件(或者在文件中编写脚本),这是行不通的。
我不知道我是否在这里犯了错误,但也许有人知道如何使用外部库以及如何在离子2中编写脚本。
编辑:以下是旋转立方体JS部分:
JS:
var camera;
var scene;
var renderer;
var mesh;
var geometry;
var material1;
var material2;
var material3;
var material4;
var material5;
var material6;
var materials;
var meshFaceMaterial;
var isMouseDown = false;
init();
animate();
function init() {
window.addEventListener('mousedown', onMouseDown);
window.addEventListener('mouseup', onMouseUp);
window.addEventListener("touchstart", handleStart);
window.addEventListener("touchend", handleEnd);
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.z = 25;
scene = new THREE.Scene();
light = new THREE.AmbientLight( 0xE6E6E6 );
scene.add( light );
geometry = new THREE.CubeGeometry( 10, 10, 10);
material1 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub1.jpg') } );
material2 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub2.jpg') } );
material3 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub3.jpg') } );
material4 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub4.jpg') } );
material5 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub5.jpg') } );
material6 = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('img/cub6.jpg') } );
materials = [material1, material3, material5, material6, material4, material2];
meshFaceMaterial = new THREE.MeshFaceMaterial( materials );
mesh = new THREE.Mesh(geometry, meshFaceMaterial );
mesh.position.z = 0;
scene.add( mesh );
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.getElementById('render').appendChild( renderer.domElement );
window.addEventListener( 'resize', onWindowResize, false );
render();
}
function onMouseDown(){
isMouseDown = true;
}
function onMouseUp(){
isMouseDown = false;
}
function handleStart(){
isMouseDown = true;
}
function handleEnd(){
isMouseDown = false;
}
function animate() {
if(!isMouseDown){
mesh.rotation.x += .012;
mesh.rotation.y += .014;
mesh.rotation.z += .018;
}
else {
mesh.rotation.x += .0;
mesh.rotation.y += .0;
mesh.rotation.y += .0;
}
render();
requestAnimationFrame( animate );
}
function render() {
renderer.render( scene, camera );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
render();
}