几何图形并没有那么糟糕,至少它不像SVG 中的椭圆弧路径语法那样烦人。两者都是可控的。
这是一个函数,它将生成一系列弧段作为路径,将每个弧段(以及该项目的任意元数据)交给回调函数:
function generateRadialNav( paper, // Raphael's paper object
cx, cy, // x and y coordinates of the center of the circle from which the arcs will be calculated
inner_radius, // pixels from origin to interior arc
radial_thickness, // width of navigation area in pixels
menu_items, // an object with one key per menu item; the value of each key is arbitrary
callback, // a finalization callback for menu items which should accept three arguments: the menu_items key, the path element, and the menu_items value. The first and third parameters are taken directly from the menu_items object.
options ) // Unused but added out of habit.
{
options = options || {};
var start_radians = options.start_radians || 0;
var end_radians = options.end_radians || Math.PI / 2;
var menu_item_count = 0;
for ( var k in menu_items )
menu_item_count++;
var slice_radians = ( end_radians - start_radians ) / menu_item_count;
var index = 0;
for ( var k in menu_items )
{
var path = [];
path.push( "M", cx + Math.cos( start_radians + slice_radians * index ) * inner_radius, cy + Math.sin( start_radians + slice_radians * index ) * inner_radius );
path.push( "L", cx + Math.cos( start_radians + slice_radians * index ) * ( inner_radius + radial_thickness ), cy + Math.sin( start_radians + slice_radians * index ) * ( inner_radius + radial_thickness ) );
path.push( "A",
inner_radius + radial_thickness, inner_radius + radial_thickness,
slice_radians, 0, 1,
cx + Math.cos( start_radians + slice_radians * ( index + 1 ) ) * ( inner_radius + radial_thickness ), cy + Math.sin( start_radians + slice_radians * ( index + 1 ) ) * ( inner_radius + radial_thickness ) );
path.push( "L", cx + Math.cos( start_radians + slice_radians * ( index + 1 ) ) * inner_radius, cy + Math.sin( start_radians + slice_radians * ( index + 1 ) ) * inner_radius );
path.push( "A",
inner_radius, inner_radius,
slice_radians, 0, 0,
cx + Math.cos( start_radians + slice_radians * index ) * inner_radius, cy + Math.sin( start_radians + slice_radians * index ) * inner_radius );
path.push( "Z" );
var element = paper.path( path ).attr( { fill: 'none', stroke: 'none' } );
callback( k, element, menu_items[k] );
index++;
}
}
该函数本质上将计算每个导航项的区域,生成具有该形状的路径,然后将菜单定义传递给回调。任何对图标的支持都必须单独添加,但我建议可以轻松地扩充 menu_items 定义中的数据以支持它。有关此方法的使用示例,请查看我的测试页——当然,请原谅匆忙的粗糙边缘!