javascript - 以 x,y 为中心翻转 svg 中的图像

标签 javascript svg href flip

我正在尝试反转 svg 中的图像。我遇到了这个thread但此解决方案适用于放置在原点 (0,0) 的图像。

如果图像的宽度是100并且图像位于(0,0),那么我执行以下操作

img.setAttributeNS(null, 'transform', 'translate(100,0) scale(-1,1)');

我尝试翻转/反转放置在 (x,y) 处的图像,但图像消失了。我不清楚我应该使用什么翻译。

fiddle 是here 如果取消注释行来设置图像的属性 (x,y),图像就会消失。

我想了解比例函数的确切工作原理以及我在这里做错了什么。

最佳答案

根据 @blex 的请求并经过修改,他提出了...这是解决方案。 https://jsfiddle.net/7b25vq82/5/

您需要计算翻转位置,一旦进行计算,Q 中的初始代码就可以正常工作......

var img = document.getElementById("flip");
var xCord = 100;
var yCord = 100;
var imageSize = 100;
var flipLocation = (xCord*2 + imageSize);

img.setAttributeNS(null, 'x', xCord);
img.setAttributeNS(null, 'y', yCord);

img.setAttributeNS(null, 'transform', 'translate('+flipLocation+',0) scale(-1,1)');

祝你好运!

关于javascript - 以 x,y 为中心翻转 svg 中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42102027/

相关文章:

javascript - 将 Meteor 的 Iron Router 参数传递给模板回调

javascript - 如何让 form::select 运行脚本来更改目标,以便每次选择不同选项时样式都会发生变化?

css - HTML5 内联 SVG 与外部 CSS

html - 带有对象标签的 SVG 在 Safari 中不缩放

javascript - IE9 中@import 引用的样式表对象的 href 属性是错误的

javascript - 在 window.location 中的路径后面附加来自 Angular 的参数

javascript - 如何在javascript中叠加图像?

android - Android 中的 SVG 支持

angular - 拦截传出链接Angular 4

javascript - 作为条件和引用类型的函数