Translate

Archive for December, 2010

How to convert Android GPS coordinates into X,Y coordinates.

Thursday, December 30th, 2010

Without further math bullshit about all the conversion systems, when you have a bunch of Android GPS coordinates (which are compatible with Google Earth and Google Maps), and you want to draw them on a finite 2D plane, here’s what worked for me.

int x =  (int) ((PLANE_WIDTH/360.0) * (180 + lon));
int y =  (int) ((PLANE_HEIGHT/180.0) * (90 - lat));
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Animating a game-like sky with HTML5 Canvas

Tuesday, December 28th, 2010


Try it | View Source

Again playing a little more with HTML5 and Canvas animation.

This time around the result is a little more pleasing to the eye, based on some ideas I have for a little arcade game I want to make I’ve created a gradient blue sky and clouds that move either to the left or right at different speeds.

If I’m correct, I don’t think there is a way to move something that’s already been painted in the canvas, therefore I made the Clouds a “Cloud” object (I used to think I could paint a vector based graphic and then manipulate it in the canvas, but I think now the HTML Canvas is more of a place where you paint and you can’t really manipulate what’s already been painted). Each cloud is nothing but an array of white circles. The program initializes several clouds randomly and then with an interval call it repaints the gradient sky, and each one of the clouds, then moves them, to repeat the cycle over and over.

I’m not sure if game devs out there are doing this or if there is a way to have the canvas move vector objects in a more efficient way than repainting the whole thing from scratch on every frame. At this point the animation looks very smooth but nothing except clouds moving is happening.

I hope in your comments I’ll find some answers as if this is the way to animate your game, meaning, keeping object representations of everything that’s been painted in memory, and repainting the canvas on every frame of the game cycle.

My goal (which I don’t know if is correct) is to do everything within a single CANVAS object. I’ve seen though how some people still like to play around with the DOM Tree, I’m not sure if adding and manipulating the DOM Tree is the correct way to procede here, I think it’d also be a nightmare to handle things when it comes to focus, or converting coordinates from one canvas to another and have everything make sense.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Playing with basics of HTML5 Canvas

Saturday, December 18th, 2010

Here’s a snippet of something I did tonight to play a bit with the Canvas and 2d Graphics context objects in javascript.

<html>
  <head>
    <title>Playing with Canvas</title>
  </head>

  <body style="padding:0 0; margin: 0 0; background:black">

    <canvas id="myCanvas" width="1024" height="1024" ></canvas>

    <script type="text/javascript">

    //convert to CSS friendly Hex String
    function d2h (n) {
      var result = n.toString(16);
      if (n < 16) {
	result = '0'+result;
      }
      return result;
    }

    var preparingBackground = true;
    var canvas = document.getElementById("myCanvas");
    var ctx = canvas.getContext("2d");

    var canvasWidth = parseInt(canvas.getAttribute("width"));
    var canvasHeight = parseInt(canvas.getAttribute("height"));

    var r=255; var red;
    var g=255; var green;
    var b=255; var blue;

    var rectHeight = canvasHeight/256;
    var rectWidth = canvasWidth/256;

    var rectX = 0;
    var rectY = 0;

    var Rect = function(xval,yval,speedXval,speedYval,r,g,b) {
      this.x = xval;
      this.y = yval;

      this.speedX = speedXval;
      this.speedY = speedYval;

      this.r = r;
      this.g = g;
      this.b = b;

      //so colors dont jump from 255 to 0.
      this.rDelta = 1;
      this.gDelta = 1;
      this.bDelta = 1;

      this.width = 1;
      this.height = 1;

      //so rectangle colors wont change so fast.
      this.colorChangeDefaultValue=1;
      this.colorChangeAcumulator=1;

      this.move = function () {
	this.x = this.x+this.speedX;
        this.y = this.y+this.speedY;

	//if you reach a borders change direction.
        if ((this.x+this.width >= canvasWidth && this.speedX > 0) ||
	    (this.x <= 0 && this.speedX < 0)) {
	  this.speedX = this.speedX*-1;
	}

        if ((this.y+this.height >= canvasHeight && this.speedY > 0) ||
	    (this.y <= 0 && this.speedY < 0)) {
	  this.speedY = this.speedY*-1;
	}
      }

      //draw and change colors
      this.draw = function () {
	//play with color gradients when you have a chance...
	if (this.colorChangeAcumulator--==0) {
	  if ((this.r >= 255 && this.rDelta > 0) ||
              (this.r <= 0 && this.rDelta < 0)) {
	    this.rDelta=this.rDelta*-1;
	  }

	  if ((this.g >= 255 && this.gDelta > 0) ||
              (this.g <= 0 && this.gDelta < 0)) {
	    this.gDelta=this.gDelta*-1;
	  }

	  if ((this.b >= 255 && this.bDelta > 0) ||
              (this.b <= 0 && this.bDelta < 0)) {
	    this.bDelta=this.bDelta*-1;
	  }

	  this.r=(this.r+this.rDelta) % 256;
	  this.g=(this.g+this.gDelta) % 256;
	  this.b=(this.b+this.bDelta) % 256; 

	  this.colorChangeAcumulator=this.colorChangeDefaultValue;
	  this.width++;
	  this.height++;
	}

	//paint the reactangle
	ctx.fillStyle="#"+d2h(this.r)+d2h(this.g)+d2h(this.b);
	ctx.fillRect(this.x,this.y,this.width,this.height);
      }
    }

    //animation interval ids
    var prepareBackgroundIntervalId = 0;
    var rectsIntervalId = 0;

    var speedX = 7;
    var speedY = 7;

    var theRects = [new Rect(0,0,speedX,speedY,10,44,100),
		    new Rect(canvasWidth,canvasHeight,-speedX,-speedY,125,0,0),
		    new Rect(canvasWidth,0,-speedX,speedY,0,210,80),
		    new Rect(0,canvasHeight,speedX,-speedY,40,40,0),
		    new Rect(canvasWidth/2,0,0,speedY,-4,40,40),
		    new Rect(canvasWidth/2,canvasHeight,4,-speedY,0,100,250),
		    new Rect(0,canvasHeight/2,speedX,0,210,100,100),
		    new Rect(canvasWidth,512,-speedX,0,100,10,0)];

    function prepareBackground() {

      if (preparingBackground) {
	r=r-1;
	g=g-1;
	b=b-1;

	red = d2h(r);
	green = d2h(g);
	blue = d2h(b);

	ctx.fillStyle = "#"+red+green+blue;
	ctx.fillRect(rectX,rectY,canvas.getAttribute("width"),rectHeight);

	rectY = rectY + rectHeight;

	preparingBackground = !(r==0 && g==0 && b==0);
      } else {
	clearInterval(prepareBackgroundIntervalId);
	rectsIntervalId = setInterval("moveRects()",33);
      }
    }

    function moveRects() {
      for (i in theRects) {
	var rect = theRects[i];
	rect.draw();
	rect.move();
      }
    }

    prepareBackgroundIntervalId = setInterval("prepareBackground()",1);

    </script>
  </body>

</html>
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

FrostWire for Android Desktop Integration Demo

Saturday, December 18th, 2010

Very convenient way to send files back and forth to your Android Phone or Tablet.

Absolutely free at frostwire.com
All source code available on bitbucket.org

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)


  • Categories

  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • October 2009
  • September 2009
  • July 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004