首页 > 代码库 > canvas图形函数

canvas图形函数

function drawStar(cobj,x, y, radius1, radius2, num, drawType, color) {//参数:画笔,圆心X。圆心Y,半径1,半径2,形状边,实心|空心,颜色

  var angle = 360 / (num * 2);

  var arr = [];

  for(var i = 0; i < num * 2; i++) {

    var starObj = {};

    if(i % 2 == 0) {

      starObj.x = x + radius1 * Math.cos(i * angle * Math.PI / 180);

      starObj.y = y + radius1 * Math.sin(i * angle * Math.PI / 180);

    } else {

      starObj.x = x + radius2 * Math.cos(i * angle * Math.PI / 180);

      starObj.y = y + radius2 * Math.sin(i * angle * Math.PI / 180);

    }

    arr.push(starObj);

  } 

  cobj.beginPath();

  cobj.fillStyle = color;

  cobj.strokeStyle = color;

  cobj.moveTo(arr[0].x, arr[0].y);

  for(var i = 1; i < arr.length; i++) {

    cobj.lineTo(arr[i].x, arr[i].y);

  }

  cobj.closePath();

  if(drawType == "fill") {

    cobj.fill();

  } else {

  cobj.stroke();

  }

}

canvas图形函数