2018年4月28日 星期六

將 matrix 轉換成 scale, rotate



function getMatrix(dom) {
            debugger;

            let css = window.getComputedStyle(dom);

            let _matrix = css['transform'];



            if (/none/i.test(_matrix)) {
                // 若沒有設定 matrix
                $(dom).css({
                    transform: 'scale(1)'
                });
                // debugger;
                _matrix = css['transform'];
            }


            // 每一deg=幾度
            let RAD_TO_DEG = 180 / Math.PI;

            let reg = /matrix\((.*)\)/;

            let res = reg.exec(_matrix);
            _matrix = res[1];

            let matrix = [[],[]];
            let position = [];

            _matrix = _matrix.split(',');

            for (let i = 0; i < _matrix.length; i++) {
                let m = Math.floor(i/2);
                let n = i % 2;

                let value = _matrix[i].replace(/^\s*|\s*$/g, '');
                value = parseFloat(value, 10);
                if(i < 4){
                    matrix[m][n] = value
                }else{
                    position.push(value);
                }
            }

            console.dir(matrix);
            console.dir(position);



            return {
                translate: {
                    x: (e + 'px'),
                    y: (f + 'px')
                },
                rotate: (rotate + 'deg'),
                scale: {
                    x: scaleX,
                    y: scaleY
                }
            }
        }