欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用js畫圖之圓、弧、扇形

 更新時(shí)間:2015年01月12日 15:18:14   投稿:hebedich  
這篇文章主要介紹了使用js繪制幾何圖形教程,本文主要是教大家繪制圓、弧、扇形,需要的朋友可以參考下

半徑為r的圓上的點(diǎn)p(x,y)與圓心O(x0,y0)的關(guān)系: x = x0+rcosA;  y = y0+rsinA ,A為弧度

樣例:http://www.zhaojz.com.cn/demo/draw6.html

一、圓

復(fù)制代碼 代碼如下:

//圓形/橢圓
//dot 圓點(diǎn)
//r 半徑
//compressionRatio 垂直壓縮比
function drawCircle(dot, r, compressionRatio, data){
    var pstart = [dot[0]+r, dot[1]]; //起點(diǎn)
    var pre = pstart;
    for(var i=0; i < 360; i+=5){
        rad = i*Math.PI/180; //計(jì)算弧度
        //r*Math.cos(rad) 弧線的終點(diǎn)相對(duì)dot的水平偏移
        //r*Math.sin(rad) 弧線的終點(diǎn)相對(duì)dot的垂直偏移
        //compressionRatio 垂直壓縮比例
        var cur = [r*Math.cos(rad)+dot[0], compressionRatio*r*Math.sin(rad)+dot[1]];
        drawLine(pre,cur);
        pre = cur; //保存當(dāng)前點(diǎn)的坐標(biāo)
    }
    drawLine(pre,pstart);//使閉合
    //描圓點(diǎn)
    drawPoint({
        pw:2,ph:2,color:'DarkRed',point:dot
    });
}

二、弧

  就在畫出圓的一部分,算法與圓相似

復(fù)制代碼 代碼如下:

//畫弧
//dot 圓點(diǎn)
//r 半徑
//angle 圓心角
//angleOfSlope 與x軸的夾角
//pop 是否彈出
//title 標(biāo)簽
function drawArc(dot, r, angle, angleOfSlope, pop, title){
    var newDot = [dot[0], dot[1]];
    var a = (angleOfSlope+angle/2)*Math.PI/180;
    if(pop){ //計(jì)算圓心的新坐標(biāo)
        newDot[0] = dot[0]+10*Math.cos(a);
        newDot[1] = dot[1]+10*Math.sin(a);
    }
    if(!angleOfSlope){
        angleOfSlope = 0;
    }
    var aos = angleOfSlope*Math.PI/180;
    var aos2 = (angleOfSlope+angle)*Math.PI/180;
    var pstart = [newDot[0]+r*Math.cos(aos), newDot[1]+r*Math.sin(aos)]; //弧線的起點(diǎn)
    var pend = [newDot[0]+r*Math.cos(aos2), newDot[1]+r*Math.sin(aos2)]; //弧線的終點(diǎn)
    var pre = pstart;
    for(var i=0; i < angle; i+=2){ //在angle范圍內(nèi)畫弧
        rad = (i+angleOfSlope)*Math.PI/180;
        var cur = [r*Math.cos(rad)+newDot[0], r*Math.sin(rad)+newDot[1]];
        drawLine(pre,cur);
        pre = cur;
    }
}

三、扇形

  將弧的兩端與圓心相連

復(fù)制代碼 代碼如下:

//扇形
//dot 圓點(diǎn)
//r 半徑
//angle 圓心角
//angleOfSlope 與x軸的夾角,確定扇形的方向
//pop 是否彈出,即是否偏離圓心
//title 標(biāo)簽
function drawSector(dot, r, angle, angleOfSlope, pop, title){
    var newDot = [dot[0], dot[1]];
    var a = (angleOfSlope+angle/2)*Math.PI/180;
    if(pop){ //計(jì)算圓心的新坐標(biāo)
        newDot[0] = dot[0]+10*Math.cos(a);
        newDot[1] = dot[1]+10*Math.sin(a);
    }
    if(!angleOfSlope){
        angleOfSlope = 0;
    }
    var aos = angleOfSlope*Math.PI/180;
    var aos2 = (angleOfSlope+angle)*Math.PI/180;
    var pstart = [newDot[0]+r*Math.cos(aos), newDot[1]+r*Math.sin(aos)]; //弧線的起點(diǎn)
    var pend = [newDot[0]+r*Math.cos(aos2), newDot[1]+r*Math.sin(aos2)]; //弧線的終點(diǎn)
    drawLine(newDot,pstart); //連接圓心與起點(diǎn)
    var pre = pstart;
    for(var i=0; i < angle; i+=2){ //在angle范圍內(nèi)畫弧
        rad = (i+angleOfSlope)*Math.PI/180;
        var cur = [r*Math.cos(rad)+newDot[0], r*Math.sin(rad)+newDot[1]];
        drawLine(pre,cur);
        pre = cur;
    }
    drawPolyline([pre, pend, newDot]); //使閉合
    //描圓心
    drawPoint({
        pw:2,ph:2,color:'DarkRed',point:dot
    });
    //標(biāo)簽
    if(title){
        document.write("<span style='height: 24px; line-height: 24px; width: 80px; text-align: center; color: RED; position: absolute; left:"+(newDot[0]+r*(2/4)*Math.cos(a)-40)+"px; top: "+(newDot[1]+r*(2/4)*Math.sin(a)-12)+"'>"+title+"</span>");
    }
}

是不是很震撼,原來js也能做如此炫酷的事情。。。

相關(guān)文章

最新評(píng)論