SVGLoader.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>SVGLoader - Three.js Docs</title>
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <script src="../scripts/highlight.min.js"></script>
  8. <link type="text/css" rel="stylesheet" href="../styles/highlight-three.css">
  9. <link type="text/css" rel="stylesheet" href="../styles/page.css">
  10. </head>
  11. <body>
  12. <p class="inheritance" translate="no"><a href="Loader.html">Loader</a> → </p>
  13. <h1 translate="no">SVGLoader</h1>
  14. <section>
  15. <header>
  16. <div class="class-description"><p>A loader for the SVG format.</p>
  17. <p>Scalable Vector Graphics is an XML-based vector image format for two-dimensional graphics
  18. with support for interactivity and animation.</p></div>
  19. <h2>Code Example</h2>
  20. <div translate="no"><pre><code class="language-js">const loader = new SVGLoader();
  21. const data = await loader.loadAsync( 'data/svgSample.svg' );
  22. const paths = data.paths;
  23. const group = new THREE.Group();
  24. for ( let i = 0; i &lt; paths.length; i ++ ) {
  25. const path = paths[ i ];
  26. const material = new THREE.MeshBasicMaterial( {
  27. color: path.color,
  28. side: THREE.DoubleSide,
  29. depthWrite: false
  30. } );
  31. const shapes = SVGLoader.createShapes( path );
  32. for ( let j = 0; j &lt; shapes.length; j ++ ) {
  33. const shape = shapes[ j ];
  34. const geometry = new THREE.ShapeGeometry( shape );
  35. const mesh = new THREE.Mesh( geometry, material );
  36. group.add( mesh );
  37. }
  38. }
  39. scene.add( group );
  40. </code></pre></div>
  41. </header>
  42. <article>
  43. <h2 class="subsection-title">Import</h2>
  44. <p><span translate="no">SVGLoader</span> is an addon, and must be imported explicitly, see <a href="https://threejs.org/manual/#en/installation" target="_blank">Installation#Addons</a>.</p>
  45. <pre><code class="language-js">import { SVGLoader } from 'three/addons/loaders/SVGLoader.js';</code></pre>
  46. <div class="container-overview">
  47. <h2>Constructor</h2>
  48. <h3 class="name name-method" id="SVGLoader" translate="no">new <a href="#SVGLoader">SVGLoader</a><span class="signature">( manager : <span class="param-type">LoadingManager</span> )</span> </h3>
  49. <div class="method">
  50. <div class="description">
  51. <p>Constructs a new SVG loader.</p>
  52. </div>
  53. <table class="params">
  54. <tbody>
  55. <tr>
  56. <td class="name"><code>manager</code></td>
  57. <td class="description last"><p>The loading manager.</p></td>
  58. </tr>
  59. </tbody>
  60. </table>
  61. </div>
  62. </div>
  63. <h2 class="subsection-title">Properties</h2>
  64. <div class="member">
  65. <h3 class="name" id="defaultDPI" translate="no">.<a href="#defaultDPI">defaultDPI</a><span class="type-signature"> : number</span> </h3>
  66. <div class="description">
  67. <p>Default dots per inch.<br/>Default is <code>90</code>.</p>
  68. </div>
  69. </div>
  70. <div class="member">
  71. <h3 class="name" id="defaultUnit" translate="no">.<a href="#defaultUnit">defaultUnit</a><span class="type-signature"> : 'mm' | 'cm' | 'in' | 'pt' | 'pc' | 'px'</span> </h3>
  72. <div class="description">
  73. <p>Default unit.<br/>Default is <code>'px'</code>.</p>
  74. </div>
  75. </div>
  76. <h2 class="subsection-title">Methods</h2>
  77. <h3 class="name name-method" id="load" translate="no">.<a href="#load">load</a><span class="signature">( url : <span class="param-type">string</span>, onLoad : <span class="param-type">function</span>, onProgress : <span class="param-type">onProgressCallback</span>, onError : <span class="param-type">onErrorCallback</span> )</span> </h3>
  78. <div class="method">
  79. <div class="description">
  80. <p>Starts loading from the given URL and passes the loaded SVG asset
  81. to the <code>onLoad()</code> callback.</p>
  82. </div>
  83. <table class="params">
  84. <tbody>
  85. <tr>
  86. <td class="name"><code>url</code></td>
  87. <td class="description last"><p>The path/URL of the file to be loaded. This can also be a data URI.</p></td>
  88. </tr>
  89. <tr>
  90. <td class="name"><code>onLoad</code></td>
  91. <td class="description last"><p>Executed when the loading process has been finished.</p></td>
  92. </tr>
  93. <tr>
  94. <td class="name"><code>onProgress</code></td>
  95. <td class="description last"><p>Executed while the loading is in progress.</p></td>
  96. </tr>
  97. <tr>
  98. <td class="name"><code>onError</code></td>
  99. <td class="description last"><p>Executed when errors occur.</p></td>
  100. </tr>
  101. </tbody>
  102. </table>
  103. <dl class="details">
  104. <dt class="tag-overrides"><strong>Overrides:</strong> <a href="Loader.html#load">Loader#load</a></dt>
  105. </dl>
  106. </div>
  107. <h3 class="name name-method" id="parse" translate="no">.<a href="#parse">parse</a><span class="signature">( text : <span class="param-type">string</span> )</span><span class="type-signature"> : Object</span> </h3>
  108. <div class="method">
  109. <div class="description">
  110. <p>Parses the given SVG data and returns the resulting data.</p>
  111. </div>
  112. <table class="params">
  113. <tbody>
  114. <tr>
  115. <td class="name"><code>text</code></td>
  116. <td class="description last"><p>The raw SVG data as a string.</p></td>
  117. </tr>
  118. </tbody>
  119. </table>
  120. <dl class="details">
  121. <dt class="tag-overrides"><strong>Overrides:</strong> <a href="Loader.html#parse">Loader#parse</a></dt>
  122. </dl>
  123. <dl class="details">
  124. <dt class="tag-returns"><strong>Returns:</strong> An object holding an array of shape paths and the
  125. SVG XML document.</dt>
  126. </dl>
  127. </div>
  128. <h2 class="subsection-title">Static Methods</h2>
  129. <h3 class="name name-method" id=".createShapes" translate="no">.<a href="#.createShapes">createShapes</a><span class="signature">( shapePath : <span class="param-type">ShapePath</span> )</span><span class="type-signature"> : Array.&lt;<a href="Shape.html">Shape</a>></span> </h3>
  130. <div class="method">
  131. <div class="description">
  132. <p>Creates from the given shape path and array of shapes.</p>
  133. </div>
  134. <table class="params">
  135. <tbody>
  136. <tr>
  137. <td class="name"><code>shapePath</code></td>
  138. <td class="description last"><p>The shape path.</p></td>
  139. </tr>
  140. </tbody>
  141. </table>
  142. <dl class="details">
  143. <dt class="tag-returns"><strong>Returns:</strong> An array of shapes.</dt>
  144. </dl>
  145. </div>
  146. <h3 class="name name-method" id=".getStrokeStyle" translate="no">.<a href="#.getStrokeStyle">getStrokeStyle</a><span class="signature">( width : <span class="param-type">number</span>, color : <span class="param-type">string</span>, lineJoin : <span class="param-type">'round' | 'bevel' | 'miter' | 'miter-limit'</span>, lineCap : <span class="param-type">'round' | 'square' | 'butt'</span>, miterLimit : <span class="param-type">number</span> )</span><span class="type-signature"> : Object</span> </h3>
  147. <div class="method">
  148. <div class="description">
  149. <p>Returns a stroke style object from the given parameters.</p>
  150. </div>
  151. <table class="params">
  152. <tbody>
  153. <tr>
  154. <td class="name"><code>width</code></td>
  155. <td class="description last"><p>The stroke width.<br/>Default is <code>1</code>.</p></td>
  156. </tr>
  157. <tr>
  158. <td class="name"><code>color</code></td>
  159. <td class="description last"><p>The stroke color, as returned by <a href="Color.html#getStyle">Color#getStyle</a>.<br/>Default is <code>'#000'</code>.</p></td>
  160. </tr>
  161. <tr>
  162. <td class="name"><code>lineJoin</code></td>
  163. <td class="description last"><p>The line join style.<br/>Default is <code>'miter'</code>.</p></td>
  164. </tr>
  165. <tr>
  166. <td class="name"><code>lineCap</code></td>
  167. <td class="description last"><p>The line cap style.<br/>Default is <code>'butt'</code>.</p></td>
  168. </tr>
  169. <tr>
  170. <td class="name"><code>miterLimit</code></td>
  171. <td class="description last"><p>Maximum join length, in multiples of the <code>width</code> parameter (join is truncated if it exceeds that distance).<br/>Default is <code>4</code>.</p></td>
  172. </tr>
  173. </tbody>
  174. </table>
  175. <dl class="details">
  176. <dt class="tag-returns"><strong>Returns:</strong> The style object.</dt>
  177. </dl>
  178. </div>
  179. <h3 class="name name-method" id=".pointsToStroke" translate="no">.<a href="#.pointsToStroke">pointsToStroke</a><span class="signature">( points : <span class="param-type">Array.&lt;Vector2></span>, style : <span class="param-type">Object</span>, arcDivisions : <span class="param-type">number</span>, minDistance : <span class="param-type">number</span> )</span><span class="type-signature"> : <a href="BufferGeometry.html">BufferGeometry</a></span> </h3>
  180. <div class="method">
  181. <div class="description">
  182. <p>Creates a stroke from an array of points.</p>
  183. </div>
  184. <table class="params">
  185. <tbody>
  186. <tr>
  187. <td class="name"><code>points</code></td>
  188. <td class="description last"><p>The points in 2D space. Minimum 2 points. The path can be open or closed (last point equals to first point).</p></td>
  189. </tr>
  190. <tr>
  191. <td class="name"><code>style</code></td>
  192. <td class="description last"><p>Object with SVG properties as returned by <code>SVGLoader.getStrokeStyle()</code>, or <code>SVGLoader.parse()</code> in the <code>path.userData.style</code> object.</p></td>
  193. </tr>
  194. <tr>
  195. <td class="name"><code>arcDivisions</code></td>
  196. <td class="description last"><p>Arc divisions for round joins and endcaps.<br/>Default is <code>12</code>.</p></td>
  197. </tr>
  198. <tr>
  199. <td class="name"><code>minDistance</code></td>
  200. <td class="description last"><p>Points closer to this distance will be merged.<br/>Default is <code>0.001</code>.</p></td>
  201. </tr>
  202. </tbody>
  203. </table>
  204. <dl class="details">
  205. <dt class="tag-returns"><strong>Returns:</strong> The stroke geometry. UV coordinates are generated ('u' along path. 'v' across it, from left to right).
  206. Returns <code>null</code> if not geometry was generated.</dt>
  207. </dl>
  208. </div>
  209. <h3 class="name name-method" id=".pointsToStrokeWithBuffers" translate="no">.<a href="#.pointsToStrokeWithBuffers">pointsToStrokeWithBuffers</a><span class="signature">( points : <span class="param-type">Array.&lt;Vector2></span>, style : <span class="param-type">Object</span>, arcDivisions : <span class="param-type">number</span>, minDistance : <span class="param-type">number</span>, vertices : <span class="param-type">Array.&lt;number></span>, normals : <span class="param-type">Array.&lt;number></span>, uvs : <span class="param-type">Array.&lt;number></span>, vertexOffset : <span class="param-type">number</span> )</span><span class="type-signature"> : number</span> </h3>
  210. <div class="method">
  211. <div class="description">
  212. <p>Creates a stroke from an array of points.</p>
  213. </div>
  214. <table class="params">
  215. <tbody>
  216. <tr>
  217. <td class="name"><code>points</code></td>
  218. <td class="description last"><p>The points in 2D space. Minimum 2 points.</p></td>
  219. </tr>
  220. <tr>
  221. <td class="name"><code>style</code></td>
  222. <td class="description last"><p>Object with SVG properties as returned by <code>SVGLoader.getStrokeStyle()</code>, or <code>SVGLoader.parse()</code> in the <code>path.userData.style</code> object.</p></td>
  223. </tr>
  224. <tr>
  225. <td class="name"><code>arcDivisions</code></td>
  226. <td class="description last"><p>Arc divisions for round joins and endcaps.<br/>Default is <code>12</code>.</p></td>
  227. </tr>
  228. <tr>
  229. <td class="name"><code>minDistance</code></td>
  230. <td class="description last"><p>Points closer to this distance will be merged.<br/>Default is <code>0.001</code>.</p></td>
  231. </tr>
  232. <tr>
  233. <td class="name"><code>vertices</code></td>
  234. <td class="description last"><p>An array holding vertices.</p></td>
  235. </tr>
  236. <tr>
  237. <td class="name"><code>normals</code></td>
  238. <td class="description last"><p>An array holding normals.</p></td>
  239. </tr>
  240. <tr>
  241. <td class="name"><code>uvs</code></td>
  242. <td class="description last"><p>An array holding uvs.</p></td>
  243. </tr>
  244. <tr>
  245. <td class="name"><code>vertexOffset</code></td>
  246. <td class="description last"><p>The vertex offset.<br/>Default is <code>0</code>.</p></td>
  247. </tr>
  248. </tbody>
  249. </table>
  250. <dl class="details">
  251. <dt class="tag-returns"><strong>Returns:</strong> The number of vertices.</dt>
  252. </dl>
  253. </div>
  254. <h2 class="subsection-title">Source</h2>
  255. <p>
  256. <a href="https://github.com/mrdoob/three.js/blob/master/examples/jsm/loaders/SVGLoader.js" target="_blank" rel="noopener" translate="no">examples/jsm/loaders/SVGLoader.js</a>
  257. </p>
  258. </article>
  259. </section>
  260. <script src="../scripts/linenumber.js"></script>
  261. <script src="../scripts/page.js"></script>
  262. </body>
  263. </html>
粤ICP备19079148号