AS3 Resize with ratio


/ Published in: ActionScript 3
Save to your folder(s)

Usage:

<code>
var newDimensions:Object = resize(800,600,320,200);
</code>

This can be adapted to pretty much any language...


Copy this code and paste it in your HTML
  1. /**
  2. * resize
  3. * Take input diemnsion and return the resulting dimensions using a ratio
  4. * @param _w initial width
  5. * @param _h initial height
  6. * @param _targetW targeted width
  7. * @param _targetH targeted height
  8. * @param _ratio set the ratio manually instead of automatically detecting it (0)
  9. * @return An object containing both resulting dimensions :object.width,object.height
  10. */
  11. function resize(_w:Number,_h:Number,_targetW:Number,_targetH:Number,_ratio:Number = 0):Object {
  12. var ratio:Number = (_ratio ==0) ? _w / _h : _ratio;
  13. var rW:Number = _targetW;
  14. var rH:Number = _targetH;
  15. if (ratio >= 1) {
  16. rH = _targetW / ratio;
  17. if (rH > _targetH) {
  18. rH = _targetH;
  19. rW = rH * ratio;
  20. }
  21. }else {
  22. rW = _targetH * ratio;
  23. if (rW > _targetW) {
  24. rW = _targetW;
  25. rH = rW / ratio;
  26. }
  27. }
  28. var newDimensions:Object = {width:rW,height:rH };
  29. return newDimensions;
  30. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.