StraMa87

Zoom D3 js

Jun 15th, 2021 (edited)
866
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export class Component implements OnInit, AfterContentInit {
  2.  
  3.     @ViewChild("maps", {static:true}) maps:ElementRef;
  4.    
  5.     svgEl:any;
  6.     base:number = 1000;
  7.     altezza:number = 600;
  8.     altezzaMappa:number = 5;
  9.     larghezzaMappa:number = 6;
  10.     ConversioneAltezza:number=0;
  11.     ConversioneBase:number=0;
  12.     scala:{b:number,h:number};
  13.     posizioneXAncora:number;
  14.     posizioneYAncora:number;
  15.     valoreDiScala:number;
  16.     entitaAncora:IAnchor;
  17.     entitaArea
  18.  
  19.  
  20.     constructor() { }
  21.  
  22.     ngAfterContentInit(): void {
  23.         this.ShowMap();
  24.     }
  25.  
  26.     ngOnInit() : void {
  27.     }
  28.  
  29.     /**
  30.      * Inserisce la mappa
  31.      * @returns {void}
  32.      */
  33.  
  34.     ShowMap() :void {
  35.  
  36.         this.scala = this.Conversione( this.altezzaMappa, this.larghezzaMappa );
  37.  
  38.         // =============================================================
  39.         // CREAZIONE DELL'SVG
  40.         // =============================================================
  41.        
  42.         this.svgEl = d3.select(this.maps.nativeElement)
  43.                         .append("svg")
  44.                         .attr("id", "svg")
  45.                         .attr("viewBox", `0 0 ${this.scala.b} ${this.scala.h}`)
  46.                         .attr("preserveAspectRatio", "xMidYMid meet")
  47.                         .attr("width", "100%")
  48.                         .attr("height", "100%")
  49.                         .style("max-width", this.scala.b)
  50.                         .style("max-height", this.scala.h)
  51.                         .html(esempio);
  52.        
  53.         d3.xml("../../../../../assets/images/map.txt", function(data) {
  54.             console.log(data);
  55.            
  56.         })
  57.        
  58.         console.log(d3.select("#svg").style("width"));
  59.        
  60.  
  61.         // =============================================================
  62.         // ZOOM
  63.         // =============================================================
  64.        
  65.         const zoom = d3.zoom()
  66.                        .scaleExtent([1, 10])
  67.                        .on("zoom", function(){
  68.                            
  69.                         d3.select("svg#svg").selectAll("*")
  70.                           .attr("transform", d3.event.transform.toString());
  71.                        });
  72.        
  73.  
  74.  
  75.  
  76.         d3.select("#zoom_in").on("click", function(){
  77.             zoom.scaleBy(d3.transition().duration(750), 1.2);
  78.         });
  79.  
  80.         d3.select("#zoom_out").on("click", function(){
  81.             zoom.scaleBy(d3.transition().duration(750), .8);
  82.         });
  83.  
  84.         this.svgEl.call(zoom, d3.zoomIdentity.translate(0,0).scale(.2)).on("dblclick.zoom", null);
  85.        
  86.         this.svgEl.on("mousemove.zoom", null);
  87.  
  88.         this.AnchorPosition();
  89.        
  90.     }
  91.  
  92.     /**
  93.      * Handler per attivare lo Zoom
  94.      * @returns {void}
  95.      */
  96.  
  97.     zoomHandler() :void {
  98.  
  99.         d3.select("svg#svg").selectAll("*")
  100.           .attr("transform", d3.event.transform.toString());
  101.     }
  102.  
  103.     /**
  104.      * Conversione delle misure nel formato grafico predefinito
  105.      * @param h parametro in altezza da convertire
  106.      * @param w parametro in larghezza da convertire
  107.      * @returns {b:number, h:number}
  108.      */
  109.     Conversione( h:number, w:number ) :{b:number, h:number} {
  110.         const unitaAltezza = this.altezza / h;
  111.         const unitaBase = this.base / w;
  112.  
  113.        if( unitaBase <= unitaAltezza){
  114.            this.valoreDiScala = unitaBase;
  115.            return {b:this.base, h:unitaBase * h};
  116.        } else {
  117.            this.valoreDiScala = unitaAltezza;
  118.            return {b:unitaAltezza * w, h:this.altezza };
  119.        }
  120.  
  121.     }
  122.  
  123.     /**
  124.      * Generazione dell'ancora al click dell'utente
  125.      * @var mouse Array con posizione X - Y al click del mouse
  126.      * @var ancora Shape ancora Generata al click
  127.      * @var dragAnchor funzione che consente di posizionare l'ancora in un punto a piacere dell'area
  128.      * @returns {void}
  129.      */
  130.  
  131.     AnchorPosition(){
  132.        let self = this;
  133.        
  134.        
  135.        this.svgEl.on( "click", function(){
  136.          
  137.             const mouse = d3.mouse(this);
  138.  
  139.             // =============================================================
  140.             // CREAZIONE DELL'ANCORA
  141.             // =============================================================
  142.            
  143.             const ancora = d3.select("#svg")
  144.                             .append("svg")
  145.                             .attr("height", 35)
  146.                             .attr("width", 35)
  147.                             .attr("xmlns", "http://www.w3.org/2000/svg")
  148.                             .attr("viewBox", "0 0 64 64")
  149.                             .attr("id", "anchor")
  150.                             .attr("transform", `translate(${mouse[0]}, ${mouse[1]}) scale(0)`)
  151.                             .attr("x", mouse[0])
  152.                             .attr("y", mouse[1])
  153.                             .html(anchor);
  154.  
  155.                
  156.            
  157.             ancora.transition()
  158.                   .duration(750)
  159.                   .attr("transform", "translate(" + mouse[0] + "," + mouse[1] + ") scale(1)")
  160.                   .attr("x", mouse[0])
  161.                   .attr("y", mouse[1])
  162.                   .attr("transform", "scale(1)");
  163.  
  164.             ancora.on("click", function(){
  165.                 if(d3.event.ctrlKey){
  166.                     ancora.transition()
  167.                           .duration(500)
  168.                           .attr("transform", `translate( ${ancora.attr("x")}, ${ancora.attr("y")} ), scale(0)`)
  169.                           .remove();
  170.  
  171.                     d3.event.stopPropagation();
  172.                 }
  173.             });
  174.  
  175.  
  176.  
  177.             ancora.on("mouseover", function(){
  178.                 if(d3.event.ctrlKey){
  179.                     d3.select(this).style("cursor", "crosshair");
  180.                 } else {
  181.                     d3.select(this).style("cursor", "default");
  182.                 }
  183.             });
  184.  
  185.             // =============================================================
  186.             // DRAG & DROP DELLE ANCORE
  187.             // =============================================================
  188.  
  189.             const dragAnchor= d3.drag()
  190.                                 .on("start", function(){
  191.                                     d3.select("#anchor")
  192.                                       .style("cursor", "grab")
  193.                                 })
  194.                                 .on("drag", function(){
  195.                                     d3.select("#anchor")
  196.                                       .style("cursor", "grab")
  197.                                     d3.select(this)
  198.                                         .attr("x", d3.event.x )
  199.                                         .attr("y", d3.event.y )
  200.                                 })
  201.                                 .on("end", function(){
  202.                                     d3.select("#anchor")
  203.                                       .style("cursor", "grab")
  204.                                       this.posizioneXAncora = d3.event.x;
  205.                                       this.posizioneYAncora = d3.event.y;
  206.                                       console.log("x: " + this.posizioneXAncora, "y: " + this.posizioneYAncora , "scala: " + self.valoreDiScala);                                      
  207.                                       self.ConvertAnchorPosition( this.posizioneXAncora, this.posizioneYAncora, self.valoreDiScala);
  208.                                 });
  209.            
  210.             ancora.call(dragAnchor);
  211.            
  212.             d3.event.stopPropagation();
  213.  
  214.         } );
  215.  
  216.     }
  217.  
  218.     ConvertAnchorPosition(x:number, y:number, scala:number) :void {
  219.         const PosX = x / scala;
  220.         const PosY = y / scala;
  221.  
  222.         console.log(PosX, PosY);
  223.        
  224.     }
  225.    
  226. }
  227.  
  228. // HTML
  229.  
  230. <div class="content-card" style="padding: 0 24px 0 24px;">
  231.      <div class="content" id="content" #maps fusePerfectScrollbar>
  232.           <div class="zoom">
  233.                <button id="zoom_in" type="button">+</button>
  234.                <button id="zoom_out" type="button">-</button>
  235.           </div>
  236.      </div>
  237. </div>
Add Comment
Please, Sign In to add comment