svg_lines.html 2.6 KB

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