Luckysheet [Feature request]单位格对角线

0sgqnhkj  于 5个月前  发布在  其他
关注(0)|答案(2)|浏览(77)

表格有单元格内设置对角斜线的需求,左上到右下或右上到左下。

llycmphe

llycmphe1#

/**

  • @description 画斜线和字符
  • @param x0:画布左上x坐标,y0左上y坐标,x1:右下x,y1:右下y,sheetTableContent:画布context,splitNum分隔多少次,splitText:分隔字符串,isLT:是否从左上角起点画线
  • @return null
    */
    function drawObliqueLine(x0, y0, x1, y1,sheetTableContent,strokeColor,splitNum,splitText,isLT){
    sheetTableContent.beginPath();
    sheetTableContent.strokeStyle=strokeColor;
    sheetTableContent.font="8px Georgia"
    sheetTableContent.fillStyle=strokeColor;
    const s0=splitNum-(Math.round(splitNum/2)-1);
    if(isLT){
    for(let i=0;i<=splitNum;i++){
    sheetTableContent.moveTo(x0, y0);
    const fSplitNum=Math.floor(splitNum/2);
    const i0=Math.round((i+1)/2);
    const i1=Math.round((i+2)/2);
    const t0=i0/s0;
    const t1=t0+(1/(fSplitNum+1));
    const t2=t0-(1/(fSplitNum+1));
    const sy=y0+(y1-y0)*t0;
    const sx=x0+(x1-x0)*t0;
    const sy1=y0+(y1-y0)*t1;
    const sx1=x0+(x1-x0)*t1;
    const sy2=y0+(y1-y0)*t2;
    const sx2=x0+(x1-x0)*t2;
    const ly0=locate(x0,y0,x1,y0,x1,sy);
    const lx0=locate(x0,y0,x0,y1,sx,y1);
    const ly1=locate(x0,y0,x1,y0,x1,sy1);
    const lx1=locate(x0,y0,x0,y1,sx1,y1);
    const ly2=locate(x0,y0,x1,sy,x1,sy2);
    const lx2=locate(x0,y0,sx,y1,sx2,y1);
    // 画对角线
    if(i % 2 == 0){
    // 画字符串
    if(splitNum%2==0&&i==splitNum) {
    sheetTableContent.fillText(splitText[i], (lx0.lastX + ly1.lastX) / 2, (lx0.lastY + ly1.lastY) / 2);
    } else if(i==0){
    sheetTableContent.fillText(splitText[i],lx0.lastX,lx0.lastY)
    }else{
    sheetTableContent.fillText(splitText[i],lx2.lastX,lx2.lastY)
    }
    //画线,防止多画一跟斜线
    if(i!==splitNum){
    sheetTableContent.lineTo(x1,sy);
    }
}else{
          // 画字符串
          if(i==splitNum) {
              if(splitNum%2==0){
                  sheetTableContent.fillText(splitText[i],ly0.lastX,ly0.lastY)
              }else{
                  sheetTableContent.fillText(splitText[i],ly2.lastX,ly2.lastY)
              }
          }else {
              sheetTableContent.fillText(splitText[i],ly2.lastX,ly2.lastY)
          }

          //画线,防止多画一跟斜线
          if(i!==splitNum){
              sheetTableContent.lineTo(sx,y1);
          }
      }
  }

}else{
for (let i = 0; i <=splitNum; i++) {
sheetTableContent.moveTo(x1, y1);
const fSplitNum=Math.floor(splitNum/2);
const i0 = Math.round((i + 1) / 2);
const t0=i0/s0;
const t1=t0+(1/(fSplitNum+1));
const t2=t0-(1/(fSplitNum+1));
const sy=y1 - (y1 - y0) * t0;
const sx=x1 - (x1 - x0) * t0;
const sy1=y1 - (y1 - y0)*t1;
const sx1=x1 - (x1 - x0)*t1;
const sy2=y1 - (y1 - y0)*t2;
const sx2=x1 - (x1 - x0)*t2;
const ly0=locate(x1,y1,x0,y1,x0,sy);
const lx0=locate(x1,y1,x1,y0,sx,y0);
const ly1=locate(x1,y1,x0,y1,x0,sy1);
const lx1=locate(x1,y1,x1,y0,sx1,y0);
const ly2=locate(x1,y1,x0,sy,x0,sy2);
const lx2=locate(x1,y1,sx,y0,sx2,y0);
if (i % 2 == 0) {
// 画字符串
if(splitNum%2==0&&i==splitNum) {
sheetTableContent.fillText(splitText[i], (ly0.lastX + lx1.lastX) / 2, (ly0.lastY + lx1.lastY) / 2);
} else if(i==0){
sheetTableContent.fillText(splitText[i],lx0.lastX,lx0.lastY)
}else{
sheetTableContent.fillText(splitText[i],lx2.lastX,lx2.lastY)
}

if(i!==splitNum) {
              sheetTableContent.lineTo(x0, sy)
          }
      } else {
          // 画字符串
          if(i==splitNum) {
              if(splitNum%2==0){
                  sheetTableContent.fillText(splitText[i],ly0.lastX,ly0.lastY)
              }else{
                  sheetTableContent.fillText(splitText[i],ly2.lastX,ly2.lastY)
              }
          }else {
              sheetTableContent.fillText(splitText[i],ly2.lastX,ly2.lastY)
          }

          if(i!==splitNum) {
              sheetTableContent.lineTo(sx, y0)
          }
      }
  }

}
sheetTableContent.stroke();
sheetTableContent.closePath()
}

// 调用方法
drawObliqueLine(start_c+ offsetLeft, start_r+offsetTop, end_c+ offsetLeft, end_r+offsetTop,sheetTableContent,' #999 ',10,[0,1,2,3,4,5,6,7,8,9,10],false);

iq3niunx

iq3niunx2#

//三角形内心
function locate(x0, y0, x1, y1, x2, y2) {
var c=Math.sqrt((x0-x1)(x0-x1)+ (y0-y1)(y0-y1));
var a=Math.sqrt((x1-x2)(x1-x2)+ (y1-y2)(y1-y2));
var b=Math.sqrt((x2-x0)(x2-x0)+ (y2-y0)(y2-y0));
var lastX=(ax0+bx1+cx2)/(a+b+c);
var lastY=(a
y0+by1+cy2)/(a+b+c);
return {lastX,lastY}
}

相关问题