How to load SVG file into SVGRenderer in three.js -
i'm trying use svgrenderer
in three.js (http://threejs.org/examples/#svg_sandbox). example shows how make svg element (a circle) on fly. want import svg file have in computer. how that?
the createelementns command doesn't seem support importing svg files?
i want image.svg
displayed on three.js scene.
you can use three.svgloader() library achieve :
var svgmanager = new three.svgloader(); var url = 'https://upload.wikimedia.org/wikipedia/commons/b/b0/newtux.svg'; function svg_loading_done_callback(doc) { init(new three.svgobject(doc)); animate(); }; svgmanager.load(url, svg_loading_done_callback, function(){console.log("loading svg...");}, function(){console.log("error loading svg!"); }); var amount = 100; var camera, scene, renderer; function init(svgobject) { camera = new three.perspectivecamera(75, window.innerwidth / window.innerheight, 1, 10000); camera.position.z = 500; scene = new three.scene(); svgobject.position.x = math.random() * 1000 - 500; svgobject.position.y = math.random() * 1000 - 500; svgobject.position.z = math.random() * 1000 - 500; scene.add(svgobject); var ambient = new three.ambientlight(0x80ffff); scene.add(ambient); var directional = new three.directionallight(0xffff00); directional.position.set(-1, 0.5, 0); scene.add(directional); renderer = new three.svgrenderer(); renderer.setclearcolor(0xf0f0f0); renderer.setsize(window.innerwidth, window.innerheight-5); document.body.appendchild(renderer.domelement); window.addeventlistener('resize', onwindowresize, false); } function onwindowresize() { camera.aspect = window.innerwidth / window.innerheight; camera.updateprojectionmatrix(); renderer.setsize(window.innerwidth, window.innerheight); } function animate() { requestanimationframe(animate); render(); } function render() { var time = date.now() * 0.0002; camera.position.x = math.sin(time) * 500; camera.position.z = math.cos(time) * 500; camera.lookat(scene.position); renderer.render(scene, camera); }
*{margin:0}
<script src="http://threejs.org/build/three.min.js"></script> <script src="http://threejs.org/examples/js/renderers/svgrenderer.js"></script> <script src="http://threejs.org/examples/js/renderers/projector.js"></script> <script src="http://threejs.org/examples/js/loaders/svgloader.js"></script>
Comments
Post a Comment