【发布时间】:2010-10-02 13:50:36
【问题描述】:
有没有办法翻转 SVG 坐标系,使 [0,0] 在左下角而不是左上角?
【问题讨论】:
标签: svg coordinate-systems cartesian
有没有办法翻转 SVG 坐标系,使 [0,0] 在左下角而不是左上角?
【问题讨论】:
标签: svg coordinate-systems cartesian
我做了很多实验,唯一合乎逻辑的方法如下:
<g transform="translate(0,400)">
<g transform="scale(1,-1)">
其中 400 是图像的高度。这是做什么的,它将所有内容向下移动,以便图像的顶部现在是图像的底部,然后缩放操作翻转 Y 坐标,以便现在离开页面/图像的位被翻转回来以填充留下的空间。
【讨论】:
viewbox="-200 -200 400 400" 实现了这一点。
matrix 转换而不是单独的 translate 和 scale 转换。要翻转 y 轴,请尝试 <g transform="matrix(1 0 0 -1 0 400">(400 是图像的高度)。
我发现的用于转换为笛卡尔坐标系的最佳组合非常简单:
css
svg.cartesian {
display:flex;
}
/* Flip the vertical axis in <g> to emulate cartesian. */
svg.cartesian > g {
transform: scaleY(-1);
}
/* Re-flip all <text> element descendants to their original side up. */
svg.cartesian > g text {
transform: scaleY(-1);
}
<html>
<head></head>
<body>
<svg class="cartesian" viewBox="-100 -100 200 200" preserveAspectRatio="xMidYMid meet">
<g>
<!-- SVG Start -->
<!-- Vertical / Horizontal Axis: Can be removed if you don't want x/y axes. -->
<path d="M0 -100 V 200" stroke="green" stroke-width="0.5" stroke-opacity="0.5" />
<path d="M-100 0 H 200" stroke="green" stroke-width="0.5" stroke-opacity="0.5" />
<!-- Plotting: This is an example plotting two points at (20, 20) and (-50, -35), replace it with your data. -->
<g transform="translate(20, 20)">
<circle r="1" />
<text>(20, 20)</text>
</g>
<g transform="translate(-50, -35)">
<circle r="0.5" />
<text>(-50, -35)</text>
</g>
<!-- SVG End -->
</g>
</svg>
</body>
</html>
这将通过 css scaleY(-1) 自动取消翻转页面上的所有文本元素。
【讨论】:
我知道这是旧的,但我在做同样的事情,尝试了@Nippysaurus 版本,但这太烦人了,因为所有东西都会被旋转(所以如果你放图像,你必须把它们旋转回来)。不过还有另一种解决方案
我所做的只是移动 svg 的 viewBox 并反转 y 轴上的所有坐标(并将对象的高度也移除到其左下角),例如:
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="300" viewBox="0 -300 200 300">
<rect x="20" y="-40" width="20" height="20" stroke="black" stroke-width="1px"></rect>
</svg>
这会将rect 放置在距离svg 左下角20,20 处,参见http://jsfiddle.net/DUVGz/
【讨论】:
transform 解决方案都是 PIA,因为它对文本的影响等...,所以这是我为 SVG 输出采用的解决方案。
是的,坐标旋转 -90,然后平移 + 新图形的高度就可以了。 W3C 有一个例子。
【讨论】:
如果您不知道 svg 的大小,则可以对整个 SVG 元素使用 CSS 转换:
#parrot {
transform-origin: 50% 50%; /* center of rotation is set to the center of the element */
transform: scale(1,-1);
}
学分: https://sarasoueidan.com/blog/svg-transformations/#transforming-svgs-with-css
【讨论】:
<g transform="translate(0,400) scale(1,-1)">
也相当于下面的
<g transform="scale(1,-1) translate(0,-400) ">
【讨论】:
另一种方法是使用 D3 v4 scaleLinear 创建一个将为您执行交换的函数。
import * as d3 from "d3";
...
// Set the height to the actual value to where you want to shift the coords.
// Most likely based on the size of the element it is contained within
let height = 1;
let y = d3.scaleLinear()
.domain([0,1])
.range([height,0]);
console.log("0 = " + y(0)); // = 1
console.log("0.5 = " + y(0.5)); // = 0.5
console.log("1 = " + y(1)); // = 0
console.log("100 = " + y(100)); // = -99
console.log("-100 = " + y(-100)); // = 101
【讨论】:
我通常所做的是将坐标绘制/放置在笛卡尔平面上 y 轴的负值处。稍后通过将y轴的负值替换为正值来传递坐标。
【讨论】:
我认为将元素旋转 180 度的最简单方法是旋转 180.1 度;
transform="translate(180.1,0,0)"
【讨论】: