[软件设计/软件工程] 是否可以在 svg 矩形中设置负宽度?

[复制链接]
发表于 2022-5-4 13:35:15
问题
我想根据鼠标移动移动一个矩形。请参阅下面的链接。

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

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

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

发生了什么?

我的代码片段。
  1. ChartMouseDown:function(e){

  2. //       e = this.normalizeMouseEvent(e);


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

  6.       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)));
  7.       this.zoomingX=true;

  8.     },



  9. ChartMouseMove: function (evt) {

  10.     if( this.zoomingX)
  11.     {

  12.     var mouseMoveX,mouseMoveY;

  13.      var mouseMoveCords=this.calMousePosition(evt);
  14.       mouseMoveX=mouseMoveCords.X;
  15.       mouseMoveY=mouseMoveCords.Y;
  16.       $(this.ZoomAreaRect).remove();
  17.       var width =  Math.abs(mouseMoveX - this.mouseDownX);
  18.       var height = mouseMoveY - this.mouseDownY;
  19.       var x;
  20.       if(mouseMoveX > this.mouseDownX)
  21.       x= this.mouseDownX;
  22.       else
  23.       x= mouseMoveX;

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

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

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

  30.     }


  31. calMousePosition:function(e)
  32.     {
  33.      var matched = jQuery.uaMatch( navigator.userAgent );
  34.      var browser = {};
  35.      var mouseX,mouseY;
  36.      if(matched.browser.toLowerCase()=="mozilla")
  37.         {
  38.         mouseX = (e.pageX)-  $(this.SvgObject).parent().offset().left;
  39.         mouseY=  (e.pageY) - $(this.SvgObject).parent().offset().top;
  40.         }
  41.         else
  42.         {
  43.         mouseX =  (e.pageX +document.documentElement.scrollLeft)-$(this.SvgObject).offset().left;
  44.         mouseY =  (e.pageY + document.documentElement.scrollTop) - $(this.SvgObject).offset().top;
  45.         }

  46.         return { X: mouseX, Y:mouseY};

  47.     },
复制代码

谢谢,

湿婆

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





上一篇:Dockerize 微服务未在 Eureka 服务器中注册
下一篇:Java中的字符序列VS字符串?

使用道具 举报

Archiver|手机版|小黑屋|吾爱开源 |网站地图

Copyright 2011 - 2012 Lnqq.NET.All Rights Reserved( ICP备案粤ICP备14042591号-1粤ICP14042591号 )

关于本站 - 版权申明 - 侵删联系 - Ln Studio! - 广告联系

本站资源来自互联网,仅供用户测试使用,相关版权归原作者所有

快速回复 返回顶部 返回列表