Ratio et ajustement de contenu


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

Permet d'ajuster la taille d'une image ou swf en fonction de son ratio et de celui de son conteneur.


Copy this code and paste it in your HTML
  1. var clipConteneur_mc:MovieClip = new MovieClip();
  2. var chargeImage:Loader = new Loader();
  3.  
  4.  
  5. clipConteneur_mc.graphics.beginFill(0x000000,1);
  6. clipConteneur_mc.graphics.drawRect(0, 0, 200, 150);
  7. clipConteneur_mc.graphics.endFill();
  8. addChild(clipConteneur_mc);
  9.  
  10. clipConteneur_mc.x = 20;
  11. clipConteneur_mc.y = 20;
  12.  
  13.  
  14. chargeImage.contentLoaderInfo.addEventListener(Event.COMPLETE, onChargeImageComplete);
  15. chargeImage.load(new URLRequest("images/image.jpg"));
  16.  
  17. function onChargeImageComplete(evt:Event):void
  18. {
  19. var contenuRatio:Number = clipConteneur_mc.width / clipConteneur_mc.height;
  20. var imgRatio:Number = evt.currentTarget.content.width / evt.currentTarget.content.height;
  21.  
  22. var img:Bitmap = Bitmap(chargeImage.content);
  23. img.smoothing = true;
  24.  
  25. if (imgRatio > contenuRatio)
  26. {
  27. img.width = clipConteneur_mc.width*0.95;
  28. img.height = (clipConteneur_mc.width/imgRatio)*0.95;
  29. }
  30. else if (imgRatio<=contenuRatio)
  31. {
  32. img.height = clipConteneur_mc.height*0.95;
  33. img.width = (clipConteneur_mc.height*imgRatio)*0.95;
  34. }
  35.  
  36. clipConteneur_mc.addChild(img);
  37.  
  38. img.x = clipConteneur_mc.width/2 - img.width/2;
  39. img.y = clipConteneur_mc.height/2 - img.height/2;
  40. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.