美文网首页
SVG 绘制弧线连接线路径

SVG 绘制弧线连接线路径

作者: small_zeo | 来源:发表于2022-05-12 19:28 被阅读0次

计算连接线路径

动画.gif

  • 定义

利用SVG <path>属性,绘制链接线路经
<path> 标签用来定义路径。
下面的命令可用于路径数据:
M = moveto
L = lineto
H = horizontal lineto
V = vertical lineto
C = curveto
S = smooth curveto
Q = quadratic Belzier curve
T = smooth quadratic Belzier curveto
A = elliptical Arc
Z = closepath

  • 弧线绘制方式
export const computeLinePath = (start, end, lineOffsetY = 0) => {
    // 弧线绘制方式
    const x = Math.abs(start.x - end.x)
    const y = Math.abs(start.y - end.y)
    if (x === 0 || y === 0) {
        return `
            M ${start.x} ${start.y}
            L ${end.x} ${end.y}
        `
    }
    let dir
    let p1
    let p2

    if (end.y < start.y) {
        const offsetY = 100
        const offsetX = 150
        dir = end.x - start.x > 0 ? 1 : -1
        p1 = `${start.x + dir * offsetX}, ${start.y + offsetY}`
        p2 = `${end.x - dir * offsetX}, ${end.y - offsetY}`
        return `
            M ${start.x} ${start.y - lineOffsetY}
            C ${p1} ${p2} ${end.x} ${end.y}
        `
    }
    const OffsetXP1 = +(1 / 12 * x).toFixed(0)
    const OffsetXP2 = +(11 / 12 * x).toFixed(0)
    const offsetYP1 = +(1 / 3 * y).toFixed(0)
    const offsetYP2 = +(2 / 3 * y).toFixed(0)
    dir = start.x - end.x > 0 ? -1 : 1
    p1 = `${start.x + dir * OffsetXP1}, ${start.y + offsetYP1}`
    p2 = `${start.x + dir * OffsetXP2}, ${start.y + offsetYP2}`
    return `
        M ${start.x} ${start.y - lineOffsetY}
        C ${p1} ${p2} ${end.x} ${end.y}
    `
}
  • 绘制箭头
export const computeTrianglePath = (start, width) => {
            return `
                    M ${start.x} ${start.y}
                    l ${width / 2} 0
                    l ${-width / 2} ${width}
                    l ${-width / 2} ${-width}
                    Z
                `
        }
  • 获取绘制箭头起始位置
export const getTriangleStart = (end)  => {
            return ({ ...end, y: end.y - 10 })
}
  • 监听鼠标点击目标事件,获取起始位置
        start = { x: 0, y: 0 }
        window.addEventListener('mousedown', (event) => {
            ...
             start = {
                        x: event.x,
                        y: event.y
            }
        })
  • 监听鼠标移动事件
        window.addEventListener('mousemove', (event) => {
                      ...
                     let end = {
                            x: event.x,
                            y: event.y 
                        } 
                        this.linePath = computeLinePath(start, end, 0)
                        this.trianglePath = computeTrianglePath(getTriangleStart(end), 10)
        })
  • 页面展示
    <svg class="processSvg-area">
      <path :d="linePath"  strokeWidth="1" fill="none"  stroke='#7083ff'/>
      <path :d='trianglePath' fill='#7083ff' stroke="none" /> 
    </svg>

相关文章

  • SVG 绘制弧线连接线路径

    计算连接线路径 动画.gif 定义 利用SVG 属性,绘制链接线路经 标签用来定义路径。下面的命令可用于路径数...

  • 用svg动态绘制图形

    以此前对svg的了解,就是通过rect/circle/line……等绘制规则图形,或者利用path路径来绘制更...

  • svg path 画弧线

    svg用路径画弧线图,对于像我这样的初学菜鸟来说不太友好,看解释来说是挺费劲的,查百度的一般难以看懂,不够通俗易懂...

  • iOS绘图

    Path:路径 Arcs:弧线 画椭圆,矩形 Path:路径 参考 ios绘图基础http://liuyanwei...

  • [HTML] svg>path

    svg的path元素可以用来绘制路径 (1)HTML (2)CSS (3)JS 注:(1)M,C为指令字母,大写表...

  • SVG路径中“d”属性的A指令用来绘制圆弧

    SVG中的路径数据,即path元素的 d 属性,有一系列的路径绘制指令,其中椭圆弧指令(A)最复杂,不算椭圆弧起始...

  • android图片系列 (2) - 静态 SVG 图片

    SVG 图片是一种可支持任意缩放的图片格式,使用 xml 定义,使用 canvas 中 path 路径来完成绘制,...

  • SVG 路径 -

    元素用于定义一个路径。下面的命令可用于路径数据:M = movetoL = linetoH = horizonta...

  • Android自定义View实现中国地图(可点击)

    目录 效果展示 实现原理 该效果的实现原理是通过读取svg图像的路径并且转换为Path,然后绘制实现的。 实现步骤...

  • Android自定义控件:路径及文字

    创建路径 canvas中绘制路径利用: void drawPath (Path path, Paint paint...

网友评论

      本文标题:SVG 绘制弧线连接线路径

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