Ver Mensaje Individual
  #2 (permalink)  
Antiguo 04/04/2012, 16:30
IXtremeLT
 
Fecha de Ingreso: julio-2011
Ubicación: Zapopan, Jal. MX
Mensajes: 316
Antigüedad: 12 años, 10 meses
Puntos: 32
Respuesta: Rendimiento muy lento en HTML5?

Ha, no me deja ponerlo arriba asi que aqui pongo el codigo.
Código Javascript:
Ver original
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" lang="es">
  3. <head>
  4.     <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  5.     <title></title>
  6.     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  7.     <script type="text/javascript">
  8.         $(function(){
  9.             var MyBalls = new balls("balls");
  10.             MyBalls.$canvas.resizable();
  11.             for( var i = 0; i < 10; i++ ) {
  12.                 MyBalls.create(
  13.                 (i+1) * 60,
  14.                 50,
  15.                 3 * (i+1),
  16.                 'rgb('+(100+parseInt(Math.random()*151))+', '+(100+parseInt(Math.random()*151))+', '+(100+parseInt(Math.random()*151))+')' );
  17.             }
  18.             setInterval(function() {
  19.                 $.each( MyBalls.balls, function(i_ball, ball) {
  20.                     ball.move();
  21.                 } );
  22.             }, 1);
  23.         });
  24.         var balls = function( id ) {
  25.             this.$canvas = $("#"+id);
  26.             this.context = this.$canvas[0].getContext("2d");
  27.             this.balls = [];
  28.         };
  29.         balls.prototype = {
  30.             create: function(CENTERX, CENTERY, RADIUS, COLOR, NO_NEW) {
  31.                 var Context = this.context;
  32.                 var This = this;
  33.                 Context.beginPath();
  34.                 Context.arc(CENTERX, CENTERY, RADIUS, 0, 2*Math.PI);
  35.                 Context.closePath();
  36.                 Context.strokeStyle = COLOR;
  37.                 Context.fillStyle = COLOR;
  38.                 Context.stroke();
  39.                 Context.fill();
  40.                 if ( NO_NEW ) {
  41.                     return;
  42.                 }
  43.                 var BallConstructor   = function() {
  44.                     this.centerX = CENTERX;
  45.                     this.centerY = CENTERY;
  46.                     this.radius  = RADIUS;
  47.                     this.color   = COLOR;
  48.                     this.dirX = 1;
  49.                     this.dirY = 1;
  50.                     this.speed= 1;
  51.                 };
  52.                 BallConstructor.prototype = {
  53.                     coords: function() {
  54.                         return {
  55.                             x1: this.centerX - this.radius,
  56.                             x2: this.centerX + this.radius,
  57.                             y1: this.centerY - this.radius,
  58.                             y2: this.centerY + this.radius,
  59.                             minX: 0,
  60.                             maxX: This.$canvas.width(),
  61.                             minY: 0,
  62.                             maxY: This.$canvas.height()
  63.                         }
  64.                     },
  65.                     move: function( SPEED, DIR ) {
  66.                         this.speed = SPEED || 1;
  67.                         this.dir = DIR || 1;
  68.                         var coords = this.coords();
  69.                         var axis = [
  70.                             ['x', 'X'],
  71.                             ['y', 'Y']
  72.                         ];
  73.                         for ( var i=0; i<2; i++ ) {
  74.                             var invert = false;
  75.                             var ax = axis[i][0];
  76.                             var AX = axis[i][1];
  77.                             var axis1 = coords[ax+'1'];
  78.                             var axis2 = coords[ax+'2'];
  79.                             var min_axis = coords['min'+AX];
  80.                             var max_axis = coords['max'+AX];
  81.                             var ThisBall = this;
  82.                             var prevcenter = this['center'+AX];
  83.                             var forward = this['dir'+AX] === 1 ? true : false;
  84.                             if ( axis1 - this.speed <= min_axis && !forward ) {
  85.                                 prevcenter = min_axis + this.radius;
  86.                                 invert = true;
  87.                             }
  88.                             if ( axis2 + this.speed >= max_axis && forward ) {
  89.                                 prevcenter = max_axis - this.radius;
  90.                                 invert = true;
  91.                             }
  92.                             $.each( This.balls, function(i_ball, that_ball) {
  93.                                 if ( ThisBall == that_ball ) {
  94.                                     return 'continue';
  95.                                 }
  96.                                 var that_coords = that_ball.coords();
  97.                                 var cond1 = coords.x1 - ThisBall.speed <= that_coords.x2;
  98.                                 var cond2 = coords.x2 + ThisBall.speed >= that_coords.x1;
  99.                                 var cond3 = coords.y1 - ThisBall.speed <= that_coords.y2;
  100.                                 var cond4 = coords.y2 + ThisBall.speed >= that_coords.y1;
  101.                                 if ( cond1 && cond2 && cond3 && cond4 ) {
  102.                                     invert = true;                                
  103.                                 }
  104.                             } );
  105.                             this['center'+AX] = prevcenter;
  106.                             if ( invert ) {
  107.                                 this['dir'+AX] *= -1;  
  108.                             }
  109.                             }
  110.                         this.centerX += this.speed * this.dirX;
  111.                         this.centerY += this.speed * this.dirY;
  112.                         This.clear();
  113.                        
  114.                         $.each( This.balls, function( i, ball ) {
  115.                             This.create(ball.centerX, ball.centerY, ball.radius, ball.color, true);
  116.                         });
  117.                     }
  118.                 };
  119.                 var BallObject = new BallConstructor;
  120.                 this.balls.push(BallObject);
  121.                 return BallObject;
  122.             },
  123.             clear: function() {
  124.                 this.context.clearRect(
  125.                     0, 0, this.$canvas.width(), this.$canvas.height()
  126.                 );
  127.             }
  128.         };
  129.     </script>
  130. </head>
  131. <body>
  132.     <canvas id="balls" width="800" height="400" style="border: 1px solid #000">
  133.    
  134.     </canvas>
  135. </body>
  136. </html>