美文网首页
vue 放大缩小 svg 图形(原理类似整个列表更新)

vue 放大缩小 svg 图形(原理类似整个列表更新)

作者: Mr菜头 | 来源:发表于2019-05-14 09:30 被阅读0次

原料 :vue+element-ui+svg

目的:让svg 图形在点击按钮后放大和缩小

html

  <template>
    <div class="svg-map">
        <div>
            <el-button type="primary" icon="el-icon-plus" circle v-on:click="zoomin()"></el-button>
            <el-button type="success" icon="el-icon-minus" circle v-on:click="zoomout()"></el-button>
        </div>
        <div>
          <svg view-box="0 0 700 800" width="100%" height="800" v-on:mousemove="dragSVG()">
              <rect v-for="station in stations" :key="station.index" :x="station.x" :y="station.y" :width="station.w" :height="station.h" 
              style="fill:#7daf7d;stroke:#b5acac;stroke-width:2"/>
              <text>1</text>
          </svg>
        </div>
    </div>
</template>

script

//stations: 需要绘制的站台
export default {
  name: 'svgMap',
  data() {
    return {
      stations:[ {num:1,x:50,y:20,w:20,h:40},{x:75,y:20,w:20,h:40},{x:100,y:20,w:20,h:40},
                {x:125,y:20,w:20,h:40},{x:150,y:20,w:20,h:40},{x:175,y:20,w:20,h:40},
                {x:200,y:20,w:20,h:40},{x:225,y:20,w:20,h:40}]
    }
  },
  methods: {
    zoomin(){//放大
      this.stations = this.draw(this.stations,1.1);
    },
    zoomout(){//缩小
      this.stations = this.draw(this.stations,0.9);//同时更新站台信息,才会更新页面
    },
    draw(pointArray,factor){//根据变化倍数更新svg的坐标
      var newPointArray = [];
      for(var i=0;i<pointArray.length;i++){//所有坐标都乘以系数,就可完成放大
        var point = {};
        point.x = pointArray[i].x*factor;
        point.y = pointArray[i].y*factor;
        point.w = pointArray[i].w*factor;
        point.h = pointArray[i].h*factor;
        newPointArray.push(point);
      }
      return newPointArray;
    }
  }
}

注意
直接更新vue的数据列表,页面的数据是不会更新的。需要一起同时更新,才会更新页面数据

以下是不会更新页面的写法

   for(var i=0;i<this.stations.length;i++){
    this.stations[i].x = this.station[i].x*factor
    this.stations[i].y = this.station[i].y*factor
    this.stations[i].w = this.station[i].w*factor
    this.stations[i].h = this.station[i].h*factor
  }

相关文章

  • vue 放大缩小 svg 图形(原理类似整个列表更新)

    原料 :vue+element-ui+svg 目的:让svg 图形在点击按钮后放大和缩小 html script ...

  • 【python】SVG映射反爬

    SVG简述说明 SVG是用于描述二维矢量图形的一种图形格式。它基于XML描述图形,对图形进行放大或缩小操作都不会影...

  • iOS 展示SVG(SVGKit的使用)

    近期公司的移动端项目的icon都在使用svg(矢量图).svg在放大或者缩小的情况下,图像的图形质量不会有所损失....

  • 3 HTML5 svg

    SVG:Scalable Vector Graphics;//可缩放矢量图形 ->svg图像放大情况下图形质量不会...

  • 在React组件中使用本地SVG文件

    关于SVG SVG可被非常多的工具读取和修改(比如vscode) 不失真, 放大缩小图像都很清晰 SVG文件是纯粹...

  • SVG长话短说

    一、何为SVG? SVG是可伸缩的矢量图形,用来定义用于网格的基于矢量的图形,它使用XML格式定义图形,图像在放大...

  • HTML笔记18:HTML5 SVG&地理位置

    一、SVG SVG是什么 可伸缩矢量图形 定义网络的基于矢量的图形 使用XML格式定义图形 在放大或改变尺寸的情况...

  • 温暖的金小仙林湖(2020.11.16)

    【放大缩小,皆为智慧】 小学数学中有一教学内容,为《图形的放大与缩小》。概括其特征,为将原来图形按照一定比例予以变...

  • SVG 简介

    基础 学习SVG之前需要掌握: HTML 与 XMLSVG参考手册:SVG元素列表 SVG 指可伸缩矢量图形 (S...

  • iOS SVG及相关使用

    近日,发现安卓同学在项目中使用的都是SVG(矢量图)的图片。 那么,什么是svg呢?svg在放大或者缩小的情况下,...

网友评论

      本文标题:vue 放大缩小 svg 图形(原理类似整个列表更新)

      本文链接:https://www.haomeiwen.com/subject/ersjaqtx.html