Mask problem with flex canvas and drag & drop

July 12th, 2009 by Andrea Franz

Some weeks ago a spent a lot of time trying to fix a bug inside an AIR application I was working on.
I wrote a custom component that has an image as child. This image is draggable, and the container should act as a mask. The problem was that when I started dragging the picture, the picture went outside its container, like the following example.

Try dragging the red square outside the white one:

This movie requires Flash Player 9

I fixed this bug just setting the scrollRect property to the container:

This movie requires Flash Player 9

Only when I realized how to fix the bug I found another guy with the same problem and the same solution.

Here the code:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application width="500" height="300" xmlns:mx="http://www.adobe.com/2006/mxml" 
  layout="vertical" verticalAlign="middle" horizontalAlign="center"
  applicationComplete="init()">
  <mx:Script>
    <![CDATA[												
      public function init():void
      {				
        this.container.scrollRect = new Rectangle(0, 0, this.container.width, this.container.height);																
        this.draggableCanvas.addEventListener(MouseEvent.MOUSE_UP, this.mouseUpHandler);				
        this.draggableCanvas.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseDownHandler);
      }			
 
      private function mouseDownHandler(event:MouseEvent):void
      {
        this.draggableCanvas.startDrag();
      }
 
      private function mouseUpHandler(event:MouseEvent):void
      {
        this.draggableCanvas.stopDrag();
        }
    ]]>
  </mx:Script>
  <mx:Canvas id="container" width="300" height="250" backgroundColor="white" horizontalScrollPolicy="off" verticalScrollPolicy="off">
    <mx:Canvas id="draggableCanvas" x="50" y="25" width="200" height="200" backgroundColor="#AA0000">			
    </mx:Canvas>
  </mx:Canvas>
</mx:Application>

2 things I like in prototype 1.6.1

March 31st, 2009 by Andrea Franz

The stable version of prototype is 1.6.0.3. I just fiddled around with the edge version (1.6.1_rc2) and I found 2 new features I really like. The first one is the Element#clone method. At last I can easily clone an element, or make a deep clone of it passing true as first argument:

<div id="genres">
  <div class="genre">
    <select name="genre[]">
      <option>Your favorite genre...</option>
      <option>Death Metal</option>
      <option>Black Metal</option>
      <option>Speed Metal</option>
    </select>
  </div>
</div>
<a id="add-genre" href="#">Add</a>
document.observe("dom:loaded", function() {
  $("add-genre").observe("click", function(event) {
    event.stop();
    var genres = $("genres"); 
    genres.insert(genres.down(".genre").clone(true));
  });
});

Just click the Add link to add a new the select tag.

Another cool new feature is the Element Storage. You can store any kind of data inside a element, and retrieve it when you need it.

Here an example:

<div id="items">
  <div class="item" id="item-1">
    <h1>Item 1</h1>
    <a href="#" class="save">Save</a>
  </div>
  <div class="item" id="item-2">
    <h1>Item 2</h1>
    <a href="#" class="save">Save</a>
  </div>
</div>
document.observe("dom:loaded", function() {
  var item = $("item-1");
  var data = item.retrieve("data", {});
  data.url = "http://cowboys.from.hell/items/1";			
 
  $("items").select("a.save").invoke("observe", "click", function(event) {
    alert(event.target.up(".item").retrieve("data").url);
  });
});

Just click the first save link and an alert will display the url I previously stored inside the first item element.

Given what I’m seeing I’m pretty excited about the upcoming version of prototype.


Terror the micro aggregator

March 17th, 2009 by Andrea Franz

Terror is a micro feed aggregator made with sinatra and bundled as a gem. It’s very simple to use, just install the gem:

gem sources -a http://gems.github.com
sudo gem install pilu-terror

Create a new project

terror new_aggregator_name
cd new_aggregator_name 

Start the server

thin start -C config/thin.yml

Run the feeds fetcher

rake feeds:fetch # run it as a cron job

You can browse the source and fork it on github.


Web app theme

January 18th, 2009 by Andrea Franz

A simple theme for web apps. Fork it on github and use it for your cool applications!

Web App Theme

[UPDATE]

Like I wrote in the comments, this layout is inspired from cool applications like lighthouse, basecamp, and others. I love those applications, I love their layouts, and it’s difficult to not be inspired by them.


Radiant Gallery on Dreamhost with Ruby 1.8.7

January 15th, 2009 by Andrea Franz

Some people in the past had problems using the Gallery extension on Dreamhost. I didn’t try Dreamhost but someone from the Radiant list solved that problem following these instructions. Check it out if you are on Dreamhost too.