Home

Quick Image Viewer

Thursday, November 26th, 2009 | Author: dehash

Image ViewerMade a quick image displayer with zoom and pan in just a few lines of code using BumpSlide and MinimalComps which can be viewed here. The main source code is below and the image is The Starry Night by Van Gogh from here


Actionscript:
  1. /**
  2. * v0.1 code by dehash.com 2009
  3. * based on example code at :
  4. * http://bumpslide.googlecode.com/svn/trunk/as3/examples/zoom/ZoomDemo_01.as
  5. * Released as open source under the BSD License
  6. * http://www.opensource.org/licenses/bsd-license.php
  7. */
  8.  
  9. package {
  10.  
  11. import com.bit101.components.HBox;
  12. import com.bit101.components.HSlider;
  13. import com.bit101.components.Label;
  14. import com.bit101.components.PushButton;
  15.  
  16. import com.bumpslide.ui.Applet;
  17. import com.bumpslide.ui.ZoomPanel;
  18. import net.stevensacks.utils.Web;
  19.  
  20. import flash.display.Bitmap;
  21. import flash.display.Loader;
  22. import flash.events.Event;
  23. import flash.events.MouseEvent;
  24. import flash.net.URLRequest;
  25.  
  26. public class ImageDisplayer extends Applet {
  27.  
  28. static private const CONTENT_URL:String = "starrynight1280.jpg";
  29. private var zoomPanel:ZoomPanel;
  30. private var image:Loader;
  31. private var myZoomSlider:HSlider;
  32. private var controls:HBox;
  33. private var credits:HBox;
  34. private var offset:uint = 50;
  35. private var label:Label;
  36.  
  37. override protected function addChildren():void {
  38.  
  39. // load image with smoothing not applied
  40. image = new Loader();
  41. image.contentLoaderInfo.addEventListener(Event.INIT, function(e:Event):void {
  42. (image.content as Bitmap).smoothing = false;
  43. });
  44. image.load(new URLRequest(CONTENT_URL));
  45.  
  46. // create zoom panel with image as content
  47. zoomPanel = new ZoomPanel();
  48. zoomPanel.content = image;
  49. addChild(zoomPanel);
  50.  
  51. // create HBox to hold controls
  52. controls = new HBox(this);
  53.  
  54. // create controls
  55. new PushButton(controls, 0, 0, "Home", function():void {
  56. zoomPanel.panTo(0, 0);
  57. });
  58. new PushButton(controls, 0, 0, "<<--", function():void {
  59. zoomPanel.panLeft();
  60. });
  61. new PushButton(controls, 0, 0, "-->>", function():void {
  62. zoomPanel.panRight();
  63. });
  64. new PushButton(controls, 0, 0, "/\\", function():void {
  65. zoomPanel.panUp();
  66. });
  67. new PushButton(controls, 0, 0, "\\/", function():void {
  68. zoomPanel.panDown();
  69. });
  70. myZoomSlider = new HSlider(controls, 0, 5, doZoom);
  71. myZoomSlider.tick = 0.25;
  72. myZoomSlider.minimum = 0.25;
  73. myZoomSlider.maximum = 7;
  74. myZoomSlider.value = 1;
  75.  
  76. addChild(controls);
  77.  
  78. zoomPanel.dragZoomControl.minZoom = .25;
  79. zoomPanel.dragZoomControl.maxZoom = 7;
  80.  
  81. zoomPanel.zoomContent.cacheAsBitmap = true;
  82.  
  83. // create HBox to hold credits
  84. credits = new HBox(this);
  85.  
  86. // create credits
  87. var labelText:String = "Van Gogh - Starry Night - 1889 -";
  88. labelText += "(source http://www.flickr.com/photos/financialaidpodcast/3956763480/)";
  89. label = new Label(credits, 0, 0, labelText);
  90.  
  91. new PushButton(credits, 0, 0, "Image Source", getSource);
  92.  
  93. }
  94.  
  95. private function getSource(e:MouseEvent):void {
  96. var link:String = "http://www.flickr.com/photos/financialaidpodcast/3956763480/";
  97. Web.getURL(link, "_blank");
  98. }
  99.  
  100. private function doZoom(event:Event):void {
  101. zoomPanel.zoom = myZoomSlider.value;
  102. }
  103.  
  104. /**
  105. * Applets are resized when stage size changes, so re-center content during redraw
  106. */
  107. override protected function draw():void {
  108. zoomPanel.move(offset, offset);
  109. zoomPanel.setSize(width * .85, height * .85);
  110. controls.x = offset;
  111. controls.y = offset / 2;
  112. credits.x = offset;
  113. credits.y = height * .95;
  114. }
  115. }
  116. }

Tags »

 | Comments Feed: RSS 2.0
Category: Code Examples, Flash

You can leave a response.
Pinging is currently not allowed.

One Response

  1. 1
    samBrown 

    nice work; thanks for the post..

Leave a Reply

CAPTCHA Image