1

我正在使用 HTML5 和 javascript。每当我绘制任何形状时,我都会制作画布。

我有一个正在显示文档的画布。如果我正在绘制矩形,那么我将添加一个不同的画布(作为要求)。

var canvas= document.createElement('canvas');
canvas.style.position = "absolute";
canvas.id=this.annotationID;

canvas.style.left=""+this.x+"px";
canvas.style.top=""+this.y+"px";
canvas.height=this.h;
canvas.width=this.w;

document.getElementById("mainDIV").appendChild(canvas);

 var ctx = canvas.getContext("2d");
 ctx.strokeStyle='black';
 ctx.lineWidth=4;
 ctx.strokeRect(0,0,this.w,this.h);

通过使用上面的代码。我在做画布。

现在如果我要调整这个画布的大小。它没有调整大小。我可以改变它的位置,但高度和宽度没有更新。我使用以下方式:-

 selectedAnnCanvas.style.left=currentAnnLeft+"px";
 selectedAnnCanvas.style.top=currentAnnTop+"px";
 selectedAnnCanvas.setAttribute('width', currenAnnWidth);
 selectedAnnCanvas.setAttribute('height', currentAnnHeight);

提前非常感谢。

编辑------调整画布代码

这里我正在绘制 8 个选择点来调整大小,移动形状。以下是8个案例:-

if (isResizeDrag) {

    var annLeft=selectedAnnCanvas.style.left;
    annLeft=annLeft.substring(0, annLeft.length - 2);

    var annTop=selectedAnnCanvas.style.top;
    annTop=annTop.substring(0,annTop.length - 2);

    var annWidth=selectedAnnCanvas.width;

    var annHeight=selectedAnnCanvas.height;

    var oldx = annLeft;
    var oldy = annTop;

    var POS=getMousePos(canvas,e);
    mx=POS.x;
    my=POS.y;

//      alert('rdy');
    switch (expectResize) {
    case 0:
        if((annLeft+annWidth>mx) && (annTop+annHeight>my))
        {
            annLeft = mx;
            annTop = my;
            annWidth += oldx - mx;
            annHeight += oldy - my;


            selectedAnnCanvas.style.left=annLeft+"px";
            selectedAnnCanvas.style.top=annTop+"px";

            selectedAnnCanvas.setAttribute('width', annWidth);
            selectedAnnCanvas.setAttribute('height', annHeight);


        }
        break;
      case 1:
          if((annLeft+annWidth>mx) && (annTop+annHeight>my))
          {
              annTop = my;
              annHeight += oldy - my;

              selectedAnnCanvas.style.top=""+annLeft+"px";
              selectedAnnCanvas.height=annHeight;

          }
        break;
      case 2:
          if((annLeft<mx) && (annHeight+annTop>my))
          {
              annHeight = my;
              annWidth = mx - oldx;
              annHeight += oldy - my;

              selectedAnnCanvas.width=annWidth;
              selectedAnnCanvas.height=annHeight;
          }
          break;
      case 3:
          if((annLeft+annWidth>mx))
          {
              annLeft = mx;
              annWidth+= oldx - mx;

              selectedAnnCanvas.style.left=""+annLeft+"px";
              selectedAnnCanvas.width=annWidth;
          }
        break;
      case 4:
          if((annLeft<mx))
          {
              annWidth = mx - oldx;
              selectedAnnCanvas.width=annWidth;
          }
        break;
      case 5:
          if((annLeft+annWidth>mx) && (annTop<my))
          {
              annLeft = mx;
              annWidth += oldx - mx;
              annHeight = my - oldy;

              selectedAnnCanvas.style.left=""+annLeft+"px";
              selectedAnnCanvas.width=annWidth;
              selectedAnnCanvas.height=annHeight;
          }
        break;
      case 6:
          if((annTop<my))
          {
              annHeight = my - oldy;

              selectedAnnCanvas.height=annHeight;
          }
        break;
      case 7:
          if(annLeft<mx && annTop<my)
          {
              annWidth = mx - oldx;
              annHeight = my - oldy;

              selectedAnnCanvas.width=annWidth;
              selectedAnnCanvas.height=annHeight;
          }
        break;
    }
    invalidate();
  }
4

0 回答 0