如何重新缩放连续旋转的元素以完全适合其(固定大小)容器?

阿菲

我让(矩形)图像无限旋转。旋转时,我想调整其大小(以免图像被截断)。
换句话说:我尝试总是尽可能大地显示整个图像。

当前结果,被截断。

因此,“0 度”示例是正确的,但“80 度”示例更像是:

想要的结果。

您可能已经注意到,这是作为 StreamElements-widget 执行的。这可能是不起作用的原因overflow:visible

HTML:

<div class="main-container">
    <img src="some_url">
</div>  

CSS:

.main-container {
    width:100%;
    height:100%;
    
    overflow:visible;
    
    display: grid;
    place-items: center;
}

img {
    max-width: 100%;
    
    object-fit: contain;
    
    animation: spin 5s infinite linear;
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(360deg);
    }
}

I thought that transform: ... scale(x)may work, but I wasn't able to do it without using fixed keyframe values (which seems like a unreliable solution).

mqbaka mqbaka

I think you can't get your way through this without Javascript and also transform the element in Javascript instead of CSS. Here is how I would do it, I didn't reproduce your exact example, I used 2 simple divs : one container and another one inside the first one (instead of the image) ;

const cont= document.querySelector('.main-container');
const rot= document.querySelector('.rotater');
const H= rot.offsetHeight, h= cont.offsetHeight;
const W= rot.offsetWidth;

var t=0;

var anim= setInterval(rotate, 20);
function rotate(){
    rot.style.transform= "rotate(-" + t + "deg)";
    update(t);
    t++;
}

function update(T){
    let alpha= Math.atan(H/W);
    let diagLength= Math.sqrt(
        Math.pow(H, 2) + Math.pow(W, 2));
    let theta= degToRad(T);
    theta= radTrunc(theta);
    theta= (theta> Math.PI/2 && theta< Math.PI) || (theta> 3*Math.PI/2 && theta< 2*Math.PI) ? Math.PI - theta : theta;
    let beta= (Math.PI/2) - (alpha + theta);
    let nonScaledHeight= Math.cos(beta) * diagLength;
    let ratio= Math.abs(h/nonScaledHeight);
    ratio= ratio> 1 ? 1 : ratio;
    rot.style.transform += " scale(" + ratio + ")";
}

function degToRad(angle){
 return angle * Math.PI / 180;
}

function radTrunc(angle){
    while(angle> (Math.PI * 2)){
        angle -= Math.PI*2;
    }
    return angle;
}
    .main-container{
        width: 600px;
        height: 200px;
        background: blue;
    }
    .rotater{
        width: 250px;
        height: 200px;
        background: green;
        margin: auto;
    }
<div class="main-container">
    <div class="rotater"></div>
</div>

您在函数中看到的所有计算update都是基本的三角学,我们计算内部 div 旋转时的高度:nonScaledHeight该值与外部 div 高度的比率将是内部 div 的缩放比例。degToRad将角度t从度数转换为弧度并radTrunc确保角度保持在范围内[0, 2*PI[

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

您如何获得XAML元素以进行缩放以适合其容器?

如何调整Java Script元素的大小以适合其容器窗口

如何缩放容器保持其右下角固定?

如何使用CSS调整图像大小以适合其容器

如何将地图的大小完全适合容器?

缩放DOM元素以使其适合CSS的父元素

如何缩放图像以适合容器?

调整元素大小以适合容器

缩放所有布局元素以适合横向模式

重新缩放整个面板以适合窗口大小

如何使照片适合其容器

适合其容器保留大小比例的图像

扩展元素以适合表格单元内部的其余容器

使图片元素中的图像适合其容器

CSS:如何填充包含固定大小元素的动态容器?

如何获得一个适合其内容旋转大小的容器(即网格)?

按定义的角度移动元素以将其移出其容器

如何在JS中更改字体大小以适合其容器?

如何缩放视频大小以适合iframe?

动态调整div的CSS缩放以适合容器的大小

如何调整比 div 容器更大的图像以完全适合其中(自动调整大小、缩小等...)

Flutter:如何移动,旋转和缩放容器?

如何缩放我的 SVG 以适应其在 d3 中的容器 div 的大小?

如何使我的文字完全适合我的容器?

fabricjs在其他元素缩放时如何保持组元素的固定大小?

缩放子元素的位置,但不缩放其大小?

如何使用ImageMagick的转换来重新缩放图像,添加信箱以产生固定大小的结果?

自动调整摇摆元素的大小以适合容器的大小

如何设置svg图标以适合容器的大小?