Saturday, February 4, 2012

lupa con flash

Posteado por Antonio con fecha Julio - 17 - 2009

Este es un efecto muy utilizado en varios tipos de desarrollo. Como puede ser en un catálogo de productos, ejemplo clásico si los hay.

Se me ocurrió armar una clase en AS3 para esta utilidad, a modo de simplificar el trabajo.

De paso les dejo una hermosa imagen del calendario Maya. Gran civilización si las hubo!

Mové el mouse sobre la imagen para ver el efecto.

A continuación les dejo la clase completa. Vale destacar los cuatro métodos con namespace en “protected”.

Estos métodos pueden ser reescritos si la clase es extendida… De manera tal que se puedan manejar los eventos de progreso de carga, y de la carga completa. Para poder sumarle un preloader tanto a la imagen chica, como a la grande por ejemplo.

Actionscript:

  1. package weedo.fx {
  2. import flash.display.DisplayObject;
  3. import flash.display.Sprite;
  4. import flash.display.Loader;
  5. import flash.events.*;
  6. import flash.net.URLRequest;
  7. import flash.ui.Mouse;
  8. public class Lupa extends Sprite
  9. {
  10. private var imgURL:String;
  11. private var imgBigURL:String;
  12. private var mascara:DisplayObject;
  13. private var small:Sprite;
  14. private var big:Sprite;
  15. private var cargaCompleta:Boolean;
  16. public function Lupa(_imgURL:String, _imgBigURL:String, _mask:DisplayObject):void
  17. {
  18. imgURL = _imgURL;
  19. imgBigURL = _imgBigURL;
  20. mascara = _mask;
  21. this.mouseChildren = false;
  22. this.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
  23. this.addEventListener(MouseEvent.ROLL_OUT, onOut);
  24. this.addEventListener(MouseEvent.ROLL_OVER, onOver);
  25. cargarImg();
  26. }
  27. private function cargarImg():void
  28. {
  29. var loaderImg:Loader = new Loader();
  30. loaderImg.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteImg);
  31. loaderImg.contentLoaderInfo.addEventListener(Event.INIT, onInitImg);
  32. loaderImg.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressImg);
  33. loaderImg.load(new URLRequest(imgURL));
  34. }
  35. private function cargarImgBig():void
  36. {
  37. var loaderImgBig:Loader = new Loader();
  38. loaderImgBig.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteImgBig);
  39. loaderImgBig.contentLoaderInfo.addEventListener(Event.INIT, onInitImgBig);
  40. loaderImgBig.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressImgBig);
  41. loaderImgBig.load(new URLRequest(imgBigURL));
  42. }
  43. protected function onProgressImg(e:ProgressEvent):void
  44. {
  45. trace(”img: “+Math.round((e.bytesLoaded*100)/e.bytesTotal));
  46. }
  47. protected function onProgressImgBig(e:ProgressEvent):void
  48. {
  49. trace(”imgBig: “+Math.round((e.bytesLoaded*100)/e.bytesTotal));
  50. }
  51. protected function onInitImg(e:Event):void
  52. {
  53. trace(”termino img”);
  54. }
  55. protected function onInitImgBig(e:Event):void
  56. {
  57. trace(”termino imgBig”);
  58. }
  59. private function onCompleteImg(e:Event):void
  60. {
  61. small = new Sprite();
  62. e.target.content.x = -(e.target.content.width*.5);
  63. e.target.content.y = -(e.target.content.height*.5);
  64. small.addChild(e.target.content);
  65. addChild(small);
  66. cargarImgBig();
  67. }
  68. private function onCompleteImgBig(e:Event):void
  69. {
  70. big = new Sprite();
  71. e.target.content.x = -(e.target.content.width*.5);
  72. e.target.content.y = -(e.target.content.height*.5);
  73. big.addChild(e.target.content);
  74. big.mask = mascara;
  75. big.visible = false;
  76. addChild(big);
  77. addChild(mascara);
  78. cargaCompleta = true;
  79. }
  80. private function onMove(e:MouseEvent):void
  81. {
  82. if(cargaCompleta){
  83. mascara.x = mouseX;
  84. mascara.y = mouseY;
  85. big.x = -((big.width * small.mouseX)/small.width) + small.mouseX;
  86. big.y = -((big.height * small.mouseY)/small.height) + small.mouseY;
  87. }
  88. }
  89. private function onOut(e:MouseEvent):void
  90. {
  91. if(cargaCompleta){
  92. big.visible = false;
  93. Mouse.show();
  94. }
  95. }
  96. private function onOver(e:MouseEvent):void
  97. {
  98. if(cargaCompleta){
  99. big.visible = true;
  100. Mouse.hide();
  101. }
  102. }
  103. public function cambiarMascara(_tg:DisplayObject):void
  104. {
  105. if(cargaCompleta){
  106. mascara = _tg;
  107. big.mask = mascara;
  108. }
  109. }
  110. }
  111. }

Para implementar la clase en el IDE de Flash deberían escribir algo así:

Actionscript:

  1. import weedo.fx.Lupa;
  2. var sh:Shape = new Shape();
  3. sh.graphics.beginFill(0×000000,1);
  4. sh.graphics.drawCircle(0, 0, 50);
  5. sh.graphics.endFill();
  6. var lp:Lupa = new Lupa(”small.jpg”, ”big.jpg”, sh);
  7. lp.x = stage.stageWidth * .5;
  8. lp.y = stage.stageHeight * .5;
  9. addChild(lp);

En este ejemplo importamos la clase en la primer linea. Luego creamos un Shape circular y por último creamos nuestra lupa con sus 3 parametros.

Primero la imagen pequeña, luego la grande y por último la máscara que usaremos como lupa.

En este caso la máscara es un Shape circular. También podría ser un rectangulo y hasta un movieclip con la forma que querramos.

Ojo que el punto de registro siempre tiene que estar en el medio de nuestra máscara.

Por ejemplo si queremos usar un rectangulo, el código debería ser así:

Actionscript:

  1. var sh:Shape = new Shape();
  2. sh.graphics.beginFill(0×000000,1);
  3. sh.graphics.drawRect(-25, -25, 50, 50);
  4. sh.graphics.endFill();

    le agradesemos a http://www.DeCabeza.net  por los textos y peliculas flash

Popularity: 1% [?]

Añadir un comentario