与Raphael JS的Radial Pie菜单

时间:2012-09-14 18:26:01

标签: jquery html css navigation raphael

感谢您关注我的困境。我正在尝试用raphael创建一个SVG菜单,我对几何体很糟糕。

下图显示了我正在制作的内容。我已经使用CSS创建了蓝色/中心部分,但似乎没有其他好方法可以获得白色/外部部分。图像&由于这些元素的阻塞性质,CSS在这方面失败了。我想生成一系列弧,其大小可以根据元素的数量而变化。

Radial Menu Example

那么,关于如何获得一系列可点击弧形的四分之一圆并可以产生悬停效果的任何建议?如果重要的话,我也想在这些东西上放置图标。

我看过几个在raphael中使用饼图的例子,我只是看不到如何调整它。啊。我应该注意几何学。

感谢您的时间。

1 个答案:

答案 0 :(得分:9)

几何图形并不是那么糟糕,至少它并不像elliptical arc path syntax in 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定义中的数据以支持它。有关使用此方法的示例,请查看my test page - 当然要赦免急速的粗糙边缘!