SVGLoader.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <base href="../../../" />
  6. <script src="page.js"></script>
  7. <link type="text/css" rel="stylesheet" href="page.css" />
  8. </head>
  9. <body>
  10. [page:Loader] &rarr;
  11. <h1>[name]</h1>
  12. <p class="desc">A loader for loading a `.svg` resource.<br >
  13. [link:https://en.wikipedia.org/wiki/Scalable_Vector_Graphics Scalable Vector Graphics] is an XML-based vector image format for two-dimensional graphics with support for interactivity and animation.
  14. </p>
  15. <h2>Import</h2>
  16. <p>
  17. [name] is an add-on, and must be imported explicitly.
  18. See [link:#manual/introduction/Installation Installation / Addons].
  19. </p>
  20. <code>
  21. import { SVGLoader } from 'three/addons/loaders/SVGLoader.js';
  22. </code>
  23. <h2>Code Example</h2>
  24. <code>
  25. // instantiate a loader
  26. const loader = new SVGLoader();
  27. // load a SVG resource
  28. loader.load(
  29. // resource URL
  30. 'data/svgSample.svg',
  31. // called when the resource is loaded
  32. function ( data ) {
  33. const paths = data.paths;
  34. const group = new THREE.Group();
  35. for ( let i = 0; i < paths.length; i ++ ) {
  36. const path = paths[ i ];
  37. const material = new THREE.MeshBasicMaterial( {
  38. color: path.color,
  39. side: THREE.DoubleSide,
  40. depthWrite: false
  41. } );
  42. const shapes = SVGLoader.createShapes( path );
  43. for ( let j = 0; j < shapes.length; j ++ ) {
  44. const shape = shapes[ j ];
  45. const geometry = new THREE.ShapeGeometry( shape );
  46. const mesh = new THREE.Mesh( geometry, material );
  47. group.add( mesh );
  48. }
  49. }
  50. scene.add( group );
  51. },
  52. // called when loading is in progresses
  53. function ( xhr ) {
  54. console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
  55. },
  56. // called when loading has errors
  57. function ( error ) {
  58. console.log( 'An error happened' );
  59. }
  60. );
  61. </code>
  62. <h2>Examples</h2>
  63. <p>
  64. [example:webgl_loader_svg]
  65. </p>
  66. <h2>Constructor</h2>
  67. <h3>[name]( [param:LoadingManager manager] )</h3>
  68. <p>
  69. [page:LoadingManager manager] — The [page:LoadingManager loadingManager] for the loader to use. Default is [page:LoadingManager THREE.DefaultLoadingManager].
  70. </p>
  71. <p>
  72. Creates a new [name].
  73. </p>
  74. <h2>Properties</h2>
  75. <p>See the base [page:Loader] class for common properties.</p>
  76. <h2>Methods</h2>
  77. <p>See the base [page:Loader] class for common methods.</p>
  78. <h3>[method:undefined load]( [param:String url], [param:Function onLoad], [param:Function onProgress], [param:Function onError] )</h3>
  79. <p>
  80. [page:String url] — A string containing the path/URL of the `.svg` file.<br />
  81. [page:Function onLoad] — (optional) A function to be called after loading is successfully completed. The function receives an array of [page:ShapePath] as an argument.<br />
  82. [page:Function onProgress] — (optional) A function to be called while the loading is in progress. The argument will be the XMLHttpRequest instance, which contains [page:Integer total] and [page:Integer loaded] bytes. If the server does not set the Content-Length header; .[page:Integer total] will be 0.<br />
  83. [page:Function onError] — (optional) A function to be called if an error occurs during loading. The function receives the error as an argument.<br />
  84. </p>
  85. <p>
  86. Begin loading from url and call onLoad with the response content.
  87. </p>
  88. <h2>Static Methods</h2>
  89. <h3>[method:Array createShapes]( [param:ShapePath shape] )</h3>
  90. <p>
  91. [page:ShapePath shape] — A ShapePath from the array of [page:ShapePath], given as argument in the onLoad function for the load function of [page:SVGLoader].<br />
  92. </p>
  93. <p>
  94. Returns one or more [page:Shape] objects created from the [param:ShapePath shape] provided as an argument in this function.
  95. </p>
  96. <h2>Source</h2>
  97. <p>
  98. [link:https://github.com/mrdoob/three.js/blob/master/examples/jsm/loaders/SVGLoader.js examples/jsm/loaders/SVGLoader.js]
  99. </p>
  100. </body>
  101. </html>