Advertisement
XiaYuanOwO

Fish.js

Mar 17th, 2023
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var RENDERER = {
  2.     POINT_INTERVAL : 5,
  3.     FISH_COUNT : 3,
  4.     MAX_INTERVAL_COUNT : 50,
  5.     INIT_HEIGHT_RATE : 0.5,
  6.     THRESHOLD : 50,
  7.    
  8.     init : function(){
  9.         this.setParameters();
  10.         this.reconstructMethods();
  11.         this.setup();
  12.         this.bindEvent();
  13.         this.render();
  14.     },
  15.     setParameters : function(){
  16.         this.$window = $(window);
  17.         this.$container = $('#flyfish');
  18.         this.$canvas = $('<canvas />');
  19.         this.context = this.$canvas.appendTo(this.$container).get(0).getContext('2d');
  20.         this.points = [];
  21.         this.fishes = [];
  22.         this.watchIds = [];
  23.     },
  24.     createSurfacePoints : function(){
  25.         var count = Math.round(this.width / this.POINT_INTERVAL);
  26.         this.pointInterval = this.width / (count - 1);
  27.         this.points.push(new SURFACE_POINT(this, 0));
  28.        
  29.         for(var i = 1; i < count; i++){
  30.             var point = new SURFACE_POINT(this, i * this.pointInterval),
  31.                 previous = this.points[i - 1];
  32.                
  33.             point.setPreviousPoint(previous);
  34.             previous.setNextPoint(point);
  35.             this.points.push(point);
  36.         }
  37.     },
  38.     reconstructMethods : function(){
  39.         this.watchWindowSize = this.watchWindowSize.bind(this);
  40.         this.jdugeToStopResize = this.jdugeToStopResize.bind(this);
  41.         this.startEpicenter = this.startEpicenter.bind(this);
  42.         this.moveEpicenter = this.moveEpicenter.bind(this);
  43.         this.render = this.render.bind(this);
  44.     },
  45.     setup : function(){
  46.         this.points.length = 0;
  47.         this.fishes.length = 0;
  48.         this.watchIds.length = 0;
  49.         this.intervalCount = this.MAX_INTERVAL_COUNT;
  50.         this.width = this.$container.width();
  51.         this.height = this.$container.height();
  52.         this.fishCount = this.FISH_COUNT * this.width / 500 * this.height / 500;
  53.         this.$canvas.attr({width : this.width, height : this.height});
  54.         this.reverse = false;
  55.        
  56.         this.fishes.push(new FISH(this));
  57.         this.createSurfacePoints();
  58.     },
  59.     watchWindowSize : function(){
  60.         this.clearTimer();
  61.         this.tmpWidth = this.$window.width();
  62.         this.tmpHeight = this.$window.height();
  63.         this.watchIds.push(setTimeout(this.jdugeToStopResize, this.WATCH_INTERVAL));
  64.     },
  65.     clearTimer : function(){
  66.         while(this.watchIds.length > 0){
  67.             clearTimeout(this.watchIds.pop());
  68.         }
  69.     },
  70.     jdugeToStopResize : function(){
  71.         var width = this.$window.width(),
  72.             height = this.$window.height(),
  73.             stopped = (width == this.tmpWidth && height == this.tmpHeight);
  74.            
  75.         this.tmpWidth = width;
  76.         this.tmpHeight = height;
  77.        
  78.         if(stopped){
  79.             this.setup();
  80.         }
  81.     },
  82.     bindEvent : function(){
  83.         this.$window.on('resize', this.watchWindowSize);
  84.         this.$container.on('mouseenter', this.startEpicenter);
  85.         this.$container.on('mousemove', this.moveEpicenter);
  86.     },
  87.     getAxis : function(event){
  88.         var offset = this.$container.offset();
  89.        
  90.         return {
  91.             x : event.clientX - offset.left + this.$window.scrollLeft(),
  92.             y : event.clientY - offset.top + this.$window.scrollTop()
  93.         };
  94.     },
  95.     startEpicenter : function(event){
  96.         this.axis = this.getAxis(event);
  97.     },
  98.     moveEpicenter : function(event){
  99.         var axis = this.getAxis(event);
  100.        
  101.         if(!this.axis){
  102.             this.axis = axis;
  103.         }
  104.         this.generateEpicenter(axis.x, axis.y, axis.y - this.axis.y);
  105.         this.axis = axis;
  106.     },
  107.     generateEpicenter : function(x, y, velocity){
  108.         if(y < this.height / 2 - this.THRESHOLD || y > this.height / 2 + this.THRESHOLD){
  109.             return;
  110.         }
  111.         var index = Math.round(x / this.pointInterval);
  112.        
  113.         if(index < 0 || index >= this.points.length){
  114.             return;
  115.         }
  116.         this.points[index].interfere(y, velocity);
  117.     },
  118.     controlStatus : function(){
  119.         for(var i = 0, count = this.points.length; i < count; i++){
  120.             this.points[i].updateSelf();
  121.         }
  122.         for(var i = 0, count = this.points.length; i < count; i++){
  123.             this.points[i].updateNeighbors();
  124.         }
  125.         if(this.fishes.length < this.fishCount){
  126.             if(--this.intervalCount == 0){
  127.                 this.intervalCount = this.MAX_INTERVAL_COUNT;
  128.                 this.fishes.push(new FISH(this));
  129.             }
  130.         }
  131.     },
  132.     render : function(){
  133.         requestAnimationFrame(this.render);
  134.         this.controlStatus();
  135.         this.context.clearRect(0, 0, this.width, this.height);
  136.         this.context.fillStyle = 'hsl(0, 0%, 95%)';
  137.        
  138.         for(var i = 0, count = this.fishes.length; i < count; i++){
  139.             this.fishes[i].render(this.context);
  140.         }
  141.         this.context.save();
  142.         this.context.globalCompositeOperation = 'xor';
  143.         this.context.beginPath();
  144.         this.context.moveTo(0, this.reverse ? 0 : this.height);
  145.        
  146.         for(var i = 0, count = this.points.length; i < count; i++){
  147.             this.points[i].render(this.context);
  148.         }
  149.         this.context.lineTo(this.width, this.reverse ? 0 : this.height);
  150.         this.context.closePath();
  151.         this.context.fill();
  152.         this.context.restore();
  153.     }
  154. };
  155. var SURFACE_POINT = function(renderer, x){
  156.     this.renderer = renderer;
  157.     this.x = x;
  158.     this.init();
  159. };
  160. SURFACE_POINT.prototype = {
  161.     SPRING_CONSTANT : 0.03,
  162.     SPRING_FRICTION : 0.9,
  163.     WAVE_SPREAD : 0.3,
  164.     ACCELARATION_RATE : 0.01,
  165.    
  166.     init : function(){
  167.         this.initHeight = this.renderer.height * this.renderer.INIT_HEIGHT_RATE;
  168.         this.height = this.initHeight;
  169.         this.fy = 0;
  170.         this.force = {previous : 0, next : 0};
  171.     },
  172.     setPreviousPoint : function(previous){
  173.         this.previous = previous;
  174.     },
  175.     setNextPoint : function(next){
  176.         this.next = next;
  177.     },
  178.     interfere : function(y, velocity){
  179.         this.fy = this.renderer.height * this.ACCELARATION_RATE * ((this.renderer.height - this.height - y) >= 0 ? -1 : 1) * Math.abs(velocity);
  180.     },
  181.     updateSelf : function(){
  182.         this.fy += this.SPRING_CONSTANT * (this.initHeight - this.height);
  183.         this.fy *= this.SPRING_FRICTION;
  184.         this.height += this.fy;
  185.     },
  186.     updateNeighbors : function(){
  187.         if(this.previous){
  188.             this.force.previous = this.WAVE_SPREAD * (this.height - this.previous.height);
  189.         }
  190.         if(this.next){
  191.             this.force.next = this.WAVE_SPREAD * (this.height - this.next.height);
  192.         }
  193.     },
  194.     render : function(context){
  195.         if(this.previous){
  196.             this.previous.height += this.force.previous;
  197.             this.previous.fy += this.force.previous;
  198.         }
  199.         if(this.next){
  200.             this.next.height += this.force.next;
  201.             this.next.fy += this.force.next;
  202.         }
  203.         context.lineTo(this.x, this.renderer.height - this.height);
  204.     }
  205. };
  206. var FISH = function(renderer){
  207.     this.renderer = renderer;
  208.     this.init();
  209. };
  210. FISH.prototype = {
  211.     GRAVITY : 0.4,
  212.    
  213.     init : function(){
  214.         this.direction = Math.random() < 0.5;
  215.         this.x = this.direction ? (this.renderer.width + this.renderer.THRESHOLD) : -this.renderer.THRESHOLD;
  216.         this.previousY = this.y;
  217.         this.vx = this.getRandomValue(4, 10) * (this.direction ? -1 : 1);
  218.        
  219.         if(this.renderer.reverse){
  220.             this.y = this.getRandomValue(this.renderer.height * 1 / 10, this.renderer.height * 4 / 10);
  221.             this.vy = this.getRandomValue(2, 5);
  222.             this.ay = this.getRandomValue(0.05, 0.2);
  223.         }else{
  224.             this.y = this.getRandomValue(this.renderer.height * 6 / 10, this.renderer.height * 9 / 10);
  225.             this.vy = this.getRandomValue(-5, -2);
  226.             this.ay = this.getRandomValue(-0.2, -0.05);
  227.         }
  228.         this.isOut = false;
  229.         this.theta = 0;
  230.         this.phi = 0;
  231.     },
  232.     getRandomValue : function(min, max){
  233.         return min + (max - min) * Math.random();
  234.     },
  235.     controlStatus : function(context){
  236.         this.previousY = this.y;
  237.         this.x += this.vx;
  238.         this.y += this.vy;
  239.         this.vy += this.ay;
  240.        
  241.         if(this.renderer.reverse){
  242.             if(this.y > this.renderer.height * this.renderer.INIT_HEIGHT_RATE){
  243.                 this.vy -= this.GRAVITY;
  244.                 this.isOut = true;
  245.             }else{
  246.                 if(this.isOut){
  247.                     this.ay = this.getRandomValue(0.05, 0.2);
  248.                 }
  249.                 this.isOut = false;
  250.             }
  251.         }else{
  252.             if(this.y < this.renderer.height * this.renderer.INIT_HEIGHT_RATE){
  253.                 this.vy += this.GRAVITY;
  254.                 this.isOut = true;
  255.             }else{
  256.                 if(this.isOut){
  257.                     this.ay = this.getRandomValue(-0.2, -0.05);
  258.                 }
  259.                 this.isOut = false;
  260.             }
  261.         }
  262.         if(!this.isOut){
  263.             this.theta += Math.PI / 20;
  264.             this.theta %= Math.PI * 2;
  265.             this.phi += Math.PI / 30;
  266.             this.phi %= Math.PI * 2;
  267.         }
  268.         this.renderer.generateEpicenter(this.x + (this.direction ? -1 : 1) * this.renderer.THRESHOLD, this.y, this.y - this.previousY);
  269.        
  270.         if(this.vx > 0 && this.x > this.renderer.width + this.renderer.THRESHOLD || this.vx < 0 && this.x < -this.renderer.THRESHOLD){
  271.             this.init();
  272.         }
  273.     },
  274.     render : function(context){
  275.         context.save();
  276.         context.translate(this.x, this.y);
  277.         context.rotate(Math.PI + Math.atan2(this.vy, this.vx));
  278.         context.scale(1, this.direction ? 1 : -1);
  279.         context.beginPath();
  280.         context.moveTo(-30, 0);
  281.         context.bezierCurveTo(-20, 15, 15, 10, 40, 0);
  282.         context.bezierCurveTo(15, -10, -20, -15, -30, 0);
  283.         context.fill();
  284.        
  285.         context.save();
  286.         context.translate(40, 0);
  287.         context.scale(0.9 + 0.2 * Math.sin(this.theta), 1);
  288.         context.beginPath();
  289.         context.moveTo(0, 0);
  290.         context.quadraticCurveTo(5, 10, 20, 8);
  291.         context.quadraticCurveTo(12, 5, 10, 0);
  292.         context.quadraticCurveTo(12, -5, 20, -8);
  293.         context.quadraticCurveTo(5, -10, 0, 0);
  294.         context.fill();
  295.         context.restore();
  296.        
  297.         context.save();
  298.         context.translate(-3, 0);
  299.         context.rotate((Math.PI / 3 + Math.PI / 10 * Math.sin(this.phi)) * (this.renderer.reverse ? -1 : 1));
  300.        
  301.         context.beginPath();
  302.        
  303.         if(this.renderer.reverse){
  304.             context.moveTo(5, 0);
  305.             context.bezierCurveTo(10, 10, 10, 30, 0, 40);
  306.             context.bezierCurveTo(-12, 25, -8, 10, 0, 0);
  307.         }else{
  308.             context.moveTo(-5, 0);
  309.             context.bezierCurveTo(-10, -10, -10, -30, 0, -40);
  310.             context.bezierCurveTo(12, -25, 8, -10, 0, 0);
  311.         }
  312.         context.closePath();
  313.         context.fill();
  314.         context.restore();
  315.         context.restore();
  316.         this.controlStatus(context);
  317.     }
  318. };
  319. window.onload = function(){
  320.     RENDERER.init();
  321. };
  322.  
  323.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement