我的Javascript代码在JSFiddle之外不起作用

时间:2014-01-20 11:05:22

标签: javascript three.js jsfiddle

由于一些奇怪的原因,我的代码将在JSFiddle中加载和工作,但是当我尝试在JSFiddle之外使用它时它将无法工作。 有谁知道修复?或者发生了什么?链接到JSFiddle ... http://jsfiddle.net/56yK9/12/

If you go onto the jsfiddle link click "run" after the page has loaded.

要查看我的html文档代码:http://pastebin.com/D9eeVysr

3 个答案:

答案 0 :(得分:0)

在你的html文档中,imageWidth和imageHeight等于0,因为其他的其他函数/代码都不会等到从网站加载的图片(在你的开发者工具中检查 - >网络)这就是问题所在,所以精灵是不可见的..你的代码可能需要改变..

var texture = THREE.ImageUtils.loadTexture(tUrl, {}, callback);
callback = function(texture){
........code ur stuff here...........
}

只需浏览链接中的代码即可了解图像/纹理加载... https://stackoverflow.com/a/21277768/2089677

答案 1 :(得分:0)

此处此代码可能对您有帮助...... http://jsfiddle.net/ebeit303/e5Q3N/1/

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js" ></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/three.js/r61/three.min.js" ></script>
</head>
<body >
    <script>
        var container;
        var camera, scene, renderer;
        var group;

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

            }
            catch (e) {
                container.innerHTML = e.toString();
            }
        });
        function init() {
            // renderer
            renderer = new THREE.WebGLRenderer({antialias: true});
            renderer.setClearColorHex(0xADD8E6, 1);
            renderer.setSize(window.innerWidth, window.innerHeight);
            container.appendChild(renderer.domElement);

            camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 2100);
            camera.position.z = 1000;

            scene = new THREE.Scene();

            group = new THREE.Object3D();
            scene.add(group);

            var tUrl = "http://www.mediafire.com/convkey/7344/ra2l3kgscpvjflafg.jpg";
            var texture = THREE.ImageUtils.loadTexture(tUrl, {}, spriteMesh);

            window.addEventListener('resize', onWindowResize, false);
            animate();
        }
        function spriteMesh(ashTexture) {
            // create sprites
            var ashMaterial = new THREE.SpriteMaterial({map: ashTexture});
            var Ash = new THREE.Sprite(ashMaterial);
            var imageWidth1 = ashMaterial.map.image.width;
            var imageHeight1 = ashMaterial.map.image.height;
            Ash.scale.set(3 * imageWidth1, 3 * imageHeight1, 1.0);
            Ash.position.set(2 * imageWidth1, 2 * imageHeight1, 1.0);
            group.add(Ash);
        }
        function onWindowResize() {
            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();
            renderer.setSize(window.innerWidth, window.innerHeight);
        }

        function animate() {
            requestAnimationFrame(animate);
            update();
            render();
        }
        function update() {
            camera.lookAt(scene.position);
            camera.updateProjectionMatrix();
        }
        function render() {
            renderer.render(scene, camera);
        }

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

答案 2 :(得分:-1)

我猜你错过了页面加载/就绪处理程序 这个小例子似乎至少表现出一些东西:

http://pastebin.com/NmnTeX6Z

你可以在这里看看你的jsfiddle创作的更清晰的输出:
jsfiddle.net/56yK9/show /