简体   繁体   中英

My Javascript Code doesnt work outside of JSFiddle

For some weird reason my code will load and work in JSFiddle, however it will not work when I try using it outside of JSFiddle. Does anyone know a fix? Or what is happening? Link to the JSFiddle... http://jsfiddle.net/56yK9/12/

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

To see my html document code: http://pastebin.com/D9eeVysr

In your html document imageWidth and imageHeight equal 0, because the rest of the other functions/code doesnt wait until the image loaded from the site, (check it in your developer tool -> network) thats the problem here, so the sprite is invisible..your code might need changes..

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

just go through the code in the link it will give you some idea on image/texture loading... https://stackoverflow.com/a/21277768/2089677

here this code might help you... 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>

I guess you are missing an on page load / ready handler.
This little example seems to at least display something:

http://pastebin.com/NmnTeX6Z

You can take a look at a much cleaner output of your jsfiddle creation here:
jsfiddle.net/56yK9/show/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM