【问题标题】:scale SVG pathPoints缩放 SVG 路径点
【发布时间】:2020-04-08 21:03:39
【问题描述】:

我正在缩放一个多边形并将实际缩放的点设置到 pathArray 中

const pointsCal = this.findPoints(allocatedTable.tableType.shape.pathArray);//calculating max x,y min x,y of pathArray
    const diameterX = (pointsCal.highX - pointsCal.lowX)/2;
    const diameterY = (pointsCal.highY - pointsCal.lowX)/2;
    const scalex = (diameterX + this.settings.tableTableSpace) / diameterX;
    const scaleY = (diameterY + this.settings.tableTableSpace) / diameterY;
pathArray.forEach((point) => {
      if (point.command !== 'z') {
        point.x -= tableCenterPoint.x;
        point.y -= tableCenterPoint.y;
        point.x *= scalex;
        point.y *= scaleY;
        point.x += tableCenterPoint.x;
        point.y += tableCenterPoint.y;[enter image description here][1]
      }
    });

但对于常规矩形,它可以正常工作,但对于旋转的形状,它不能正确粘贴

我认为我在计算 scale X 和 scaleY 值时在逻辑上犯了错误

【问题讨论】:

    标签: svg path


    【解决方案1】:

    为什么要除以 2?

    试试这样的:

    const width = pointsCal.highX - pointsCal.lowX;
    const height = pointsCal.highY - pointsCal.lowY;
    const scalex = this.settings.tableTableSpace / width;
    const scaleY = this.settings.tableTableSpace / height;
    

    如果这不起作用,那么您需要提供minimal workable example

    更新

    我仍然不能 100% 确定您想要什么。但是查看代码,我假设您想要缩放原始形状以填充 SVG。但也允许在它周围进行一些填充。

    如果是这样,你会想要做这样的事情:

      DrawScalledRectangle() {
        // Get the size of the original polygon
        const bbox = this.tableGroup.getBBox();
        // Get the size of the <svg> element.
        // This will be the value at the time this function is run. But the <svg> has width
        // and height of "100%" so it may change if the window is resized.
        const mainSvg = document.getElementById("mainSVG");
        const svgWidth = mainSvg.clientWidth;
        const svgHeight = mainSvg.clientHeight;
    
        // The scale will be svgSize / originalSize (but we subtract the padding from the svgSize first)
        const scaleX = (svgWidth - this.tableTableSpace * 2) / bbox.width;
        const scaleY = (svgHeight- this.tableTableSpace * 2) / bbox.height;
    
    
        this.pathArray.forEach(point => {
          if (point.command !== "z") {
            // New point location = padding + (oldPoint - shapePosition) * scale
            point.x = this.tableTableSpace + (point.x - bbox.x) * scaleX;
            point.y = this.tableTableSpace + (point.y - bbox.y) * scaleY;
          }
        });
        console.log(this.pathArray);
        ...snip...
      }
    

    https://stackblitz.com/edit/angular-thb9w5?file=src/app/app.component.ts

    【讨论】:

    • 谢谢,你能帮忙解释一下为什么我们从 X 值中减去 y 值,得到 heightconst height = pointsCal.highY - pointsCal.low;
    • 这是一个错字,来自您的原始代码,我错过了。我现在已经修好了。
    • stackblitz.com/edit/angular-z3ivot,正如你问我的,已经简化了代码,但上面的逻辑对我不起作用,你能帮我统一扩展吗
    • 均匀地在边缘之间分配相同的空间
    • 非常感谢,但我需要将形状设置为被调用形状的中心点,然后形状的所有边缘的填充将相同
    猜你喜欢
    • 2018-07-18
    • 2017-05-08
    • 2014-11-12
    • 1970-01-01
    • 2012-04-22
    • 2021-07-21
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多