简体   繁体   English

AngularJS-SVG拖动旋转的元素

[英]AngularJS - SVG drag a rotated Element

I'm having an issue with a rotated SVG Element with an own coordinate space. 我在旋转的SVG元素具有自己的坐标空间时遇到问题。 The normal drag and drop doesn't work. 正常的拖放操作无效。 For example: The element moves right instead of up and left instead of down. 例如:元素向右移动而不是向上移动,向左移动而不是向下移动。 I looking for a simple solution to use a normal drag and drop for SVG Elements with different degree settings. 我正在寻找一种简单的解决方案,以对具有不同程度设置的SVG元素使用常规拖放。

A demo can be seen here http://jsfiddle.net/jmdajkkb/ 可以在这里查看演示http://jsfiddle.net/jmdajkkb/

HTML-View: HTML视图:

<div ng-app>
    <div ng-controller="myController">
        <svg style="height: 612px; width: 612px;" ng-mouseup="mouseUp()" ng-mousemove="mouseMove($event)">
            <rect fill="orange" width="100" height="100" ng-mousedown="elementMouseDown($event)"  ng-attr-x="{{element.x}}" ng-attr-y="{{element.y}}" ng-attr-transform="rotate({{element.rotation.degree}}, {{element.rotation.x}}, {{element.rotation.y}})" />
        </svg>
    </div> 
</div>

JavaScript: JavaScript:

function myController($scope) {
    $scope.element = {
                "x": 350,
                "y": 50,
                "rotation": {
                    "degree": 60,
                    "x": 50,
                    "y": 50
                }
            };

    var isDragging = false, x, y;    

    $scope.elementMouseDown = function(eventArgs){
        isDragging = true;
        x = eventArgs.clientX;
        y = eventArgs.clientY;
    };

    $scope.mouseMove = function(eventArgs) {
        if(isDragging) {
            $scope.element.x += eventArgs.clientX - x;
            $scope.element.y += eventArgs.clientY - y;

            x = eventArgs.clientX;
            y = eventArgs.clientY;
        };
    };

    $scope.mouseUp = function() {
        isDragging = false;
    };
}

By the way, should the "Drag and Drop" logic be placed in the controller or in a directive? 顺便说一下,应将“拖放”逻辑放在控制器中还是在指令中?

UPDATE 更新

Yes, with additional translate in transform it works. 是的,可以在转换中使用其他翻译。 But in my project we have a little more complicated scenario. 但是在我的项目中,我们有一个稍微复杂的场景。

The user can insert more elements (rectangles) at run time. 用户可以在运行时插入更多元素(矩形)。 He can select one element by clicking on it and use drag & drop or rotate features. 他可以通过单击来选择一个元素,并使用拖放或旋转功能。 The selection Elements are available in a separate group with circles. 选择元素可在带有圆圈的单独组中使用。

Here`s the new code to get a better view: http://jsfiddle.net/kjaxdf8c/ 这是获得更好视图的新代码: http : //jsfiddle.net/kjaxdf8c/

I have found solution. 我找到了解决方案。 Here is code, it works: 这是代码,它可以工作:

   app.directive('svgInternal', function () {
   var telo ='<g ng-mousedown="elementMouseDown($event)" ' +
   'ng-attr-transform=" translate({{element.x}},{{element.y}})'+
   'rotate({{element.rotation.degree}},'+
   '{{element.rotation.x}}, {{element.rotation.y}})">'+
   '<rect class="rect" fill="orange" width="100" height="100"'+
   ' '+
   '  />'+
   '<g class="rect" ng-show="isSelected" ng-attr-transform=" translate({{ 
   -element.x}},{{-element.y}})">'+ 

          '<circle fill="#3EA3DC" stroke-width="2" ng-attr-cx="
         {{elementSelections[0].x}}" ng-attr-cy="{{elementSelections[0].y}}" 
   r="8" />'+
            '<circle fill="#3EA3DC" stroke-width="2" ng-attr-cx="
   {{elementSelections[1].x}}" ng-attr-cy="{{elementSelections[1].y}}" r="8" 
   />'+
            '<circle fill="#3EA3DC" stroke-width="2" ng-attr-cx="
   {{elementSelections[2].x}}" ng-attr-cy="{{elementSelections[2].y}}" r="8"  
   />'+
            '<circle fill="#3EA3DC" stroke-width="2" ng-attr-cx="
   {{elementSelections[3].x}}" ng-attr-cy="{{elementSelections[3].y}}" r="8" 
  />'+

        '</g><text x="0" y="15" fill="red">{{element.x}}</text></g>';

return {
  templateNamespace: 'svg',
  template: telo,
  restrict: 'E',
  replace: true
};

}); });

Best regards from Goran 来自Goran的问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM