FlakesTexture.js 876 B

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