You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.0 KiB

1 year ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. var w = dotty.width = window.innerWidth,
  2. h = dotty.height = window.innerHeight,
  3. sum = w + h,
  4. ctx = dotty.getContext( '2d' ),
  5. opts = {
  6. side: 15,
  7. picksParTick: 2,
  8. baseTime: 40,
  9. addedTime: 10,
  10. colors: [ 'rgba(49,56,141,alp)', 'rgba(254,8,97,alp)', 'rgba(253,255,86,alp)' ],
  11. addedAlpha: 20,
  12. strokeColor: 'rgb(43,45,53)',
  13. hueSpeed: .2,
  14. repaintAlpha: 1
  15. },
  16. difX = Math.sqrt( 3 ) * opts.side / 2, // height of a equilateral triangle
  17. difY = opts.side * 3 / 2, // side of a triangle ( because it goes down to a vertex ) then half a side of the triangle in the hex below: s + s/2 = s*3/2
  18. rad = Math.PI / 6, // TAU / 6 = PI / 3 I thought, but apparently this way works better
  19. cos = Math.cos( rad ) * opts.side,
  20. sin = Math.sin( rad ) * opts.side,
  21. hexs = [],
  22. tick = 0;
  23. function loop(){
  24. window.requestAnimationFrame( loop );
  25. tick += opts.hueSpeed;
  26. ctx.shadowBlur = 0;
  27. ctx.fillStyle = 'rgba(32,35,45,alp)'.replace( 'alp', opts.repaintAlpha );
  28. ctx.fillRect( 0, 0, w, h );
  29. for( var i = 0; i < opts.picksParTick; ++i )
  30. hexs[ ( Math.random() * hexs.length ) |0 ].pick();
  31. hexs.map( function( hex ){ hex.step(); } );
  32. }
  33. function Hex( x, y ){
  34. this.x = x;
  35. this.y = y;
  36. this.sum = this.x + this.y;
  37. this.picked = false;
  38. this.time = 0;
  39. this.targetTime = 0;
  40. this.xs = [ this.x + cos, this.x, this.x - cos, this.x - cos, this.x, this.x + cos ];
  41. this.ys = [ this.y - sin, this.y - opts.side, this.y - sin, this.y + sin, this.y + opts.side, this.y + sin ];
  42. }
  43. Hex.prototype.pick = function(){
  44. this.color = opts.colors[ ( Math.random() * opts.colors.length ) |0 ];
  45. this.picked = true;
  46. this.time = this.time || 0;
  47. this.targetTime = this.targetTime || ( opts.baseTime + opts.addedTime * Math.random() ) |0;
  48. }
  49. Hex.prototype.step = function(){
  50. var prop = this.time / this.targetTime;
  51. ctx.beginPath();
  52. ctx.moveTo( this.xs[0], this.ys[0] );
  53. for( var i = 1; i < this.xs.length; ++i )
  54. ctx.lineTo( this.xs[i], this.ys[i] );
  55. ctx.lineTo( this.xs[0], this.ys[0] );
  56. if( this.picked ){
  57. ++this.time;
  58. if( this.time >= this.targetTime ){
  59. this.time = 0;
  60. this.targetTime = 0;
  61. this.picked = false;
  62. }
  63. ctx.fillStyle = ctx.shadowColor = this.color.replace( 'alp', Math.sin( prop * Math.PI ) );
  64. ctx.fill();
  65. } else {
  66. ctx.strokeStyle = ctx.shadowColor = opts.strokeColor;
  67. ctx.stroke();
  68. }
  69. }
  70. for( var x = 0; x < w; x += difX*2 ){
  71. var i = 0;
  72. for( var y = 0; y < h; y += difY ){
  73. ++i;
  74. hexs.push( new Hex( x + difX * ( i % 2 ), y ) );
  75. }
  76. }
  77. loop();
  78. window.addEventListener( 'resize', function(){
  79. w = dotty.width = window.innerWidth;
  80. h = dotty.height = window.innerHeight;
  81. sum = w + h;
  82. hexs.length = 0;
  83. for( var x = 0; x < w; x += difX*2 ){
  84. var i = 0;
  85. for( var y = 0; y < h; y += difY ){
  86. ++i;
  87. hexs.push( new Hex( x + difX * ( i % 2 ), y ) );
  88. }
  89. }
  90. })