svg_lines.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js svg - lines</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <meta property="og:title" content="three.js svg - lines">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/svg_lines.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/svg_lines.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <style>
  13. svg {
  14. display: block;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> svg - lines
  21. </div>
  22. <script type="importmap">
  23. {
  24. "imports": {
  25. "three": "../build/three.module.js",
  26. "three/addons/": "./jsm/"
  27. }
  28. }
  29. </script>
  30. <script type="module">
  31. import * as THREE from 'three';
  32. import { SVGRenderer } from 'three/addons/renderers/SVGRenderer.js';
  33. THREE.ColorManagement.enabled = false;
  34. let camera, scene, renderer;
  35. init();
  36. animate();
  37. function init() {
  38. camera = new THREE.PerspectiveCamera( 33, window.innerWidth / window.innerHeight, 0.1, 100 );
  39. camera.position.z = 10;
  40. scene = new THREE.Scene();
  41. scene.background = new THREE.Color( 0, 0, 0 );
  42. renderer = new SVGRenderer();
  43. renderer.setSize( window.innerWidth, window.innerHeight );
  44. document.body.appendChild( renderer.domElement );
  45. //
  46. const vertices = [];
  47. const divisions = 50;
  48. for ( let i = 0; i <= divisions; i ++ ) {
  49. const v = ( i / divisions ) * ( Math.PI * 2 );
  50. const x = Math.sin( v );
  51. const z = Math.cos( v );
  52. vertices.push( x, 0, z );
  53. }
  54. const geometry = new THREE.BufferGeometry();
  55. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  56. //
  57. for ( let i = 1; i <= 3; i ++ ) {
  58. const material = new THREE.LineBasicMaterial( {
  59. color: Math.random() * 0xffffff,
  60. linewidth: 10
  61. } );
  62. const line = new THREE.Line( geometry, material );
  63. line.scale.setScalar( i / 3 );
  64. scene.add( line );
  65. }
  66. const material = new THREE.LineDashedMaterial( {
  67. color: 'blue',
  68. linewidth: 1,
  69. dashSize: 10,
  70. gapSize: 10
  71. } );
  72. const line = new THREE.Line( geometry, material );
  73. line.scale.setScalar( 2 );
  74. scene.add( line );
  75. //
  76. window.addEventListener( 'resize', onWindowResize );
  77. }
  78. function onWindowResize() {
  79. camera.aspect = window.innerWidth / window.innerHeight;
  80. camera.updateProjectionMatrix();
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. }
  83. function animate() {
  84. let count = 0;
  85. const time = performance.now() / 1000;
  86. scene.traverse( function ( child ) {
  87. child.rotation.x = count + ( time / 3 );
  88. child.rotation.z = count + ( time / 4 );
  89. count ++;
  90. } );
  91. renderer.render( scene, camera );
  92. requestAnimationFrame( animate );
  93. }
  94. </script>
  95. </body>
  96. </html>
粤ICP备19079148号