Total Pageviews

Monday 25 May 2015

Sprite Sheet Animation Using JavaScript and HTML5

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Sprite Sheet Animation</title>
</head>

<body>
<canvas id="myCanves" width="100" height="100">
<!-- Insert fallback content here -->
</canvas>
<script>
        var _width = 100;
var _height = 100;
var _frame = 4;
var _currentFrameCount = 0;
var ctx;

canvas = document.getElementById("myCanves");
ctx = canvas.getContext("2d");

var img = new Image();
img.src = "sprite.png";
img.onload = draw;

function draw ()
{
   ctx.clearRect(0, 0, _width, _height);   //to clear previously draw sprite before drawing new.
   ctx.drawImage(img, 0, _height * _currentFrameCount, _width, _height, 0, 0, _width, _height);  //to draw a sprite sheet with given height width and co-ordinates.

if(_currentFrameCount == _frame)
{
  _currentFrameCount = 0;
}
else
{
   _currentFrameCount++;
}
    }
setInterval(draw, 100);    // to call a draw function in a regular interval.

</script>
</body>
</html>

Equal Spacing between Dynamically created Textfields

private function createEqualSpacing(textfield : MovieClip, text : String, lastClip : MovieClip) : void
{
      (textfield.txt).text = text;
      (textfield.txt).multiline = true;
      (textfield.txt).width = 420;
      //Autosize textfield according to the text.
      (textfield.txt).autosize = TextFieldAutoSize.LEFT;
      (textfield.txt).border = true;

      if(lastClip != null)
      {
             textfield.y = lastClip.y + lastClip.height + 20;  //20 is fixed value space given between                                                                                                     //textfields.
      }
}


call the above function wherever required as below :

createEqualSpacing (mc1, xml.Text1, null);
createEqualSpacing (mc2, xml.Text2, mc2);
createEqualSpacing (mc3, xml.Text3, mc2);

Saturday 23 May 2015

Auto Resize font size of text inside textfield

Below function will be called as :

txtFieldFontResize (txtField, txtField.width, txtField.height);

private function txtFieldFontResize(txt:TextField, txtWidth : Number, txtHeight : Number):void
{
      //You set this according to your TextField's dimensions
      var maxTextWidth:int = txtWidth - 5; //(txtWidth == TextField.width)
      var maxTextHeight:int = txtWidth;

      var f:TextFormat = txt.getTextFormat();

     //decrease font size until the text fits
     while (txt.textWidth > maxTextWidth || txt.textHeight > maxTextHeight)
     {
                      f.size = int(f.size) - 1;
                      txt.setTextFormat(f);
      }
}


This code resize font size of textfield according to the text.

But there will be two things to keep in mind that :

1) Sometimes, after resizing font textfield will move up according to the ratio of font size decrement, So we need to set y position for this below while loop code block displayed above.

2) We need to set a limit of font size decrement, because suppose if font size will decrease till size 5 then user will not able to see that.

That's why we need to write few lines of code like,

if(f.size == 5)
{
     return;
}

I hope, my try is useful for few readers...
Happy Coding...:)


Microsoft Logo using flexbox and Reactjs

 <!DOCTYPE html> <html> <head>     <script src="https://unpkg.com/react@18/umd/react.development.js" crossori...