剪辑路径动态转换以填充容器

时间:2019-02-10 00:14:33

标签: reactjs math scale translate clip-path

我正在处理一个React组件,我希望能够传递任何图像及其尺寸,根据x,y宽度和矩形区域的高度计算出的剪切路径。这部分我工作得很好。然后,我想将裁剪后的区域缩放回以填充div,此刻,我将这个div设为原始图像尺寸以使其简单。我的缩放比例部分计算正确,但无法计算出换算的剪切路径区域的数学值。这是我的组件(在Typescript中):


interface RootProps {
  links: Link[];
}

interface RootState {

}

class Root extends React.Component<RootProps, RootState> {
  constructor(props) {
    super(props);
    this.state = {

    }
  }

    pp = (x: number, y: number): string => {
        return x + "% " + y + "%"
    }


    renderClippedImage = (name: string, x: number, y: number, width: number, height: number) => {
        let iWidth = 600;
        let iHeight = 360;

        let p1 = {
            x: (x / iWidth) * 100,
            y: (y / iHeight) * 100
        }
        let p2 = {
            x: (x / iWidth) * 100,
            y: ((y + height) / iHeight) * 100
        }
        let p3 = {
            x: ((x + width) / iWidth) * 100,
            y: ((y + height) / iHeight) * 100
        }
        let p4 = {
            x: ((x + width) / iWidth) * 100,
            y: (y / iHeight) * 100
        }


        let clipPathString = 'polygon(' +
            this.pp(p1.x, p1.y) + ', ' +
            this.pp(p2.x, p2.y) + ', ' +
            this.pp(p3.x, p3.y) + ', ' +
            this.pp(p4.x, p4.y) + ')';

        let pX = (x) / iWidth;
        let pY = (y) / iHeight;
        let portionCoverageX = (width) / iWidth;
        let portionCoverageY = (height) / iHeight;

        let scaleX = 1;
        let scaleY = 1;
        if (portionCoverageX > 0) {
            scaleX = 1 / portionCoverageX;
        }
        if (portionCoverageY > 0) {
            scaleY = 1 / portionCoverageY;
        }

        let translateX = -(((pX * scaleX) / 2) * 100); //this doesn't work
        let translateY = 0; //similar issues getting this to work

        let pathClipping = {
            WebkitClipPath: clipPathString,
            clipPath: clipPathString,
            transform: 'translateX(' + translateX + '%) translateY(' + translateY + '%) scaleX(' + scaleX + ') scaleY(' + scaleY + ')'
        }

        console.log({
            name: name,
            pX: pX,
            pY: pY,
            portionCoverageX: portionCoverageX,
            portionCoverageY: portionCoverageY,
            scaleX: scaleX,
            scaleY: scaleY,
            translateX: translateX,
            translateY: translateY
        })

        return (
            <div style={{ textAlign: 'center', width: '100%', backgroundColor: 'lightseagreen' }}>
                <b>{name}</b><br />
                <div style={{ display: 'inline-block', width: 640, height: 360, backgroundColor: 'darkslateblue' }}>
                    <img width="640" height="360" src="https://placekitten.com/640/360" style={pathClipping} />
                </div>
            </div>
        )
    }

    render() {
        return (
            <div style={{textAlign: 'center'}}>
        <b>Original</b><br />
                <img width="640" height="360" src="https://placekitten.com/640/360" />
                <br />
                {this.renderClippedImage("one", 80, 100, 200, 100)}
                <br />
                {this.renderClippedImage("two", 50, 50, 150, 100)}
                <br />
                {this.renderClippedImage("three", 300, 10, 300, 340)}
            </div>
        )
    }

}

ReactDOM.render(<Root />, document.getElementById('mount-node'));

这是一个Codepen,在这里您可以看到,我当前的数学实际上适用于方案3,但它也需要适用于1、2和任何其他合法区域组合。

https://codepen.io/anon/pen/xMWGRO

我想解决的具体问题是:

let translateX = (((pX * iWidth) / 2)); //this doesn't work for all examples

translateY是一个类似的问题,我们将不胜感激。

1 个答案:

答案 0 :(得分:1)

我认为您可以将transform-origin设置为top left,这样可以简化转换位置和比例的计算。

let scaleX = iWidth / width;
let scaleY = iHeight / height;

let pX = x / iWidth;
let pY = y / iHeight;
let translateX = -pX * 100 * scaleX;
let translateY = -pY * 100 * scaleY;

let pathClipping = {
  WebkitClipPath: clipPathString,
  clipPath: clipPathString,
  transformOrigin: `top left`,
  transform: `
    translate(${translateX}%, ${translateY}%)
    scale(${scaleX}, ${scaleY})
  `
};

在此处查看codesandbox