【问题标题】:SVG grid overlaid imageSVG 网格叠加图像
【发布时间】:2017-05-11 18:52:28
【问题描述】:

我什至不知道如何解释。

我有一个 JPG 格式的楼层布局,我想让用户鼠标悬停或点击地图的不同区域以获取有关它的动态信息。为了让地图的不同区域进行交互,我想我可以在其顶部/后面覆盖一个动态 SVG 网格,这样,我可以将动态数据分配给网格坐标。

我在执行此操作时遇到问题,我无法让网格和图像相互调整大小。此外,由于 SVG 网格是动态的,当图像更大时,默认情况下,我的网格有更多的线,这会在放大/缩小时使坐标关闭。

是否可以生成与图像匹配的固定网格(例如 20x10)并将其叠加到图像上。让它与图像一起缩放?我今天刚开始使用 SVG,因此我们将不胜感激。

Angular2 中的代码

<svg attr.width="{{w}}" attr.height="{{h}}" id="mySVG">
    <image xlink:href="../asset/building.jpg" x="0" y="0" height="100%" width="100%"/>
    <rect x="0" y="0" attr.width="{{w}}" attr.height="{{h}}" stroke="black" fill="url(#GridPattern)" stroke-width="5"
        class="hello"/>

     <defs>
        <pattern id="GridPattern" x="0" y="0" attr.width="{{wGap}}" attr.height="{{hGap}}" patternUnits="userSpaceOnUse">
          <line x1="0" y1="0" attr.x2="{{wGap}}" y2="0" stroke="lightblue" stroke-width="1px" />
          <line x1="0" y1="0" x2="0" attr.y2="{{hGap}}" stroke="lightblue" stroke-width="1px" />
        </pattern> 
    </defs>

    <g id="group1" fill="red">
    <!--<rect x="1cm" y="1cm" width="1cm" height="1cm"/>
    <rect x="3cm" y="1cm" width="1cm" height="1cm"/>-->
    <text *ngFor="let h of loc"  attr.x="{{h.x*wGap}}" attr.y="{{h.y*hGap}}" fill="red" text-anchor='middle'>{{h.text}},h:{{h.y*hGap}},w:{{h.x*wGap}}</text>
    <text *ngFor="let x of xNum; let i = index"  attr.x="{{i*wGap}}" y="20" fill="red" style="writing-mode: tb; glyph-orientation-vertical: 0;
                              letter-spacing: -3;" >{{i}}</text>
    <text *ngFor="let x of yNum; let i = index"  x="20" attr.y="{{i*hGap}}" fill="red">{{i}}</text>
  </g>



</svg>



   import {Component, OnInit,NgZone} from '@angular/core';


    @Component({
        selector: 'home',
        template: require('./home.html')
    })

    export class Home implements OnInit{
        bol:boolean = true;

        w:number;
        h:number;
        wGap:number;
        hGap:number;
        hMultiplier:number = 30;
        wMultiplier:number = 40;
        yNum = new Array(this.hMultiplier);
        xNum = new Array(this.wMultiplier);
        hLine:any;
        wLine:any;

        loc:Object[] = [{x:37,y:3,text:"testing 1"},
                        {x:31,y:25,text:"testing 2"},
                        {x:2,y:9,text:"testing 3"},
                        {x:35,y:8,text:"testing 4"},
                        {x:22,y:10,text:"testing 5"}]
        constructor(ngZone:NgZone){
            console.log(this.xNum,this.yNum)
            console.log(window.innerHeight, window.innerWidth);
            this.w = window.innerWidth-50;
            this.h = window.innerHeight-200;
             this.wGap = Math.ceil(this.w/this.wMultiplier);
                    this.hGap = Math.ceil(this.h/this.hMultiplier);

                    //////
                    //this.calculateLocation();
            window.onresize = (e)=>{
                ngZone.run(()=>{
                    console.log(window.innerWidth,window.innerHeight);
                    //this.w = document.getElementById('bldImg')['width'];
                    //this.h = document.getElementById('bldImg')['height'];
                    this.w = window.innerWidth-50;
                    this.h = window.innerHeight-200;
                    this.wGap = Math.ceil(this.w/this.wGap);
                    this.hGap = Math.ceil(this.h/this.hGap);

                    //////
                   // this.calculateLocation();
                    console.log(this.wGap)
                    console.log(this.hGap)
                });
            }
        }
        ngOnInit(){

        }


    }

【问题讨论】:

  • 显示您目前拥有的代码,以便我们知道您哪里出错了。
  • 罗伯特,我发布了代码,我得到了在图像顶部生成的网格,唯一的程序是缩放,网格间距保持不变。
  • 您是否考虑过使用image map?这将是一个更简单的解决方案。
  • 我会调查的,以前从未听说过。谢谢保罗

标签: html svg


【解决方案1】:

我设法使用 svg 标记并使用 Angular 版本 4 创建了我自己的交互式地图,您可以在此链接中查看 plnkr - 请查看代码

<svg
  version="1.1"
  xmlns="http://www.w3.org/2000/svg" 
  xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 2000 800">
  <svg:path
    *ngFor="let country of countries; let i = index"
    [attr.data-index]="i"
    (mouseover)="countryHover = country.country_country"
    (click)="clicked(i)"
    class="blob"
    [class.selected]="hasCountry(i)"
    [attr.d]="country.country_path"
    fill="currentColor">
  </svg:path>
</svg>

Image of the Output incase if Plunker doesnt work

https://plnkr.co/edit/2heVmd7l4UokqR2OcNgm?p=preview

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2016-03-27
    相关资源
    最近更新 更多