FlakesTexture.js 912 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. class FlakesTexture {
  5. constructor( width = 512, height = 512 ) {
  6. var canvas = document.createElement( 'canvas' );
  7. canvas.width = width;
  8. canvas.height = height;
  9. var context = canvas.getContext( '2d' );
  10. context.fillStyle = 'rgb(127,127,255)';
  11. context.fillRect( 0, 0, width, height );
  12. for ( var i = 0; i < 4000; i ++ ) {
  13. var x = Math.random() * width;
  14. var y = Math.random() * height;
  15. var r = Math.random() * 3 + 3;
  16. var nx = Math.random() * 2 - 1;
  17. var ny = Math.random() * 2 - 1;
  18. var nz = 1.5;
  19. var l = Math.sqrt( nx * nx + ny * ny + nz * nz );
  20. nx /= l; ny /= l; nz /= l;
  21. context.fillStyle = 'rgb(' + ( nx * 127 + 127 ) + ',' + ( ny * 127 + 127 ) + ',' + ( nz * 255 ) + ')';
  22. context.beginPath();
  23. context.arc( x, y, r, 0, Math.PI * 2 );
  24. context.fill();
  25. }
  26. return canvas;
  27. }
  28. }
  29. export { FlakesTexture };