Scacci, Flex, Flash e ActionScript

Tag: BitmapData API

Usiamo il Threshold

by admin on Jun.14, 2009, under Actionscript 3.0

Il supporto del Threshold è stato introdotto con la versione 8 del Flash Player.

Il metodo threshold( ) è uno dei metodi più complessi nel panorama delle BitmapData API, ma abbastanza potente una volta che abbiamo capito come lavora. Il metodo usa due BitmapData:

  • destBitmap, che rappresenta la bitmap che sarà alterata.
  • sourceBitmap , che è l’altra bitmap che riceve dei parametri che sono gli sono stati passati. Questo metodo usa i pixel di questa bitmap per fare i suoi calcoli.

Il metodo confronta ogni pixel nel sourceBitmap contro un valore specificato, usando uno dei sei operatori confrontati. Per ulteriori approfondimenti si può consultare il LiveDocs di Adobe.

Ecco in breve la sintassi del metodo:
destBitmap.threshold(sourceBitmap,

sourceRect,
destPoint,
operation,
threshold,
color,
mask,
copySource)

Il parametro, sourceRect , è u’istanza della classe flash.geom.Rectangle. Esso definisce quale porzione del sourceBitmap vogliamo usare per il confronto. Se vogliamo usare l’intera, possiamo passare in sourceBitmap.rect come un valore di questo parametro.

Il parametro destPoint specifica il punto nel destBitmap nel quale i pixels iniziano ad essere affetti dall’alterazione, l’immagine sourceBitmap e sovrappoosta sulla destBitmap. Se vogliamo usare come punto di destinazione 0, 0 basta passarli come parametri al new Point( )  es.: (new Point(0, 0 )).

Il parametro operation è una delle sei stringhe che rappresentano gli operatori di confronto in Actionscript, esse sono: < , <= , > , >= , == , e !=

Il parametro successivo è threshold, ogni pixel viene confrontato con questo valore.

Il parametro mask serve per isolare un particolare canale di colore.

Adesso facciamo un esempio pratico sull’uso del threshold:

?View Code ACTIONSCRIPT
import caurina.transitions.*;
 
var url:String = "images/foto7_b.jpg";
var image1:Bitmap;
var destimage2:Bitmap;
var perc:Number;
var loader:Loader;
var image:Bitmap;
 
var holder:MovieClip;
holder = new MovieClip();
addChild(holder);
 
configureAssets();
 
function configureAssets():void {
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
loader.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
loader.contentLoaderInfo.addEventListener(Event.OPEN, openHandler);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
 
var request:URLRequest = new URLRequest(url);
loader.load(request);
holder.addChild(loader);
}
function completeHandler(e:Event):void {
//trace("Caricamento Completato");
 
tf.text = "Caricamento Completato";
}
function initHandler(e:Event):void {
 
var loaderHgt = e.currentTarget.height;
var loaderWdt = e.currentTarget.width;
 
var src_bmpd1:BitmapData = new BitmapData(loaderWdt, loaderHgt);
src_bmpd1.draw(this);
 
var dest_bmpd2:BitmapData = new BitmapData(loaderWdt, loaderHgt, false);
destimage2 = new Bitmap(dest_bmpd2);
holder.addChild(destimage2);
 
var j:Number = 0;
 
function onTransitions():void {
 
j++;
//trace(j)
dest_bmpd2.threshold(src_bmpd1, new Rectangle(0, 0, src_bmpd1.width, src_bmpd1.height), new Point(0, 0), "&gt;=", (j/55) * 0xFFFFFF, 0xFFFFFFFF, 0xFFFFFF, true);
}
Tweener.addCaller(this, {onUpdate:onTransitions, time:1, count:55, transition:"linear"});
 
function onRepeatEffect():void {
 
Tweener.addCaller(this, {onUpdate:onTransitions, time:1, count:55, transition:"linear"});
 
}
 
repeat_btn.addEventListener(MouseEvent.CLICK, onRepeatEffect);
}
 
function openHandler(e:Event):void {
 
//trace("Caricamento inizializzato, attendere il completamento!");
 
}
 
var tf:TextField = new TextField();
tf.autoSize = TextFieldAutoSize.LEFT;
tf.textColor = 0xFF0000;
tf.x = 30;
tf.y = 366;
addChild(tf);
 
function progressHandler(e:ProgressEvent) {
 
perc = Math.round((e.bytesLoaded/e.bytesTotal)*100);
//trace("Caricamento in corso: "+perc+"%");
tf.text = String("Loading: " + perc + "%");
}
 
function ioErrorHandler(e:IOErrorEvent):void {
trace("Impossibile caricare l'immagine: " + url);
 
}
 
/////////////////////////
 
repeat_btn.label = "Repeat";

Download source file

Guarda esempio

1 Comment :, , , more...

Cerca qualcosa?

Usa il form sottostante per la ricerca nel sito:

Ancora non hai trovato quello che cerchi? Lascia un commento su un post!