How can I avoid d3.zoom translate on first zoom or pan?











up vote
0
down vote

favorite












I try to make an orgchart with zoom and paning. To do so I need to call .nodeSize which translate my "root" to (0,0). Then I re-translate it by width/2 and it's ok. But when I implement d3.zoom, my "root" go back to (0,0)... How can i avoid this behavior ?



Here is the code concerned :



var svg = d3.select("#svg1").append("svg")
.attr("width", width)
.attr("height", height)
.call(d3.zoom().on("zoom", function () {
svg.attr("transform", d3.event.transform);
}))
.append('g').attr("transform","translate(" + width / 2 + "," + 0 + ")");


Thanks for reading.



EDIT : I did a JSFiddle so you can try my current code. Don't pay attention to big traingles (I don't have them on my real project) =>
https://jsfiddle.net/rhz1n8m4/12/










share|improve this question









New contributor




Zoom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote

    favorite












    I try to make an orgchart with zoom and paning. To do so I need to call .nodeSize which translate my "root" to (0,0). Then I re-translate it by width/2 and it's ok. But when I implement d3.zoom, my "root" go back to (0,0)... How can i avoid this behavior ?



    Here is the code concerned :



    var svg = d3.select("#svg1").append("svg")
    .attr("width", width)
    .attr("height", height)
    .call(d3.zoom().on("zoom", function () {
    svg.attr("transform", d3.event.transform);
    }))
    .append('g').attr("transform","translate(" + width / 2 + "," + 0 + ")");


    Thanks for reading.



    EDIT : I did a JSFiddle so you can try my current code. Don't pay attention to big traingles (I don't have them on my real project) =>
    https://jsfiddle.net/rhz1n8m4/12/










    share|improve this question









    New contributor




    Zoom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I try to make an orgchart with zoom and paning. To do so I need to call .nodeSize which translate my "root" to (0,0). Then I re-translate it by width/2 and it's ok. But when I implement d3.zoom, my "root" go back to (0,0)... How can i avoid this behavior ?



      Here is the code concerned :



      var svg = d3.select("#svg1").append("svg")
      .attr("width", width)
      .attr("height", height)
      .call(d3.zoom().on("zoom", function () {
      svg.attr("transform", d3.event.transform);
      }))
      .append('g').attr("transform","translate(" + width / 2 + "," + 0 + ")");


      Thanks for reading.



      EDIT : I did a JSFiddle so you can try my current code. Don't pay attention to big traingles (I don't have them on my real project) =>
      https://jsfiddle.net/rhz1n8m4/12/










      share|improve this question









      New contributor




      Zoom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I try to make an orgchart with zoom and paning. To do so I need to call .nodeSize which translate my "root" to (0,0). Then I re-translate it by width/2 and it's ok. But when I implement d3.zoom, my "root" go back to (0,0)... How can i avoid this behavior ?



      Here is the code concerned :



      var svg = d3.select("#svg1").append("svg")
      .attr("width", width)
      .attr("height", height)
      .call(d3.zoom().on("zoom", function () {
      svg.attr("transform", d3.event.transform);
      }))
      .append('g').attr("transform","translate(" + width / 2 + "," + 0 + ")");


      Thanks for reading.



      EDIT : I did a JSFiddle so you can try my current code. Don't pay attention to big traingles (I don't have them on my real project) =>
      https://jsfiddle.net/rhz1n8m4/12/







      d3.js






      share|improve this question









      New contributor




      Zoom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Zoom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited Nov 8 at 10:57





















      New contributor




      Zoom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 8 at 9:59









      Zoom

      33




      33




      New contributor




      Zoom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Zoom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Zoom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Apply the zoom to a node below the width/2 transform.



          Rename your vars to reflect what they contain: svg does not contain the svg element.



          var svg = d3.select("#svg1").append("svg")
          .attr("width", width)
          .attr("height", height)
          .call(d3.zoom().on("zoom", function () { svg.attr("transform", d3.event.transform); }))
          .append('g').attr("transform","translate(" + width / 2 + "," + 0 + ")")
          .append('g');





          share|improve this answer





















          • THANK YOU! It works, I will analyse carefully why, I'm totally new to JavaScript AND D3. Thank you again for your time and your answer !
            – Zoom
            Nov 8 at 12:02










          • Just to be complete, this works BUT the center of the zoom seems to be translated by width/2 now. I will inestigate !
            – Zoom
            Nov 8 at 12:07











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Zoom is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205333%2fhow-can-i-avoid-d3-zoom-translate-on-first-zoom-or-pan%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          Apply the zoom to a node below the width/2 transform.



          Rename your vars to reflect what they contain: svg does not contain the svg element.



          var svg = d3.select("#svg1").append("svg")
          .attr("width", width)
          .attr("height", height)
          .call(d3.zoom().on("zoom", function () { svg.attr("transform", d3.event.transform); }))
          .append('g').attr("transform","translate(" + width / 2 + "," + 0 + ")")
          .append('g');





          share|improve this answer





















          • THANK YOU! It works, I will analyse carefully why, I'm totally new to JavaScript AND D3. Thank you again for your time and your answer !
            – Zoom
            Nov 8 at 12:02










          • Just to be complete, this works BUT the center of the zoom seems to be translated by width/2 now. I will inestigate !
            – Zoom
            Nov 8 at 12:07















          up vote
          0
          down vote



          accepted










          Apply the zoom to a node below the width/2 transform.



          Rename your vars to reflect what they contain: svg does not contain the svg element.



          var svg = d3.select("#svg1").append("svg")
          .attr("width", width)
          .attr("height", height)
          .call(d3.zoom().on("zoom", function () { svg.attr("transform", d3.event.transform); }))
          .append('g').attr("transform","translate(" + width / 2 + "," + 0 + ")")
          .append('g');





          share|improve this answer





















          • THANK YOU! It works, I will analyse carefully why, I'm totally new to JavaScript AND D3. Thank you again for your time and your answer !
            – Zoom
            Nov 8 at 12:02










          • Just to be complete, this works BUT the center of the zoom seems to be translated by width/2 now. I will inestigate !
            – Zoom
            Nov 8 at 12:07













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          Apply the zoom to a node below the width/2 transform.



          Rename your vars to reflect what they contain: svg does not contain the svg element.



          var svg = d3.select("#svg1").append("svg")
          .attr("width", width)
          .attr("height", height)
          .call(d3.zoom().on("zoom", function () { svg.attr("transform", d3.event.transform); }))
          .append('g').attr("transform","translate(" + width / 2 + "," + 0 + ")")
          .append('g');





          share|improve this answer












          Apply the zoom to a node below the width/2 transform.



          Rename your vars to reflect what they contain: svg does not contain the svg element.



          var svg = d3.select("#svg1").append("svg")
          .attr("width", width)
          .attr("height", height)
          .call(d3.zoom().on("zoom", function () { svg.attr("transform", d3.event.transform); }))
          .append('g').attr("transform","translate(" + width / 2 + "," + 0 + ")")
          .append('g');






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 11:17









          rioV8

          3,4032210




          3,4032210












          • THANK YOU! It works, I will analyse carefully why, I'm totally new to JavaScript AND D3. Thank you again for your time and your answer !
            – Zoom
            Nov 8 at 12:02










          • Just to be complete, this works BUT the center of the zoom seems to be translated by width/2 now. I will inestigate !
            – Zoom
            Nov 8 at 12:07


















          • THANK YOU! It works, I will analyse carefully why, I'm totally new to JavaScript AND D3. Thank you again for your time and your answer !
            – Zoom
            Nov 8 at 12:02










          • Just to be complete, this works BUT the center of the zoom seems to be translated by width/2 now. I will inestigate !
            – Zoom
            Nov 8 at 12:07
















          THANK YOU! It works, I will analyse carefully why, I'm totally new to JavaScript AND D3. Thank you again for your time and your answer !
          – Zoom
          Nov 8 at 12:02




          THANK YOU! It works, I will analyse carefully why, I'm totally new to JavaScript AND D3. Thank you again for your time and your answer !
          – Zoom
          Nov 8 at 12:02












          Just to be complete, this works BUT the center of the zoom seems to be translated by width/2 now. I will inestigate !
          – Zoom
          Nov 8 at 12:07




          Just to be complete, this works BUT the center of the zoom seems to be translated by width/2 now. I will inestigate !
          – Zoom
          Nov 8 at 12:07










          Zoom is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          Zoom is a new contributor. Be nice, and check out our Code of Conduct.













          Zoom is a new contributor. Be nice, and check out our Code of Conduct.












          Zoom is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205333%2fhow-can-i-avoid-d3-zoom-translate-on-first-zoom-or-pan%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          Schultheiß

          Liste der Kulturdenkmale in Wilsdruff

          Android Play Services Check