svg使用

1、正常使用

<svg ref="svgRef">
    <!-- 绘制多边形 -->
    <polygon points="100,100 150,100 150,150 100,150" fill="red" stroke="black" stroke-width="1"></polygon>
    <!-- 绘制线条 -->
    <line x1="100" y1="100" x2="150" y2="150" stroke="black" stroke-width="1"></line>
    <!-- 编写文本 -->
    <text x="50" y="50" fill="black" font-size="18" text-anchor="middle">
        {{ item.text}}
    </text>
</svg>

2、动态使用

<template>
     <svg ref="svgRef">
         <!-- 编写文本 -->
         <text v-for="(item, index) in texts" :key="index" :x="item.style.left" :y="item.style.top" fill="black" font-size="18" text-anchor="middle">
            {{ item.text }}
         </text>
     </svg>
</template>

<script>
    export default {
        methods: {
            mark_border() {
                const svg = this.$refs.svgRef;
                // 绘制多边形
                const polygon = document.createElementNS("http://www.w3.org/2000/svg", "polygon");
                // 设置多边形的顶点坐标
                polygon.setAttribute("points", "100,100 150,100 150,150 100,150");
                // 设置多边形的内部颜色
                polygon.setAttribute("fill", "red");

                // 设置多边形的边框颜色和宽度
                polygon.setAttribute("stroke", "black");
                polygon.setAttribute("stroke-width", "1");

                // 将多边形添加到SVG元素中
                svg.appendChild(polygon);

                // 绘制线条
                const line= document.createElementNS("http://www.w3.org/2000/svg", "line");
                // 设置线条的起点坐标
                line.setAttribute("x1", "50");
                line.setAttribute("y1", "50");
                // 设置线条的终点坐标
                line.setAttribute("x2", "100");
                line.setAttribute("x2", "100");


                // 设置多边形的边框颜色和宽度
                line.setAttribute("stroke", "black");
                line.setAttribute("stroke-width", "1");

                // 将多边形添加到SVG元素中
                svg.appendChild(line);
            }
        }
    }
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值