【问题标题】:Is it possible to set negative width in svg rectangle?是否可以在 svg 矩形中设置负宽度?
【发布时间】:2013-03-13 21:43:05
【问题描述】:

我想根据鼠标移动来移动矩形。请参考以下链接。

http://atomicrobotdesign.com/blog_media/draw/draw1.html

在 mousedown 事件中获取矩形开始位置并开始拖动它将在鼠标移动事件中创建矩形。但是当我移动到上一个值时(即移动到小于鼠标按下的值意味着它将返回负值)所以宽度变为负值。

上面的链接是画布矩形。但我创建了具有相同逻辑的 svg 矩形。

不支持矩形的负宽度?或者我如何根据鼠标移动移动矩形?

怎么了?

我的代码 sn-p。

ChartMouseDown:function(e){

//       e = this.normalizeMouseEvent(e);


      var mousedownCords=this.calMousePosition(e);
      this.mouseDownX=mousedownCords.X;
      this.mouseDownY=mousedownCords.Y;

      this.chartMouseDownPoint= this.GetValuebyPoint(Math.abs(this.mouseDownX-this.model.m_AreaBounds.X),Math.abs(this.mouseDownY-(this.model.m_AreaBounds.Y + this.model.m_AreaBounds.Height)));
      this.zoomingX=true;

    },



ChartMouseMove: function (evt) {

    if( this.zoomingX)
    {

    var mouseMoveX,mouseMoveY;

     var mouseMoveCords=this.calMousePosition(evt);
      mouseMoveX=mouseMoveCords.X;
      mouseMoveY=mouseMoveCords.Y;
      $(this.ZoomAreaRect).remove();
      var width =  Math.abs(mouseMoveX - this.mouseDownX);
      var height = mouseMoveY - this.mouseDownY;
      var x;
      if(mouseMoveX > this.mouseDownX)
      x= this.mouseDownX;
      else
      x= mouseMoveX;

       this.ZoomAreaRect = document.createElementNS("http://www.w3.org/2000/svg", "rect");

        $(this.ZoomAreaRect).attr({
            'id': 'ZoomArea', 'x': x, 'y': this.model.m_AreaBounds.Y, 'width': width, 'height': this.model.m_AreaBounds.Height,
            'fill': 'gray', 'stroke-width': 1, 'stroke':'gray'
        });

       $(this.ZoomAreaRect).appendTo(this.SvgObject);

    }


 calMousePosition:function(e)
    {
     var matched = jQuery.uaMatch( navigator.userAgent );
     var browser = {};
     var mouseX,mouseY;
     if(matched.browser.toLowerCase()=="mozilla")
        {
        mouseX = (e.pageX)-  $(this.SvgObject).parent().offset().left;
        mouseY=  (e.pageY) - $(this.SvgObject).parent().offset().top;
        }
        else
        {
        mouseX =  (e.pageX +document.documentElement.scrollLeft)-$(this.SvgObject).offset().left;
        mouseY =  (e.pageY + document.documentElement.scrollTop) - $(this.SvgObject).offset().top;
        }

        return { X: mouseX, Y:mouseY};

    },

谢谢,

湿婆

【问题讨论】:

    标签: svg jquery-svg rect


    【解决方案1】:

    您可以使用多边形而不是矩形,点为

    points={`${x},${y} ${x + width},${y} ${x + width},${y + height} ${x},${y + height}`}
    

    【讨论】:

      【解决方案2】:

      如果您有两个坐标数组 start 和 end,其中 (start[0],start[1]) 是左上角(或右下角),(end[0],end[1]) 是左上角(或右下角),那么这是绘制该矩形的算法,它不关心是否这些坐标被切换

                    <rect
                      fill="rgba(255,0,0,0.3)"
                      x={Math.min(start[0], end[0])}
                      y={Math.min(start[1], end[1])}
                      width={Math.abs(start[0] - end[0])}
                      height={Math.abs(start[1] - end[1])}
                    />
      

      【讨论】:

        【解决方案3】:

        如果您想在 SVG 中实现这一点,您必须确保矩形的宽度/高度始终为正,并相应地计算 x/y。

        【讨论】:

        • 现在的问题是如何用 SVG 实现同样的效果?
        • @JSmith 你总是可以问另一个问题,但如果你仔细考虑的话应该很明显。
        • 我只找到了一个复杂的解决方案。虽然会有一个更简单的。干杯
        猜你喜欢
        • 2013-12-20
        • 1970-01-01
        • 2020-07-25
        • 2023-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-20
        • 2023-04-03
        相关资源
        最近更新 更多