当我们将鼠标移动到中心 div(MouseOver 效果)时,我正在尝试使用 css3 和 javascript 实现以下效果
我创建了一个小型库,它接受 3 个参数元素、源点、目标点并返回 css3D 矩阵和更新元素。这是我的 javascript 代码。
var BLEND = BLEND || {};
BLEND.Util = BLEND.Util || {};
function print(msg) {
console.log(msg);
}
BLEND.Util.VendorPrefix = "";
BLEND.Util.DetectVendorPrefix = function() {
var styles = window.getComputedStyle(document.documentElement, ''),
pre = (Array.prototype.slice.call(styles).join('').match(/-(moz|webkit|ms)-/) || (styles.OLink === '' && ['', 'o']))[1];
BLEND.Util.VendorPrefix = pre[0].toUpperCase() + pre.substr(1) + "Transform";
}
BLEND.Util.DetectVendorPrefix();
BLEND.TransformElement = function(elm, src, dest) {
var L = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]];
var R = [0, 0, 0, 0, 0, 0, 0, 0];
for (var i = 0; i < 4; i++) {
L[i] = [];
L[i][0] = L[i + 4][3] = src[i].x;
L[i][2] = L[i + 4][4] = src[i].y;
L[i][2] = L[i + 4][5] = 1;
L[i][3] = L[i][4] = L[i][5] = L[i + 4][0] = L[i + 4][3] = L[i + 4][2] = 0;
L[i][6] = -src[i].x * dest[i].x;
L[i][7] = -src[i].y * dest[i].x;
L[i + 4][6] = -src[i].x * dest[i].y;
L[i + 4][7] = -src[i].y * dest[i].y;
R[i] = dest[i].x;
R[i + 4] = dest[i].y;
}
var RM = [];
for (i = 0; i < R.length; i++) {
RM[i] = [R[i]];
}
var Left = Matrix.create(L);
var Right = Matrix.create(R);
var res = Matrix.calculate(Left, Right);
print (res);
if (BLEND.Util.VendorPrefix == 'WebkitTransform') {
var matrix3D = new CSSMatrix();
matrix3D.m11 = res.get(0,0);
matrix3D.m12 = res.get(3,0);
matrix3D.m13 = 0;
matrix3D.m14 = res.get(6,0);
matrix3D.m21 = res.get(1,0);
matrix3D.m22 = res.get(4,0);
matrix3D.m23 = 0;
matrix3D.m24 = res.get(7,0);
matrix3D.m31 = 0;
matrix3D.m32 = 0;
matrix3D.m33 = 1;
matrix3D.m34 = 0;
matrix3D.m41 = res.get(2,0);
matrix3D.m42 = res.get(5,0);
matrix3D.m43 = 0;
matrix3D.m44 = 1;
elm.style.webkitTransform = matrix3D;
} else {
if (BLEND.Util.VendorPrefix === "")
BLEND.Util.DetectVendorPrefix();
elm.style[BLEND.Util.VendorPrefix] = "matrix3d(" + res.get(0,0) + "," + res.get(3,0) + ", 0," + res.get(6,0) + "," + res.get(1,0) + "," + res.get(4,0) + ", 0," + res.get(7,0) + ",0, 0, 1, 0," + res.get(2,0) + "," + res.get(5,0) + ", 0, 1)";
}
}
更新: Here is JSFiddle
我正在为 9 个具有适当源和目标坐标的 div 中的每一个调用 TransformElement 方法。但它没有按预期工作。请提出可能的解决方案。 无论如何我们可以使用 three.js 来做到这一点吗(只是问可能是它愚蠢的想法)?
更新:我们可以使用 CSS3D 渲染器和 Three.js 来实现吗?
想法是创建平面并将其切片为 3x3 网格,将鼠标悬停在平面的每个面上,我们可以向上缩放该 div,并且我们必须根据当前 div 分别缩放其他 div?可能吗?
最佳答案
我没有尝试使用您的库,但这是我对您的问题的解决方案:http://codepen.io/bali_balo/pen/87b980a2acf722b1c9feb35f3fcb1c65/ (我用的是Haml和SCSS,你可以点击每个面板右上角的小眼睛看到编译后的代码)
我用了this article编写这段代码。
首先计算 9 个矩阵(对应于悬停的图 block 和每个周围的图 block ),使用之前链接的文章中提供的 numericjs 和公式。然后在鼠标悬停时将这些矩阵应用于相应的图 block 。这是获取变换矩阵的代码,知道变换前后的 4 个点的位置:
//from and to are arrays of 4 objects containing a property x and a property y
//describing the 4 points of the quadrilateral to transform
function getTransform(from, to)
{
var A = [], i;
for(i = 0; i < 4; i++)
{
A.push([from[i].x, from[i].y, 1, 0, 0, 0, -from[i].x * to[i].x, -from[i].y * to[i].x]);
A.push([0, 0, 0, from[i].x, from[i].y, 1, -from[i].x * to[i].y, -from[i].y * to[i].y]);
}
var b = [];
for(i = 0; i < 4; i++)
{
b.push(to[i].x);
b.push(to[i].y);
}
//Uses numericjs to solve matrices equations
//returns h knowing A and b where A.h = b
var h = numeric.solve(A, b);
//Column major representation
return [[h[0], h[3], 0, h[6]],
[h[1], h[4], 0, h[7]],
[ 0 , 0 , 1, 0 ],
[h[2], h[5], 0, 1 ]];
}
请注意,正如我的代码中提到的那样,如果您想要动画转换,您不能只使用 CSS(因为它只会转换矩阵的每个数字,但这很少会给出适当的结果)。您可以尝试使用 javascript 来完成它,但它可能会有点慢(我没有测试它),因为您无法缓存矩阵并且必须在每一帧计算它们。
关于javascript - CSS3D矩阵生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27524938/