先日エントリーしたJSFLですが、どうも実装が緩いのでちろちろ手直し入れていて。改めてアップすることにしました。主なところは、曲線の情報を得るのに使っているEdgeというオブジェクトが、始点と終点の情報が他の曲線と比べて逆になる場合がある、ということの対応と、書き出す座標値の方向を順方向、反転方向選べるようにしたところ。
とりあえずこんな感じです。
//**************************************************************************** // Script for Tweener _bezier Option // author nutsu // version 0.2 // // sample output : { x:460, y:100, _bezier:[{x:250, y:200},{x:355, y:150}] } //**************************************************************************** var edgeArray = fl.getDocumentDOM().selection[0].edges; var tmp = []; var tmp2 = []; var turn = []; for ( i=0; i<edgeArray.length; i++ ) tmp.push( edgeArray[i] ); var e0 = tmp.shift(); tmp2.push( e0 ); turn.push( false ); joinX0 = e0.getControl(0).x; joinY0 = e0.getControl(0).y; joinX2 = e0.getControl(2).x; joinY2 = e0.getControl(2).y; while( tmp2.length<edgeArray.length ) { flg_join = false; var min_value = -1; var min_pos = -1; var min_index = -1; for( i=0; i<tmp.length; i++ ) { var ed = tmp[i]; var p0 = ed.getControl(0); var p2 = ed.getControl(2); if( p0.x==joinX2 && p0.y==joinY2 ) { flg_join = true; tmp2.push( ed ); turn.push( false ); joinX2 = p2.x; joinY2 = p2.y; tmp.splice( i, 1 ); i = 0; } else if( p2.x==joinX0 && p2.y==joinY0 ) { flg_join = true; tmp2.unshift( ed ); turn.push( false ); joinX0 = p0.x; joinY0 = p0.y; tmp.splice( i, 1 ); i = 0; } else if( p2.x==joinX2 && p2.y==joinY2 ) { flg_join = true; tmp2.push( ed ); turn.push( true ); joinX2 = p0.x; joinY2 = p0.y; tmp.splice( i, 1 ); i = 0; } else if( p0.x==joinX0 && p0.y==joinY0 ) { flg_join = true; tmp2.unshift( ed ); turn.push( true ); joinX0 = p2.x; joinY0 = p2.y; tmp.splice( i, 1 ); i = 0; } else { var min1 = (p0.x-joinX2)*(p0.x-joinX2) + (p0.y-joinY2)*(p0.y-joinY2); var min2 = (p2.x-joinX0)*(p2.x-joinX0) + (p2.y-joinY0)*(p2.y-joinY0); var min3 = (p2.x-joinX2)*(p2.x-joinX2) + (p2.y-joinY2)*(p2.y-joinY2); var min4 = (p0.x-joinX0)*(p0.x-joinX0) + (p0.y-joinY0)*(p0.y-joinY0); if( min1<min_value || min_value<0 ) { min_value = min1; min_pos = 1; min_index = i; } if( min2<min_value ) { min_value = min2; min_pos = 2; min_index = i; } if( min3<min_value ) { min_value = min3; min_pos = 3; min_index = i; } if( min4<min_value ) { min_value = min4; min_pos = 4; min_index = i; } } } //最近傍に連結 if( flg_join==false ) { fl.trace("debug>>" + "join nearest point." ); var ed = tmp[min_index]; var p0 = ed.getControl(0); var p2 = ed.getControl(2); if( min_pos==1 ) { tmp2.push( ed ); turn.push( false ); joinX2 = p2.x; joinY2 = p2.y; } else if( min_pos==2 ) { tmp2.unshift( ed ); turn.push( false ); joinX0 = p0.x; joinY0 = p0.y; } else if( min_pos==3 ) { tmp2.push( ed ); turn.push( true ); joinX2 = p0.x; joinY2 = p0.y; } else { tmp2.unshift( ed ); turn.push( true ); joinX0 = p2.x; joinY0 = p2.y; } tmp.splice( min_index, 1 ); } } var lastpos = ""; var option = "_bezier:["; if( confirm("Reverse the curve?")==false ) { for( i=0; i<tmp2.length; i++ ) { var ed = tmp2[i]; var p0 = ed.getControl(0); var p1 = ed.getControl(1); var p2 = ed.getControl(2); option += "{x:" + p1.x + ",y:" + p1.y + "}"; if( i==tmp2.length-1 ) lastpos = (turn[i]) ? "x:" + p0.x + ", y:" + p0.y : "x:" + p2.x + ", y:" + p2.y; else option += ","; } } else { for( i=(tmp2.length-1); i>-1; i-- ) { var ed = tmp2[i]; var p0 = ed.getControl(2); var p1 = ed.getControl(1); var p2 = ed.getControl(0); option += "{x:" + p1.x + ",y:" + p1.y + "}"; if( i==0 ) lastpos = (turn[i]) ? "x:" + p0.x + ", y:" + p0.y : "x:" + p2.x + ", y:" + p2.y; else option += ","; } } option += "]"; fl.trace( "{" + lastpos + ", " + option + "}" );
あんまりテストしてないのでまだまだバグるかもですが。