如何缩放我的SVG内部元素以适合窗口的宽度和高度

时间:2019-07-07 11:29:29

标签: javascript svg svg-animate

请检查此演示。

https://codepen.io/wasiabbas/pen/JQmXmE

当花括号从左边来时,我需要使其适合高度,当它旋转时,我需要将花括号适合窗口宽度。我该怎么办?

var winWidth = window.innerWidth,
        winHeight = window.innerHeight;

    const bracesContainer = document.querySelector('.braces-container');
    const braces = document.getElementById('braces');
    const bracesbg = document.getElementById('bracesbg');
    const leftbraces = document.getElementById('leftcb');
    const topbraces = document.getElementById('topcb');

    function loadAnimation(){

        $(braces).css({
            backgroundColor: "#000",
            width: winWidth,
            height: winHeight
        });

        TweenMax.to(braces, 0.9, {
            right:0
        });

        TweenMax.to(leftbraces, 0.9, {
            opacity: 0,
            rotation: -90,
            y: '60%',
            x: '10%',
            transformOrigin: 'center center',
            delay: .5
        });


        TweenMax.to(topbraces, 1.2, {
            opacity:1,
            delay: 1
        });

    }

    $(document).ready(function(){
        loadAnimation();
    });

1 个答案:

答案 0 :(得分:0)

下面是一个简单的解决方案的片段(使用D3.js):

const svg = d3.select('svg');
const width = parseInt(svg.attr('width'));
const height = parseInt(svg.attr('height'));

svg.select('path')
	.transition()
    .delay(500)
	.duration(1000)
	.attr('transform', `translate(0, ${width}) rotate(-90) scale(${width/height})`)
svg {
	background-color: #000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>

<svg width='250' height='200'>
<path d='M 0,0 H 100 Q 125,0 125,25 V 75 Q 125,100 150,100 
Q 125,100 125,125 V 175 Q 125,200 100,200 H 0 z' fill='#fff' />
</svg>