Javascript/jQuery 如何从矩阵值中获取比例值

标签 javascript jquery matrix scale

首先获取一个矩阵。

this.getMatrix = function(obj)
{
    var matrix = obj.css("-webkit-transform") ||
                 obj.css("-moz-transform")    ||
                 obj.css("-ms-transform")     ||
                 obj.css("-o-transform")      ||
                 obj.css("transform");
    return matrix;
};

并获取一个标度的值。

this.getScaleDegrees = function(obj)
{
    var matrix = this.getMatrix(obj),
        matrixRegex = /matrix\((-?\d*\.?\d+),\s*0,\s*0,\s*(-?\d*\.?\d+),\s*0,\s*0\)/,
        matches = matrix.match(matrixRegex);
    return matches;
};

并且获取到一个rotate的值。

this.getRotationDegrees = function(obj)
{
    var matrix = this.getMatrix(obj),
        angle = 0;

    if(matrix !== 'none') {
        var values = matrix.split('(')[1].split(')')[0].split(','),
            a = values[0],
            b = values[1];
        angle = Math.round(Math.atan2(b, a) * (180/Math.PI));
    }

    return angle;
};

现在,我遇到了一个问题。
当元素同时存在旋转和缩放时,函数“getScaleDegrees”失败。
由于“getRotationDegrees”函数正常运行,
我认为我将在函数“getRotationDegrees”的过程的帮助下编辑函数“getScaleDegrees”。

因此,问题是如何获取比例的值。

有什么好的思路或者计算方法吗?


编辑:

有一个函数可以改变缩放和旋转,每次缩放和旋转的值都不同。函数“getMatrix”返回的值变成这样。

none [no rotate & no scale]  
matrix(1.2, 0, 0, 1.2, 0, 0) [edit scale]  
matrix(0.965926, -0.258819, 0.258819, 0.965926, 0, 0) [edit rotate]  
matrix(1.3523, -0.362347, 0.362347, 1.3523, 0, 0) [edit rotate & edit scale]

最佳答案

解决方案之一。

将矩阵转换为数组(thanx eicto)

this.parseMatrix = function(_str)
{
    return _str.replace(/^matrix(3d)?\((.*)\)$/,'$2').split(/, /);
};

获取比例的值

this.getScaleDegrees = function(obj)
{
    var matrix = this.parseMatrix(this.getMatrix(obj)),
        scale = 1;

    if(matrix[0] !== 'none') {
        var a = matrix[0],
            b = matrix[1],
            d = 10;
        scale = Math.round( Math.sqrt( a*a + b*b ) * d ) / d;
    }

    return scale;
};

'Math.sqrt( a*a + b*b )' 引用 CSS-TRICKS .

关于Javascript/jQuery 如何从矩阵值中获取比例值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13524092/

相关文章:

Matlab:如何找到满足特定要求的矩阵的行索引?

javascript - 使用 nodeJS async-await 反复提示用户直到解决

javascript - 如果一个事件触发了随机次数,我如何在 Javascript/Jquery 中捕获最后一次?

javascript - 将一个大li附加到ul : best way?

javascript - Chrome 上的 Jquery Form.submit() 有效但在 Firefox 中无效

jquery - 出现 "Scripts may close only the windows that were opened by it"错误

python - numpy 点积但保留为矢量(不添加元素)

javascript - 使用 TypeScript 创建新的 sql.js 数据库

javascript - jQuery 动画函数的愚蠢问题

arrays - 逻辑数组 - 在赋值 A(I) = B 中,B 和 I 中的元素数必须相同