久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

D3js:使用其中一個子級拖動組

D3js: Dragging a group by using one of it#39;s children(D3js:使用其中一個子級拖動組)
本文介紹了D3js:使用其中一個子級拖動組的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

Jsfiddle:http://jsfiddle.net/6NBy2/

代碼:

var in_editor_drag = d3.behavior.drag()
             .origin(function() {
                var g = this.parentNode;
                return {x: d3.transform(g.getAttribute("transform")).translate[0],
                        y: d3.transform(g.getAttribute("transform")).translate[1]};
            })
            .on("drag", function(d,i) {

                g = this.parentNode;
                translate = d3.transform(g.getAttribute("transform")).translate;
                x = d3.event.dx + translate[0],
                y = d3.event.dy + translate[1];
                d3.select(g).attr("transform", "translate(" + x + "," + y + ")");
                d3.event.sourceEvent.stopPropagation();
            });

svg = d3.select("svg");
d = {x: 20, y: 20 };
groups = svg
        .append("g")
        .attr("transform", "translate(20, 20)");

groups
    .append("rect")
        .attr("x", 0)
        .attr("y", 0)
        .attr("width", 100)
        .attr("height", 100)
        .style("fill", "green")
        .call(in_editor_drag)
        .style("opacity", 0.4);

我正在嘗試通過使用其中一個孩子作為句柄來拖動一個組.簡單地說,我想要做的是,當一個組的孩子被拖動時:

I'm trying to drag a group by using one of it's children as a handle. Simply, what i'm trying to do is, when a groups child is dragged:

  • 獲取組的翻譯轉換
  • 從 d3.event.dx、d3.event.dy 獲取拖動距離
  • 對組的變換屬性應用差異

當拖動孩子時,組不會按預期移動.它移動的距離小于拖動的距離,并開始在這里和那里跳躍.

When child dragged, group does not move as expected. It moves less than the dragged distance, and it begins to jump here and there.

我在這里做錯了什么?

更新的 jsfiddle:http://jsfiddle.net/6NBy2/2/

Updated jsfiddle: http://jsfiddle.net/6NBy2/2/

我正在嘗試通過使用一個或多個孩子作為拖動手柄來拖動整個組.

I'm trying to drag the whole group by using one or more of it's children as dragging handles.

推薦答案

這是一個老問題,但沒有真正回答.我遇到了完全相同的問題,并且只想將組拖動一個子元素(不是 <g> 的所有子元素).問題是,d3.event.dx/y 是相對于 <g> 的位置計算的.一旦 <g>.attr(transform", translate(x, y)") 移動,d3.event.dx/dy 被調整為新的(較小的)值.這會導致不平穩的運動,大約.光標速度的一半.我為此找到了兩種可能的解決方案:

This is an old question, but not really answered. I had exactly the same problem and wanted to drag the group by only one child (not all child elements of the <g>). The problem is, that the d3.event.dx/y is calculated relatively to the position of the <g>. And as soon as the <g> is moved by .attr("transform", "translate(x, y)"), the d3.event.dx/dy is adjusted to the new (smaller) value. This results in a jerky movement with approx. the half of the speed of the cursor. I found two possible solutions for this:

首先(最后我采用了這種方法):

將拖動句柄 rect 直接附加到 svg 而不是 <g>.所以它相對于 <svg> 而不是 <g> 定位.然后在 on drag 函數中同時移動(<rect><g>).

Append the drag handle rect directly to the svg and not to the <g>. So it is positioned relatively to the <svg> and not to the <g>. Then move both (the <rect> and the <g>) within the on drag function.

var svg = d3.select("svg");
var group = svg
    .append("g").attr("id", "group")
    .attr("transform", "translate(0, 0)");
group
    .append("rect")
    .attr("x", 0)
    .attr("y", 0)
    .attr("width", 100)
    .attr("height", 100)
    .style("fill", "green")
    .style("opacity", 0.4);
group
    .append("text")
    .attr("x", 10)
    .attr("y", 5)
    .attr("dominant-baseline", "hanging")
    .text("drag me");
