1. 程式人生 > >raphael.js的使用

raphael.js的使用

itl nsf 遍歷 實現 push ext att 數組遍歷 demo

1.raphael.js svg畫圖的開源庫,支持IE8+

官方api: http://dmitrybaranovskiy.github.io/raphael/reference.html

Github地址: https://github.com/DmitryBaranovskiy/raphael/

2.js引用

//raphael.js主庫

<script src="./raphael.js" type="text/javascript"></script>

//raphael.js擴展庫,可實現局部元素拖拽,背景畫布拖拽縮放等
<script src="./raphael.extension.js" type="text/javascript"></script>

3.初始化

假設頁面上有兩個畫布需要同時繪制

<div id="main1" class="main main1" style="width:250px;height:300px;">
</div>
<div id="main2" class="main main2" style="width:250px;height:300px;">
</div>

js初始化部分:

var paper1=Raphael.createNew(document.getElementsByClassName(‘main‘)[0], 500, 400);
var paper2=Raphael.createNew(document.getElementsByClassName(‘main‘)[1], 500, 400);

//第一個參數(必選)為繪制主函數,此處設置為init函數(具體實現在下方),

//第二個參數(可選)為true表示每次繪制清除畫布,false不清除畫布,

//第三個參數(可選)為任意類型,是用戶傳給繪制主函數的自定義額外參數。

paper1.draw(init,true,{type:"post"});
paper2.draw(init);

//繪制主函數的實現,第一個參數(必選)為當前raphael實例,第二個參數(可選)為用戶自定義額外參數(與上方draw函數的第三個參數對應)

function init(paper,data){
console.log("start ,extra params:",data);
//畫圓
var cir1=paper.circle(15,15,10).attr({
fill:"red", //填充色
stroke:"blue", //邊緣線
"stroke-width":4 //
});

//矩形,起始點x,y,width,height
var rect1=paper.rect(40,25,60,40).attr({fill:"red",stroke:"green"});
var rect2=paper.rect(110,25,60,40,5).attr({fill:"red",stroke:"green"});

//橢圓
var ellipse1 = paper.ellipse(15,55,10,20).attr({
"fill":"#17A9C6", // background color of the ellipse
"stroke":"#2A6570", // ellipse‘s border color
"stroke-width":2 // border width
});
//M 移動到(moveTo) (x y)+
//Z 閉合路徑(closepath) (none)
//L 直線(lineTo) (x y)+
//H 水平直線 x+
//V 垂直直線 y+
//C 曲線(curveto) (x1 y1 x2 y2 x y)+
//S 平滑曲線 (x2 y2 x y)+
//Q 二次貝賽爾曲線 (x1 y1 x y)+
//T 平滑二次貝塞爾曲線 (x y)+
//A 橢圓弧 (rx ry x-axis-rotation large-arc-flag sweep-flag x y)+
//R Catmull-Rom 曲線* x1 y1 (x y)+

//繪制路徑

paper.path("M 40,40 H 90 V 60 H 70 V 110 H 60 V 60 H 40 z").attr({
"fill": "#5DDEF4",
"stroke": "#2A6570",
"stroke-width": 2
});
//2、三角形使用Path命令L
paper.path("M130,30 L200,30 L160,90 z").attr({
"fill": "#5DDEF4",
"stroke": "#2A6570",
"stroke-width": 2
});
//3、T形使用Path命令H,V
paper.path("M 40,40 H 90 V 60 H 70 V 110 H 60 V 60 H 40 z").attr({
"fill": "#5DDEF4",
"stroke": "#2A6570",
"stroke-width": 2
});
//4、2次貝塞爾曲線形,使用path命令Q
paper.path("M240,40L300,40L300,100");
paper.path("M240,40Q300,40 300,100").attr(‘stroke‘, ‘red‘);
//5、2次貝塞爾曲線形,使用path命令Q和T(第一個是正常繪制,第二個光滑連接)
paper.path(‘M10,250 L90,130 L160,160 L250,190 L250,70‘);
paper.path(‘M10,250 Q90,130 160,160 T250,70‘).attr(‘stroke‘, ‘red‘);
//6、繪制3次貝賽爾曲線,使用命令C,平滑畫線使用命令S
paper.path(‘M320,120 L350,180 L450,260 L480,140‘);
paper.path(‘M320,120 C350,180 450,260 480,140‘).attr(‘stroke‘, ‘red‘);
paper.path(‘M320,120 S450,260 480,140‘).attr(‘stroke‘, ‘yellow‘);
//transform
//T 平移|S 縮放 | R 按角度旋轉| M 變換矩陣
var rect2=paper.rect(110,95,60,40,5).attr({fill:"red",stroke:"green"}).transform("r90t20,0");
//rect2.animate(
// { "width":"300", "height":"200" },
// 500,
// ‘bounce‘,
// function(){ }
//);
var text1=paper.text(110,195,"你");//.attr({"font-size":"10px"});
var text1=paper.text(120,195,"好");

var rect3=paper.rect(110,195,60,40,5).attr({fill:"red",stroke:"green"});
//“linear”(線性)
//“<”或“easeIn”或“ease-in” (由慢到快)
//“>”或“easeOut”或“ease-out”(又快到慢)
//“<>”或“easeInOut”或“ease-in-out”(由慢到快再到慢)
//“backIn”或“back-in”(開始時回彈)
//“backOut”或“back-out”(結束時回彈)
//“elastic”(橡皮筋)
//“bounce”(彈跳)
//rect3.animate({
// transform: "r90,110,195t100,0s1.5"
//},2000,"backOut",function(){console.log("finish");})
//rect3.click(function(){
// alert("hahah!");
// });
rect3.data({
id:1,
name:"n1"
});
rect3.data({
id:2,
type:"test"
})
rect3.removeData("id")
rect3.dblclick(function(){
alert("It‘s a double click !"+rect3.data("name"));
})

var cir2=paper.circle(110,250,30);
var newCircle2=cir2.clone();
var newBBox2=newCircle2.getBBox();
console.log(newBBox2)
paper.rect(newBBox2.x,newBBox2.y,newBBox2.width,newBBox2.height);
//toFront() 、toBack() 、hide() 、show() 、remove()
//清空
//paper.clear()
var img1=paper.image("./favicon.ico",105,245,10,10);
paper.setSize(600,800);

var raphaelSet = paper.set();
raphaelSet.push(rect3,cir2);
// raphaelSet.splice(1, 1, cir, cir1, cir2);
raphaelSet.forEach(function(ele){
ele.attr({
"fill": "red"
});
console.log(ele[0]);
})
//raphaelSet.clear()
raphaelSet.attr({
"fill": "red"
});
console.log("paper",paper);
paper.setViewBox(00,0,200,200,false)
//paper.scale(1.3,1.3);
//元素可拖拽

img1.draggable();
//背景畫布可拖拽
paper.draggable();
text1.toFront()
setTimeout(function(){
text1.animate({
transform:"r360,115,200t10,25",
"font-size":30
},3000)
},3000)
window.paper=paper;
}

//縮放函數

function zoom(num,paper){
var paper=paper || window.paper;
if(num>0 ){
paper.zoomIn();
}
if(num<0 ){
paper.zoomOut();
}
if(num==0){
paper.zoom(1);
}
}

4.實例應用

偶然在一個商業js項目裏找到一幅世界地圖的json文件,借此試了試raphael的效率如何。

貼出代碼:

<!DOCTYPE html>
<html>

<head>
    <title></title>
    <meta charset="UTF-8" />
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta http-equiv=X-UA-Compatible content="IE=edge">
    <style>
        canvas {
            width: 100%;
            height: 100%;
        }
        
        body {
            overflow: hidden;
            padding: 0;
        }
        
        .main {
            width: 100%;
            height: 100%;
            padding: 0px;
            margin: auto;
            overflow: hidden;
            border: 1px solid yellow;
        }
    </style>
</head>

<body>
    <div id="main1" class="main main1">

    </div>
    <!-- 在此用jquery主要是為了map.json文件的讀取及數組遍歷省事 -->
    <script src="./jquery-2.2.0.js" type="text/javascript"></script>
    <script src="./raphael.js" type="text/javascript"></script>
    <script src="./raphael.extension.js" type="text/javascript"></script>
    <!-- http://www.hightopo.com/demo/large-screen/index.html -->
    <script src="./map.json"></script>

    <script type="text/javascript">
        function train(result) {
            console.log(result)
        }

        function init(paper, data) {
            console.log("start", data, window.jsonFile);

            var map = window.jsonFile;
            $.each(map.comps, function(i, e) {

                var pathStr = "";
                $.each(e.points, function(ii, ee) {
                    if (ii == 0) {
                        pathStr = "M" + ee;
                    } else {
                        if (e.segments) {
                            if (ii % 2 == 0 && e.segments[ii / 2] == 1) {
                                pathStr += ",M" + ee;
                            } else if (ii % 2 == 0 && e.segments[ii / 2] != 1) {
                                pathStr += ",L" + ee;
                            } else if (ii % 2 != 0) {
                                pathStr += "," + ee;
                            }
                        } else {
                            if (ii % 2 == 0) {
                                pathStr += ",L" + ee;
                            } else {
                                pathStr += "," + ee;
                            }
                        }


                    }

                });

                paper.path(pathStr)
                    .attr({
                        "stroke-width": e.borderWidth,
                        stroke: e.borderColor,
                    })
            });


            paper.draggable();
        }
        var paper1 = Raphael.createNew(document.getElementsByClassName(main)[0], $(window).width(), $(window).height());
        paper1.draw(init, true, {
            type: "post"
        });
        //setInterval(function(){
        //    paper1.draw(init,true);
        //    paper2.draw(init,true);
        //},2000);
    </script>
</body>

</html>

效果展示;

技術分享圖片

raphael.js的使用