写程序的时候突然想到的方法,感觉简单又好用,一个实用函数,所以发出来,下面是这个函数:
function txtToPosArr(dt:String, w:Number, h:Number):Array {
/*
* author: fanflash(www.fanflash.cn)
* date: 2007-5-14
* info: 将文字转为点阵诸存
* -----Parameters-------
* dt: 要转换成字符串的字符
* w: 两点像素点之间的横轴间距
* h: 两点像素点之间的纵轴间距
* note: 如果想增加点的密度,可以把文本字号设置大些
*/
//检查参数的可用性
w = (w != undefined) ? w : 0;
h = (h != undefined) ? h : 0;
var t_txt:TextField = _root.createTextField("txtToPosTxt", _root.getNextHighestDepth(), -500, -500, 100, 100);
//设置文本
t_txt.autoSize = true;
t_txt.multiline = false;
t_txt.wordWrap = false;
t_txt.selectable = false;
t_txt.text = dt;
t_txt._visible = false;
//生成图片数据
var txtBmp:flash.display.BitmapData = new flash.display.BitmapData(t_txt._width, t_txt._height, false, 0xffffff);
txtBmp.draw(t_txt);
//创建文字数组
var t_arr:Array = new Array();
var pix:Number;
for (var y = 0; y<t_txt._height; y++) {
for (var x = 0; x<t_txt._width; x++) {
if (txtBmp.getPixel(x, y) != 0xffffff) {
t_arr.push({x:x+x*w, y:y+y*h});
}
}
}
//删除创建的文字
t_txt.removeTextField();
txtBmp.dispose();
return t_arr;
}
原理很简单,利用BITMAP类把黑色的点选出来,因为白色的是背景,所以只要是黑色的点,那么都是笔划和线条了.下面是一个用此函数的示例程序:
源程序:
//welcome to www.fanflash.cn
System.useCodepage = true;
Stage.scaleMode = "noScale";
Stage.showMenu = false;
function main() {
var t:Array;
ok_btn.onPress = function() {
removeMC();
t = txtToPosArr(input_txt.text, 4, 4);
for (var i in t) {
createMC(t.x, t.y);
}
};
}
function txtToPosArr(dt:String, w:Number, h:Number):Array {
/*
* author: fanflash(www.fanflash.cn)
* date: 2007-5-14
* info: 将文字转为点阵诸存
* -----Parameters-------
* dt: 要转换成字符串的字符
* w: 两点像素点之间的横轴间距
* h: 两点像素点之间的纵轴间距
* note: 如果想增加点的密度,可以把文本字号设置大些
*/
//检查参数的可用性
w = (w != undefined) ? w : 0;
h = (h != undefined) ? h : 0;
var t_txt:TextField = _root.createTextField("txtToPosTxt", _root.getNextHighestDepth(), -500, -500, 100, 100);
//设置文本
t_txt.autoSize = true;
t_txt.multiline = false;
t_txt.wordWrap = false;
t_txt.selectable = false;
t_txt.text = dt;
t_txt._visible = false;
//生成图片数据
var txtBmp:flash.display.BitmapData = new flash.display.BitmapData(t_txt._width, t_txt._height, false, 0xffffff);
txtBmp.draw(t_txt);
//创建文字数组
var t_arr:Array = new Array();
var pix:Number;
for (var y = 0; y<t_txt._height; y++) {
for (var x = 0; x<t_txt._width; x++) {
if (txtBmp.getPixel(x, y) != 0xffffff) {
t_arr.push({x:x+x*w, y:y+y*h});
}
}
}
//删除创建的文字
t_txt.removeTextField();
txtBmp.dispose();
return t_arr;
}
import flash.filters.GlowFilter;
import mx.transitions.Tween;
import mx.transitions.easing.*;
function createMC(x:Number, y:Number) {
//点陈坐标偏移
x += 50;
y += 150;
//补间动画样式
var tweenFun:Function = Back.easeOut;
var d:Number = _root.getNextHighestDepth();
var _mc:MovieClip = _root.attachMovie("dot", "dot"+d, d);
_mc.cacheAsBitmap = true;
//_mc.filters = [new GlowFilter(Math.round(Math.random()*0xffffff))];
_mc.filters = [new GlowFilter(0xffffff)];
_mc._x = Math.random()*Stage.width;
_mc._y = Math.random()*Stage.height;
var xt:Number = 1+Math.round(Math.random()*2);
var yt:Number = 1+Math.round(Math.random()*2);
new Tween(_mc, "_x", tweenFun, _mc._x, x, xt, true);
new Tween(_mc, "_y", tweenFun, _mc._y, y, yt, true);
}
function removeMC() {
for (var m in _root) {
if (_root[m]._name.substr(0, 3) == "dot") {
_root[m].removeMovieClip();
}
}
}
main();
源程序下载:
|
/upimg/soft/5/1_070528001150.rar |