handle = svg
    .append("rect")
        .data([{
        // Position of the rectangle
        x: 0,
        y: 0
    }]) 
    .attr("class", "draghandle")
    .attr("x", 0)
    .attr("y", 0)
    .attr("width", 100)
    .attr("height", 20)
    .style("fill", "blue")
    .style("opacity", 0.4)
    .attr("cursor", "move")
    .call(d3.drag().on("drag", function (d) {
            console.log("yep");
            d.x += d3.event.dx;
            d.y += d3.event.dy;
            
            // Move handle rect
            d3.select(this)
                .attr("x", function (d) {
                    return d.x;
                })
                .attr("y", function (d) {
                    return d.y;
                });
            
            // Move Group
            d3.select("#group").attr("transform", "translate(" + [d.x, d.y] + ")");
    }));

<body>
    <svg width="400" height="400"></svg>
    <script src="https://d3js.org/d3.v4.min.js"></script>
</body>

第二:

使用 d3.event.sourceEvent.path[0] 檢查拖動事件期間光標在哪個元素上,僅當句柄 <rect> 時才運行拖動功能代碼>被點擊.使用這種方法,所有元素都可以在一個 <g> 中進行分組(無需在組外添加額外的 <rect>).這種方法的缺點是,如果鼠標向下移動光標到拖動手柄上,也會執行拖動.

Check on which element the cursor was during the drag event with d3.event.sourceEvent.path[0] and run the drag function only if the handle <rect> was clicked. With this approach, all elements can be grouped within one <g> (no need for an additional <rect> outside the group). The downside of this method is, that the drag is also executed, if the cursor is moved over the drag handle with mouse down.

var svg = d3.select("svg");
var group = svg
    .append("g")
    .data([{
        // Position of the rectangle
        x: 0,
        y: 0
    }])
    .attr("id", "group")
    .attr("transform", function (d) {
            return "translate(" + d.x + ", " + d.y + ")"
    })
    .call(d3.drag().on("drag", function (d) {
        if (d3.event.sourceEvent.target.classList.value === "draghandle") {
            console.log("yep");
            d.x += d3.event.dx;
            d.y += d3.event.dy;
            d3.select(this).attr("transform", function (d) {
                return "translate(" + [d.x, d.y] + ")"
            })
        } else {
            console.log("nope");
            return;
        }
    }));
group
    .append("rect")
    .attr("x", 0)
    .attr("y", 0)
    .attr("width", 100)
    .attr("height", 100)
    .style("fill", "green")
    .style("opacity", 0.4);
group
    .append("text")
    .attr("x", 10)
    .attr("y", 5)
    .attr("dominant-baseline", "hanging")
    .text("drag me");
handle = group
    .append("rect")
    .attr("class", "draghandle")
    .attr("x", 0)
    .attr("y", 0)
    .attr("width", 100)
    .attr("height", 20)
    .style("fill", "blue")
    .style("opacity", 0.4)
    .attr("cursor", "move");

<body>
    <svg width="400" height="400"></svg>
    <script src="https://d3js.org/d3.v4.min.js"></script>
</body>

這篇關于D3js:使用其中一個子級拖動組的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認為文檔“準備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發技術
What do jasmine runs and waitsFor actually do?(jasmine 運行和等待實際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對鏈式方法進行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 狠狠干美女| 亚洲三级av | 久草青青草 | 一区二区三区精品视频 | 亚洲精品在线视频 | 久久久久久久久久久成人 | 国产操操操 | 亚洲国产免费 | 国产99久久精品一区二区永久免费 | 三极网站 | 国产精品永久免费 | 久久鲁视频 | 亚洲国产精品va在线看黑人 | 久久不射网 | 国产精品免费在线 | 国产欧美日韩一区 | 男人的天堂中文字幕 | 国产精品久久久久久久久免费软件 | 国产精品日韩一区 | 欧美日韩国产一区二区 | 久久久久国产一区二区三区 | 久久一区精品 | 午夜日韩精品 | 精品国产一区二区三区久久久蜜月 | 日韩成人精品一区 | 精品日韩一区 | 中文字幕av在线一二三区 | 久久综合狠狠综合久久 | 国产日韩欧美在线 | 亚洲一区二区三区在线视频 | 91久久精品国产 | 91影院| 粉嫩粉嫩芽的虎白女18在线视频 | 欧美性视频在线播放 | 久久久蜜臀国产一区二区 | 亚洲男女视频在线观看 | 亚洲一级毛片 | 91麻豆精品国产91久久久久久 | 欧美日韩国产一区 | 欧美1页| 日韩一级免费大片 |