zoukankan      html  css  js  c++  java
  • 使用Raphaël类库实现的超酷动画技能图表

    在线演示

    在这篇文章中GBin1将介绍如何使用Raphaël这个js类库构建图表。Raphaël是一个非常小的js类库用于构建丰富的矢量图形。

    这个图形的创意非常简单,我们使用一些圆弧来展示一个技能百分比数值到一个主圆周中,当我们将鼠标挪到对应的圆弧,将显示对应的技能百分比并且产生对应特效。

    HTML标签

    HTML代码结构包括一个命名为"get"的主容器标签。这个容器包括了所有的用于展现图形的数据。然后我们创建一个新的名字叫"diagram"的div元素,这个元素用来作为生成圆弧的容器,代码如下:

    1. <div id="diagram"></div>
    2. <div class="get">
    3. <div class="arc">
    4. <span class="text">jQuery</span>
    5. <input type="hidden" class="percent" value="95" />
    6. <input type="hidden" class="color" value="#97BE0D" />
    7. </div>
    8. <div class="arc">
    9.  
    10. <span class="text">CSS3</span>
    11. <input type="hidden" class="percent" value="90" />
    12. <input type="hidden" class="color" value="#D84F5F" />
    13. </div>
    14. <div class="arc">
    15. <span class="text">HTML5</span>
    16. <input type="hidden" class="percent" value="80" />
    17. <input type="hidden" class="color" value="#88B8E6" />
    18.  
    19. </div>
    20. <div class="arc">
    21. <span class="text">PHP</span>
    22. <input type="hidden" class="percent" value="53" />
    23. <input type="hidden" class="color" value="#BEDBE9" />
    24. </div>
    25. <div class="arc">
    26. <span class="text">MySQL</span>
    27. <input type="hidden" class="percent" value="45" />
    28. <input type="hidden" class="color" value="#EDEBEE" />
    29. </div>
    30. </div>

    CSS样式定义

    在这个演示的样式表我们只定义如下:

    1. 隐藏class为'get'的元素

    2. 设置id为'diagram'的元素宽度和高度

    1. .get {
    2. display:none;
    3. }
    4.  
    5. #diagram {
    6. float:left;
    7. width:600px;
    8. height:600px;
    9. }

    Javascript

    主要方法是创建一个新的Raphaël对象(变量为'r'),然后画出我们到一个圆形,使用我们指定的半径'rad'。

    然后我们在创建的Raphaël对象中创建一个新的圆形。我们使得圆形居中,并且添加一些文字。

    1. var o = {
    2. init: function(){
    3. this.diagram();
    4. },
    5. random: function(l, u){
    6. return Math.floor((Math.random()*(u-l+1))+l);
    7. },
    8. diagram: function(){
    9. var r = Raphael('diagram', 600, 600),
    10. rad = 73;
    11.  
    12. r.circle(300, 300, 85).attr({ stroke: 'none', fill: '#193340' });
    13.  
    14. var title = r.text(300, 300, 'Skills').attr({
    15. font: '20px Arial',
    16. fill: '#fff'
    17. }).toFront();
    18.  
    19. }
    20. }
     

    接下来我们更深入一些。

    我们将扩展Raphaël对象,使用一些自定义的属性:

    • alpha - 圆弧的度数
    • random - 指定的随机数
    • sx,sy - 起始的位置
    • x,y - 结束位置
    • path
    1. var o = {
    2. init: function(){
    3. this.diagram();
    4. },
    5. random: function(l, u){
    6. return Math.floor((Math.random()*(u-l+1))+l);
    7. },
    8. diagram: function(){
    9. var r = Raphael('diagram', 600, 600),
    10. rad = 73;
    11.  
    12. r.circle(300, 300, 85).attr({ stroke: 'none', fill: '#193340' });
    13.  
    14. var title = r.text(300, 300, 'Skills').attr({
    15. font: '20px Arial',
    16. fill: '#fff'
    17. }).toFront();
    18.  
    19. r.customAttributes.arc = function(value, color, rad){
    20. var v = 3.6*value,
    21. alpha = v == 360 ? 359.99 : v,
    22. random = o.random(91, 240),
    23. a = (random-alpha) * Math.PI/180,
    24. b = random * Math.PI/180,
    25. sx = 300 + rad * Math.cos(b),
    26. sy = 300 - rad * Math.sin(b),
    27. x = 300 + rad * Math.cos(a),
    28. y = 300 - rad * Math.sin(a),
    29. path = [['M', sx, sy], ['A', rad, rad, 0, +(alpha > 180), 1, x, y]];
    30. return { path: path, stroke: color }
    31. }
    32.  
    33. $('.get').find('.arc').each(function(i){
    34. var t = $(this),
    35. color = t.find('.color').val(),
    36. value = t.find('.percent').val(),
    37. text = t.find('.text').text();
    38.  
    39. rad += 30;
    40. var z = r.path().attr({ arc: [value, color, rad], 'stroke-width': 26 });
    41.  
    42. z.mouseover(function(){
    43. this.animate({ 'stroke-width': 50, opacity: .75 }, 1000, 'elastic');
    44. if(Raphael.type != 'VML') //solves IE problem
    45. this.toFront();
    46. title.animate({ opacity: 0 }, 500, '>', function(){
    47. this.attr({ text: text + 'n' + value + '%' }).animate({ opacity: 1 }, 500, '<');
    48. });
    49. }).mouseout(function(){
    50. this.animate({ 'stroke-width': 26, opacity: 1 }, 1000, 'elastic');
    51. });
    52. });
    53. }
    54. }
     

    然后我们取得所有需要的数据,例如,百分比,弧度颜色,及其文字。我们给每一个弧度添加半径数值最后创建一个新的圆弧path。最后一步我们添加鼠标hover的动画效果。当鼠标悬浮到圆弧时,标题会改变(即圆圈里的内容)。同时我们让圆弧变大一些。

    今天我们介绍了 Raphaël的类库,如果大家有兴趣,可以去类库的网站查看更多的例子: Raphaël主站

      详细内容参见原文:http://www.gbtags.com/gb/share/5828.htm

     
  • 相关阅读:
    js对象数组(JSON) 根据某个共同字段 分组
    一个 函数 用来转化esSearch 的range 条件
    关于 vuex 报错 Do not mutate vuex store state outside mutation handlers.
    android listview 重用view导致的选择混乱问题
    android SDK和ADT的更新
    Android中adb push和adb install的使用区别
    pycharm中添加扩展工具pylint
    su Authentication failure解决
    Putty以及adb网络调试
    有关android源码编译的几个问题
  • 原文地址:https://www.cnblogs.com/gbtags/p/4663997.html
Copyright © 2011-2022 走看看