Advertisement
SDL2

create box overlay in js

Jul 2nd, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class App {
  2.  
  3.   constructor() {
  4.    
  5.     this.randFrom = [
  6.       'first',
  7.       'last',
  8.       'center'
  9.     ];
  10.    
  11.     this.easing = [
  12.       'linear',
  13.       'easeInOutQuad',
  14.       'easeInOutCubic',
  15.       'easeInOutQuart',
  16.       'easeInOutQuint',
  17.       'easeInOutSine',
  18.       'easeInOutExpo',
  19.       'easeInOutCirc',
  20.       'easeInOutBack',
  21.       'cubicBezier(.5, .05, .1, .3)',
  22.       'spring(1, 80, 10, 0)',
  23.       'steps(10)'
  24.     ];
  25.    
  26.     this.randFromText = document.getElementById('randFrom');
  27.     this.randEasingText = document.getElementById('randEasing');
  28.   }
  29.  
  30.   init() {
  31.  
  32.       this.camera = new THREE.PerspectiveCamera( 25, window.innerWidth / window.innerHeight, 0.1, 1000 );
  33.       this.camera.position.x = -45;
  34.       this.camera.position.y = 30;
  35.       this.camera.position.z = -45;
  36.    
  37.       // this.controls = new THREE.OrbitControls(this.camera);
  38.       this.camera.lookAt(new THREE.Vector3(5,-5,5));
  39.  
  40.       this.scene = new THREE.Scene();
  41.    
  42.       this.resizeListener = e => this.onResize(e);
  43.         window.addEventListener( 'resize', this.resizeListener, false );
  44.    
  45.       this.createBoxes();
  46.  
  47.       this.renderer = new THREE.WebGLRenderer({
  48.         antialias: true
  49.       });
  50.  
  51.       this.renderer.setPixelRatio(window.devicePixelRatio);
  52.       this.renderer.setSize( window.innerWidth, window.innerHeight );
  53.  
  54.       document.body.appendChild( this.renderer.domElement );
  55.    
  56.       this.beginAnimationLoop();
  57.  
  58.       this.animate();
  59.   }
  60.  
  61.   createBoxes() {
  62.     this.geometry = new THREE.BoxBufferGeometry(1, 10, 1);
  63.    
  64.     let vertexShader = `
  65.       varying vec2 vUv;
  66.       void main()   {
  67.         vUv = uv;
  68.         gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
  69.       }
  70.     `;
  71.    
  72.     let fragmentShader = `
  73.       #extension GL_OES_standard_derivatives : enable
  74.  
  75.       varying vec2 vUv;
  76.       uniform float thickness;
  77.  
  78.       float edgeFactor(vec2 p){
  79.         vec2 grid = abs(fract(p - 0.5) - 0.5) / fwidth(p) / thickness;
  80.         return min(grid.x, grid.y);
  81.       }
  82.  
  83.       void main() {
  84.  
  85.         float a = edgeFactor(vUv);
  86.  
  87.         vec3 c = mix(vec3(1), vec3(0), a);
  88.  
  89.         gl_FragColor = vec4(c, 1.0);
  90.       }
  91.     `;
  92.    
  93.     let material = new THREE.ShaderMaterial({
  94.       uniforms: {
  95.         thickness: {
  96.           value: 1.5
  97.         }
  98.       },
  99.       vertexShader,
  100.       fragmentShader
  101.     });
  102.    
  103.     let cube = new THREE.Mesh( this.geometry, material );      
  104.    
  105.     let offset = 1.25;
  106.     this.nRows = 25;
  107.     this.nCols = 25;
  108.     this.staggerArray = [];
  109.    
  110.     for(var column = 0; column < this.nCols; column++) {
  111.       for(var row = 0; row < this.nRows; row++) {
  112.         let obj = cube.clone();
  113.         obj.position.x = (row * offset) - ((this.nRows*0.5) + (this.geometry.parameters.width*0.5));
  114.         obj.position.y = -(this.geometry.parameters.height*0.5);
  115.         obj.position.z = (column * offset) - ((this.nCols*0.5) + (this.geometry.parameters.width*0.5));
  116.         this.staggerArray.push(obj.position);
  117.         this.scene.add(obj);
  118.       }
  119.     }
  120.   }
  121.  
  122.   beginAnimationLoop() {
  123.      
  124.     // random from array
  125.     let randFrom = this.randFrom[Math.floor(Math.random()*this.randFrom.length)];
  126.     let easingString = this.easing[Math.floor(Math.random()*this.easing.length)];
  127.    
  128.     this.randFromText.textContent = randFrom;
  129.     this.randEasingText.textContent = easingString;
  130.    
  131.     anime({
  132.       targets: this.staggerArray,
  133.       y: [
  134.         {value: (this.geometry.parameters.height*0.25), duration: 500},
  135.         {value: -(this.geometry.parameters.height*0.25), duration: 2000},
  136.       ],
  137.       delay: anime.stagger(200, {grid: [this.nRows, this.nCols], from: randFrom}),
  138.       easing: easingString,
  139.       complete: (anim) => this.beginAnimationLoop()
  140.     });
  141.    
  142.   }
  143.  
  144.   animate() {
  145.  
  146.       requestAnimationFrame( () => this.animate() );
  147.       this.update();
  148.       this.render();
  149.   }
  150.  
  151.   update() {
  152.      
  153.     // update orbit controls
  154.     if(this.controls) {
  155.       this.controls.update();
  156.     }
  157.   }
  158.  
  159.   render() {
  160.     this.renderer.render( this.scene, this.camera );
  161.   }
  162.  
  163.   onResize() {
  164.  
  165.     // scene & camera update
  166.     this.camera.aspect = window.innerWidth / window.innerHeight;
  167.     this.camera.updateProjectionMatrix();
  168.     this.renderer.setSize( window.innerWidth, window.innerHeight );
  169.   }
  170.  
  171. }
  172.  
  173. let app = new App();
  174. app.init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement