forked from: ActionScript Worker test
/**
* Copyright bkzen ( http://wonderfl.net/user/bkzen )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/cVOL
*/
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.geom.Point;
import flash.system.Capabilities;
import flash.text.TextField;
import flash.display.Sprite;
import flash.system.MessageChannel;
import flash.system.Worker;
import flash.system.WorkerDomain;
import flash.utils.ByteArray;
public class FlashTest extends Sprite {
private var worker:Worker;
private var messageChannel:MessageChannel;
private var bmd:BitmapData;
private var bmp:Bitmap;
private var bytes: ByteArray;
public function FlashTest() {
// write as3 code here..
var version: Array = Capabilities.version.split(" ")[1].split(",");
if (!Worker.isSupported || !(version[0] >= 11 && version[1] >= 5) )
{
var txt:TextField = new TextField();
txt.autoSize = "left";
txt.text = "Not support";
addChild(txt);
return;
}
bmd = new BitmapData(1024, 1024, true, 0);
if (Worker.current.isPrimordial)
{
txt = new TextField();
txt.autoSize = "left";
bmp = new Bitmap(bmd, "auto", true);
bytes = bmd.getPixels(bmd.rect);
bytes.shareable = true;
worker = WorkerDomain.current.createWorker(loaderInfo.bytes);
messageChannel = worker.createMessageChannel(Worker.current);
worker.setSharedProperty("_test_channel", messageChannel);
worker.setSharedProperty("bytes", bytes);
worker.start();
txt.text = messageChannel.receive(true);
addChild(bmp);
addChild(txt);
addEventListener(Event.ENTER_FRAME, mainLoop);
}
else
{
messageChannel = Worker.current.getSharedProperty("_test_channel");
bytes = Worker.current.getSharedProperty("bytes");
messageChannel.send("hogemoja");
addEventListener(Event.ENTER_FRAME, workersLoop);
}
}
private function mainLoop(e: Event): void
{
bmd.lock();
bytes.position = 0;
bmd.setPixels(bmd.rect, bytes);
bmd.unlock();
}
private function workersLoop(e:Event):void
{
var offsets: Array = [], n: int = 12;
for (var i:int = 0; i < n; i++)
{
offsets[i] = new Point();
}
for (i = 0; i < 3; i++ )
{
bmd.perlinNoise(10, 10, n, Math.random() * 0xFFFFFF, true, true, 7, false, offsets);
}
bytes.position = 0;
bmd.copyPixelsToByteArray(bmd.rect, bytes);
}
}
}