SVGLoader.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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" rel="noopener">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"><a href="LoadingManager.html">LoadingManager</a></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">
  57. <strong>manager</strong>
  58. </td>
  59. <td class="description last">
  60. <p>The loading manager.</p>
  61. </td>
  62. </tr>
  63. </tbody>
  64. </table>
  65. </div>
  66. </div>
  67. <h2 class="subsection-title">Properties</h2>
  68. <div class="member">
  69. <h3 class="name" id="defaultDPI" translate="no">.<a href="#defaultDPI">defaultDPI</a><span class="type-signature"> : number</span> </h3>
  70. <div class="description">
  71. <p>Default dots per inch.</p>
  72. <p>Default is <code>90</code>.</p>
  73. </div>
  74. </div>
  75. <div class="member">
  76. <h3 class="name" id="defaultUnit" translate="no">.<a href="#defaultUnit">defaultUnit</a><span class="type-signature"> : 'mm' | 'cm' | 'in' | 'pt' | 'pc' | 'px'</span> </h3>
  77. <div class="description">
  78. <p>Default unit.</p>
  79. <p>Default is <code>'px'</code>.</p>
  80. </div>
  81. </div>
  82. <h2 class="subsection-title">Methods</h2>
  83. <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"><a href="global.html#onProgressCallback">onProgressCallback</a></span>, onError : <span class="param-type"><a href="global.html#onErrorCallback">onErrorCallback</a></span> )</span> </h3>
  84. <div class="method">
  85. <div class="description">
  86. <p>Starts loading from the given URL and passes the loaded SVG asset
  87. to the <code>onLoad()</code> callback.</p>
  88. </div>
  89. <table class="params">
  90. <tbody>
  91. <tr>
  92. <td class="name">
  93. <strong>url</strong>
  94. </td>
  95. <td class="description last">
  96. <p>The path/URL of the file to be loaded. This can also be a data URI.</p>
  97. </td>
  98. </tr>
  99. <tr>
  100. <td class="name">
  101. <strong>onLoad</strong>
  102. </td>
  103. <td class="description last">
  104. <p>Executed when the loading process has been finished.</p>
  105. </td>
  106. </tr>
  107. <tr>
  108. <td class="name">
  109. <strong>onProgress</strong>
  110. </td>
  111. <td class="description last">
  112. <p>Executed while the loading is in progress.</p>
  113. </td>
  114. </tr>
  115. <tr>
  116. <td class="name">
  117. <strong>onError</strong>
  118. </td>
  119. <td class="description last">
  120. <p>Executed when errors occur.</p>
  121. </td>
  122. </tr>
  123. </tbody>
  124. </table>
  125. <dl class="details">
  126. <dt class="tag-overrides"><strong>Overrides:</strong> <a href="Loader.html#load">Loader#load</a></dt>
  127. </dl>
  128. </div>
  129. <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>
  130. <div class="method">
  131. <div class="description">
  132. <p>Parses the given SVG data and returns the resulting data.</p>
  133. </div>
  134. <table class="params">
  135. <tbody>
  136. <tr>
  137. <td class="name">
  138. <strong>text</strong>
  139. </td>
  140. <td class="description last">
  141. <p>The raw SVG data as a string.</p>
  142. </td>
  143. </tr>
  144. </tbody>
  145. </table>
  146. <dl class="details">
  147. <dt class="tag-overrides"><strong>Overrides:</strong> <a href="Loader.html#parse">Loader#parse</a></dt>
  148. </dl>
  149. <dl class="details">
  150. <dt class="tag-returns"><strong>Returns:</strong> An object holding an array of shape paths and the
  151. SVG XML document.</dt>
  152. </dl>
  153. </div>
  154. <h2 class="subsection-title">Static Methods</h2>
  155. <h3 class="name name-method" id=".createShapes" translate="no">.<a href="#.createShapes">createShapes</a><span class="signature">( shapePath : <span class="param-type"><a href="ShapePath.html">ShapePath</a></span> )</span><span class="type-signature"> : Array.&lt;<a href="Shape.html">Shape</a>></span> </h3>
  156. <div class="method">
  157. <div class="description">
  158. <p>Creates from the given shape path and array of shapes.</p>
  159. </div>
  160. <table class="params">
  161. <tbody>
  162. <tr>
  163. <td class="name">
  164. <strong>shapePath</strong>
  165. </td>
  166. <td class="description last">
  167. <p>The shape path.</p>
  168. </td>
  169. </tr>
  170. </tbody>
  171. </table>
  172. <dl class="details">
  173. <dt class="tag-returns"><strong>Returns:</strong> An array of shapes.</dt>
  174. </dl>
  175. </div>
  176. <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>
  177. <div class="method">
  178. <div class="description">
  179. <p>Returns a stroke style object from the given parameters.</p>
  180. </div>
  181. <table class="params">
  182. <tbody>
  183. <tr>
  184. <td class="name">
  185. <strong>width</strong>
  186. </td>
  187. <td class="description last">
  188. <p>The stroke width.</p>
  189. <p>Default is <code>1</code>.</p>
  190. </td>
  191. </tr>
  192. <tr>
  193. <td class="name">
  194. <strong>color</strong>
  195. </td>
  196. <td class="description last">
  197. <p>The stroke color, as returned by <a href="Color.html#getStyle">Color#getStyle</a>.</p>
  198. <p>Default is <code>'#000'</code>.</p>
  199. </td>
  200. </tr>
  201. <tr>
  202. <td class="name">
  203. <strong>lineJoin</strong>
  204. </td>
  205. <td class="description last">
  206. <p>The line join style.</p>
  207. <p>Default is <code>'miter'</code>.</p>
  208. </td>
  209. </tr>
  210. <tr>
  211. <td class="name">
  212. <strong>lineCap</strong>
  213. </td>
  214. <td class="description last">
  215. <p>The line cap style.</p>
  216. <p>Default is <code>'butt'</code>.</p>
  217. </td>
  218. </tr>
  219. <tr>
  220. <td class="name">
  221. <strong>miterLimit</strong>
  222. </td>
  223. <td class="description last">
  224. <p>Maximum join length, in multiples of the <code>width</code> parameter (join is truncated if it exceeds that distance).</p>
  225. <p>Default is <code>4</code>.</p>
  226. </td>
  227. </tr>
  228. </tbody>
  229. </table>
  230. <dl class="details">
  231. <dt class="tag-returns"><strong>Returns:</strong> The style object.</dt>
  232. </dl>
  233. </div>
  234. <h3 class="name name-method" id=".pointsToStroke" translate="no">.<a href="#.pointsToStroke">pointsToStroke</a><span class="signature">( points : <span class="param-type">Array.&lt;<a href="Vector2.html">Vector2</a>></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>
  235. <div class="method">
  236. <div class="description">
  237. <p>Creates a stroke from an array of points.</p>
  238. </div>
  239. <table class="params">
  240. <tbody>
  241. <tr>
  242. <td class="name">
  243. <strong>points</strong>
  244. </td>
  245. <td class="description last">
  246. <p>The points in 2D space. Minimum 2 points. The path can be open or closed (last point equals to first point).</p>
  247. </td>
  248. </tr>
  249. <tr>
  250. <td class="name">
  251. <strong>style</strong>
  252. </td>
  253. <td class="description last">
  254. <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>
  255. </td>
  256. </tr>
  257. <tr>
  258. <td class="name">
  259. <strong>arcDivisions</strong>
  260. </td>
  261. <td class="description last">
  262. <p>Arc divisions for round joins and endcaps.</p>
  263. <p>Default is <code>12</code>.</p>
  264. </td>
  265. </tr>
  266. <tr>
  267. <td class="name">
  268. <strong>minDistance</strong>
  269. </td>
  270. <td class="description last">
  271. <p>Points closer to this distance will be merged.</p>
  272. <p>Default is <code>0.001</code>.</p>
  273. </td>
  274. </tr>
  275. </tbody>
  276. </table>
  277. <dl class="details">
  278. <dt class="tag-returns"><strong>Returns:</strong> The stroke geometry. UV coordinates are generated ('u' along path. 'v' across it, from left to right).
  279. Returns <code>null</code> if not geometry was generated.</dt>
  280. </dl>
  281. </div>
  282. <h3 class="name name-method" id=".pointsToStrokeWithBuffers" translate="no">.<a href="#.pointsToStrokeWithBuffers">pointsToStrokeWithBuffers</a><span class="signature">( points : <span class="param-type">Array.&lt;<a href="Vector2.html">Vector2</a>></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>
  283. <div class="method">
  284. <div class="description">
  285. <p>Creates a stroke from an array of points.</p>
  286. </div>
  287. <table class="params">
  288. <tbody>
  289. <tr>
  290. <td class="name">
  291. <strong>points</strong>
  292. </td>
  293. <td class="description last">
  294. <p>The points in 2D space. Minimum 2 points.</p>
  295. </td>
  296. </tr>
  297. <tr>
  298. <td class="name">
  299. <strong>style</strong>
  300. </td>
  301. <td class="description last">
  302. <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>
  303. </td>
  304. </tr>
  305. <tr>
  306. <td class="name">
  307. <strong>arcDivisions</strong>
  308. </td>
  309. <td class="description last">
  310. <p>Arc divisions for round joins and endcaps.</p>
  311. <p>Default is <code>12</code>.</p>
  312. </td>
  313. </tr>
  314. <tr>
  315. <td class="name">
  316. <strong>minDistance</strong>
  317. </td>
  318. <td class="description last">
  319. <p>Points closer to this distance will be merged.</p>
  320. <p>Default is <code>0.001</code>.</p>
  321. </td>
  322. </tr>
  323. <tr>
  324. <td class="name">
  325. <strong>vertices</strong>
  326. </td>
  327. <td class="description last">
  328. <p>An array holding vertices.</p>
  329. </td>
  330. </tr>
  331. <tr>
  332. <td class="name">
  333. <strong>normals</strong>
  334. </td>
  335. <td class="description last">
  336. <p>An array holding normals.</p>
  337. </td>
  338. </tr>
  339. <tr>
  340. <td class="name">
  341. <strong>uvs</strong>
  342. </td>
  343. <td class="description last">
  344. <p>An array holding uvs.</p>
  345. </td>
  346. </tr>
  347. <tr>
  348. <td class="name">
  349. <strong>vertexOffset</strong>
  350. </td>
  351. <td class="description last">
  352. <p>The vertex offset.</p>
  353. <p>Default is <code>0</code>.</p>
  354. </td>
  355. </tr>
  356. </tbody>
  357. </table>
  358. <dl class="details">
  359. <dt class="tag-returns"><strong>Returns:</strong> The number of vertices.</dt>
  360. </dl>
  361. </div>
  362. <h2 class="subsection-title">Source</h2>
  363. <p>
  364. <a href="https://github.com/mrdoob/three.js/blob/master/examples/jsm/loaders/SVGLoader.js" translate="no" target="_blank" rel="noopener">examples/jsm/loaders/SVGLoader.js</a>
  365. </p>
  366. </article>
  367. </section>
  368. <script src="../scripts/linenumber.js"></script>
  369. <script src="../scripts/page.js"></script>
  370. </body>
  371. </html>
粤ICP备19079148号