- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
上下文:@AnaTudor (@daredevil) is talking about使用 skewX
或 skewY
时 SVG 的 d
移动距离,所以我想知道是否有任何方法可以按顺序计算该距离补偿翻译,并避免使用链式 translate
。
测试用例:在下面的代码片段中,我们不会使用特定于 SVG 的链接和/或嵌套,我们仅使用相同的变换值,但按照变换函数的特定顺序;
style
:translate
、rotate
、skewX
、 skewY
, scale
; transform
属性:translate
、scale
、rotate
, skewX
, `skewY.现在,如您所见,两个矩形的位置不同,如果您单击按钮,第二个矩形会更接近我们的预期,但仍需要针对所有情况进行更多计算。
问题:我们如何更改 fixOrigin
函数来调整所有可能的变换函数组合的翻译,以一种看起来与 CSS3 变换相同的方式?
var el1 = document.querySelectorAll('path')[0],
el2 = document.querySelectorAll('path')[1],
el2BB = el2.getBBox(), el2cx = el2BB.x + el2BB.width/2, el2cy = el2BB.y + el2BB.height/2,
btn = document.querySelectorAll('button')[0], btn1 = document.querySelectorAll('button')[1],
x = 20, y = 20, scale = 0.6, rotate = 45, skewX = 20, skewY = -20;
el1.style.transform = 'translate(20px, 20px) rotate(45deg) skewX(20deg) skewY(-20deg) scale(0.6)';
el1.style.transformOrigin = '50% 50% 0px';
el2.setAttribute('transform', 'translate('+x+','+y+') scale('+scale+') rotate('+rotate+' '+el2cx+','+el2cy+') skewX('+skewX+') skewY('+skewY+')');
function fixOrigin(){
x += (1-scale) * el2BB.width/2;
y += (1-scale) * el2BB.height/2;
el2.setAttribute('transform', 'translate('+x+','+y+') scale('+scale+') rotate('+rotate+' '+el2cx+','+el2cy+') skewX('+skewX+') skewY('+skewY+')');
}
btn.addEventListener('click',fixOrigin,false);
function fixEverything() {
// scale binds all transform functions together
if ( !!scale ) {
//most important make sure we have translation values
//!!(x) && (x=0); !!(y) && (y=0);
// first adjust translate based on scale value
x += (1-scale) * el2BB.width/2;
y += (1-scale) * el2BB.height/2;
//now we also adjust the rotation transform origin based on SKEWS
if (!!rotate) {
// el2cx += .... el2cy += ...
}
//almost there, now we adjust the translation based on SKEWS
// x += ... y += ...
// last case, when SKEWS are used alone
} else if ( !scale && !rotate ) {
// adjust translation here
// x += ... y += ...
}
el2.setAttribute('transform', 'translate(' + x + ',' + y + ') scale(' + scale + ') rotate(' + rotate + ' ' + el2cx + ',' + el2cy + ') skewX(' + skewX + ') skewY(' + skewY + ')');
}
btn1.addEventListener('click', fixEverything, false);
/* desired transform
transform-origin: 50% 50% 0px;
transform: translate(20px, 20px) rotate(45deg) skewX(20deg) skewY(-20deg) scale(0.6);
*/
svg {
overflow: visible; width:30%;
border: 1px solid #eee; /* some sort of ruler */
}
<button>Fix Transform Origin</button><button>Fix All</button><br>
<p>Click to change the `transform` attribute</p>
<svg id="svgMixedCSS" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 600 500">
<path fill="green" d="M426.671 0h-341.328c-46.937 0-85.343 38.405-85.343 85.345v341.311c0 46.969 38.406 85.344 85.343 85.344h341.328c46.938 0 85.329-38.375 85.329-85.345v-341.31c0-46.94-38.391-85.345-85.329-85.345z" ></path>
</svg>
<svg id="svgMixedAttr" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 600 500">
<path fill="indigo" d="M426.671 0h-341.328c-46.937 0-85.343 38.405-85.343 85.345v341.311c0 46.969 38.406 85.344 85.343 85.344h341.328c46.938 0 85.329-38.375 85.329-85.345v-341.31c0-46.94-38.391-85.345-85.329-85.345z"></path>
</svg>
我制作了一个超酷的片段供您玩玩。更新:它还有一个包含所有可能情况的功能草案。
最佳答案
由于您目前的核心问题似乎是如何计算 d
距离。它来自一个谈话,其中对于 skewX
的情况,倾斜操作基本上被描述为 (x, y) ↦ (x + d, y) .如果你看the SVG spec你会发现一个矩阵,它基本上说明了 skewX(a)
的确切公式是(x, y) ↦ (x + cos(a) y, y)
.同样对于 skewY(a)
你有(x, y) ↦ (x, y + cos(a) x)
.所以我会说 d := cos(a) * y
在第一种情况下和 d := cos(a) * x
在第二种情况下。
不过,问题标题向我提出了一个不同的问题。该问题可以表述如下:给定 transform="skewX(c) translate(a, b)"
对于一些数字 a
, b
和 c
, 找到 d
和 e
这样我刚才给出的转换与 translate(d, e) skewX(c)
相同.或者换句话说:我必须如何更改 transform
的条目?如果我想将转换移动到 skewX
的外部.
要找到这些数字,请查看相应的矩阵乘积,如defined in the spec :
⎡1 tan(c) 0⎤ ⎡1 0 a⎤ ⎡1 tan(c) a + tan(c) b⎤ ⎡1 0 a + tan(c) b⎤ ⎡1 tan(c) 0⎤
⎢0 1 0⎥∙⎢0 1 b⎥ = ⎢0 1 b ⎥ = ⎢0 1 b ⎥∙⎢0 1 0⎥
⎣0 0 1⎦ ⎣0 0 1⎦ ⎣0 0 1 ⎦ ⎣0 0 1 ⎦ ⎣0 0 1⎦
所以你会得到 d = a + tan(c) * b
和 e = b
.您只需将倾斜变换应用于平移向量。换句话说:
skewX(c) translate(a, b) = translate(a + tan(c) * b, b) skewX(c)
您可以对 y
进行类似的计算并获得:
skewY(c) translate(a, b) = translate(a, b + tan(c) * a) skewY(c)
如果你同时拥有 skewX
和 skewY
合并后,您可以移动 translate
一次走出一步,这样在每一步你只需要处理一个倾斜方向。如果你想要相反的方向(即移动 translate
更接近倾斜的内部),使用 - tan(c)
而不是 + tan(c)
在这些公式中。
关于javascript - 在 SVG 上使用 skewX 和 skewY 时如何补偿平移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39191054/
我看到以下关于 MPEG 1 运动估计/补偿的解释,只是想知道它是否正确: 为什么我们不直接编码当前 block 和引用 block 之间的原始差异?因为残差的数字通常会小很多。例如,假设一个物体在图
计算机屏幕上像素的亮度通常与像素的数字 RGB 三元组值不线性相关。早期 CRT 的非线性响应需要补偿非线性编码,我们今天仍在继续使用此类编码。 通常我们在计算机屏幕上生成图像并在那里使用它们,所以一
我不知道它叫什么。状态栏?随着接待和电池生命周期的增加。但是有没有一种简单的方法可以判断它是否显示在应用程序中? 我正在编写一些通用代码,如果没有栏,我希望将某些内容放置在屏幕顶部,如果有,则在栏下方
观察下面的简单示例: div { border-bottom: 1px solid black; border-radius: 20%; padding: 10px; } Test 在 S
我有一个这样的嵌套视频: Live camera feed 当用户拍照时,图像沿y轴偏移 Captured Still image 我确实想要捕获整个图像并让用户上下滚动。他们目前可以这样做,但我希望
我从客户端收到了一个字体 (Calibre) 作为 OTF 文件,用于网络应用程序,但文件的“行高”(或者你怎么调用它?)有些奇怪。实际文本溢出顶部的 dom 元素并在下面留下一堆空间,导致各种丑陋。
我编写了一个数学计算器,它从用户那里接收一个字符串并对其进行解析。它使用 double 来保存计算时涉及的所有值。解决后,我将其打印出来,并使用 std::setprecision() 确保其输出正确
不同的 Android 设备具有不同的屏幕尺寸和屏幕密度。编写视频游戏时,补偿差异的好方法是什么? 最佳答案 我们先从 Android 的文档开始 Supporting Multiple Screen
我正在制作个人资料图片裁剪编辑器,它允许在区域内拖动、缩放和旋转图像。 图片的拖动是通过捕捉区域的mousedown和mousemove事件,计算区域内游标开始和停止的x/y坐标,得到游标移动的距离。
我正在尝试按地理区域对 AnyLogic GISRegions 集合进行排序。所述面积是使用GISRegion.area(units)计算的,这很简单。然而,我使用的区域是城市规模的,并且该方法返回一
首先,我希望这不是重复的。我读过很多类似的问题,但找不到与此特定问题相关的问题。 我有一个 javascript 日期选择器,它在内部使用 javascript 日期,这会产生意想不到的副作用。当我选
您可以在此处查看实时版本:http://steffiwilson.com/test . IE9 没有将内容 block 居中。 (我很确定旧版本的 IE 也不能正常工作,但我不能肯定地说。)我已经确保
我是一名优秀的程序员,十分优秀!