svg_lines.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. svg {
  10. display: block;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <div id="info">
  16. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> svg - lines
  17. </div>
  18. <!-- Import maps polyfill -->
  19. <!-- Remove this when import maps will be widely supported -->
  20. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  21. <script type="importmap">
  22. {
  23. "imports": {
  24. "three": "../build/three.module.js"
  25. }
  26. }
  27. </script>
  28. <script type="module">
  29. import * as THREE from 'three';
  30. import { SVGRenderer } from './jsm/renderers/SVGRenderer.js';
  31. let camera, scene, renderer;
  32. init();
  33. animate();
  34. function init() {
  35. camera = new THREE.PerspectiveCamera( 33, window.innerWidth / window.innerHeight, 0.1, 100 );
  36. camera.position.z = 10;
  37. scene = new THREE.Scene();
  38. scene.background = new THREE.Color( 0, 0, 0 );
  39. renderer = new SVGRenderer();
  40. renderer.setSize( window.innerWidth, window.innerHeight );
  41. document.body.appendChild( renderer.domElement );
  42. //
  43. const vertices = [];
  44. const divisions = 50;
  45. for ( let i = 0; i <= divisions; i ++ ) {
  46. const v = ( i / divisions ) * ( Math.PI * 2 );
  47. const x = Math.sin( v );
  48. const z = Math.cos( v );
  49. vertices.push( x, 0, z );
  50. }
  51. const geometry = new THREE.BufferGeometry();
  52. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  53. //
  54. for ( let i = 1; i <= 3; i ++ ) {
  55. const material = new THREE.LineBasicMaterial( {
  56. color: Math.random() * 0xffffff,
  57. linewidth: 10
  58. } );
  59. const line = new THREE.Line( geometry, material );
  60. line.scale.setScalar( i / 3 );
  61. scene.add( line );
  62. }
  63. const material = new THREE.LineDashedMaterial( {
  64. color: 'blue',
  65. linewidth: 1,
  66. dashSize: 10,
  67. gapSize: 10
  68. } );
  69. const line = new THREE.Line( geometry, material );
  70. line.scale.setScalar( 2 );
  71. scene.add( line );
  72. //
  73. window.addEventListener( 'resize', onWindowResize );
  74. }
  75. function onWindowResize() {
  76. camera.aspect = window.innerWidth / window.innerHeight;
  77. camera.updateProjectionMatrix();
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. }
  80. function animate() {
  81. let count = 0;
  82. const time = performance.now() / 1000;
  83. scene.traverse( function ( child ) {
  84. child.rotation.x = count + ( time / 3 );
  85. child.rotation.z = count + ( time / 4 );
  86. count ++;
  87. } );
  88. renderer.render( scene, camera );
  89. requestAnimationFrame( animate );
  90. }
  91. </script>
  92. </body>
  93. </html>