【问题标题】:Rotating an image and moving it in proportion to its parents x and y旋转图像并按其父元素 x 和 y 的比例移动它
【发布时间】:2021-09-17 04:10:42
【问题描述】:

我正在尝试使用 BlazorWheelZoom 在 Blazor 中制作一个图像预览组件,然后使用自定义旋转方法,该方法可以将图像在两个方向上旋转 90 度。

通过将以下内容应用于图像元素,我可以轻松完成这项工作:

div.rotate90 img {
    -webkit-transform: rotate(90deg);
    -moz-transform: rotate(90deg);
    -o-transform: rotate(90deg);
    -ms-transform: rotate(90deg);
    transform: rotate(90deg) !important;
}

但是,当我在将图像旋转 90 度后尝试移动图像时,它会相对于自身移动图像。

像这样:

所以从它自己的角度来看它“认为”它正在向上移动,但从我的角度来看它不是。

这是我的 HTML:

<div class="rotate270 common" style="border: thin solid black; width: 510px; height: 510px; padding: 5px; background-color: lightgrey;" b-beatmol9sb="">
<!--!-->
<div class="alert-danger" b-beatmol9sb="">xxx</div>
<!--!-->
<img
    src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI1MDAiIGhlaWdodD0iNTAwIiAvPg=="
    _bl_11=""
    style="background-image: url('/Img/apples.jpg'); background-repeat: no-repeat; background-size: 500px 500px; background-position: -89px -136px; cursor: default;"
/>
</div> 

我只想让图像以与光标相同的方式移动。可以,但仅在我处于 0 度时。

【问题讨论】:

    标签: html css blazor transform webassembly


    【解决方案1】:

    使用父 &lt;div&gt; 进行移动,使用子 &lt;div&gt; 进行旋转。

    顺便说一句,如果需要,您可以考虑内联 &lt;style&gt; 而不是类。然后你可以旋转任何你喜欢的度数:

    <button @onclick="()=>AdjustX(-10)">?</button>
    <button @onclick="()=>AdjustY(-10)">?</button>
    <button @onclick="()=>AdjustX(10)">?</button>
    <button @onclick="()=>AdjustY(10)">?</button>
    <button @onclick="DoRotate">Rotate</button>
    @rotation.ToString()
    
    <div class="movethis" style="position:relative; transform: translate(@(offsetX)px, @(offsetY)px)">
        <img src="/files/icons/Logo1.png"
             style="transform: rotate(@(rotation)deg) !important;">
    </div>
    
    @code {
        int rotation = 0;
        int offsetX = 0;
        int offsetY = 0;
    
        async Task DoRotate()
        {
            rotation = (rotation + 15) % 360;
            StateHasChanged();
        }
        async Task AdjustX(int adjustment)
        {
            offsetX += adjustment;
            StateHasChanged();
        }
        async Task AdjustY(int adjustment)
        {
            offsetY += adjustment;
            StateHasChanged();
        }
    }
    

    请注意,根据 caniuse.com,当前浏览器不再需要“转换”前缀。

    【讨论】:

      【解决方案2】:

      先翻译再旋转。

      transform: translate(@($"{translateX}%"), @($"{translateY}%")) rotate(@($"{angle}deg"));
      
      <div class="image-container">
          <img src='https://source.unsplash.com/daily' class="image-control" />
      </div>
      
      <input type="range" min="-90" max="90" @bind-value="@angle" @bind-value:event="oninput" />
      <input type="range" min="-100" max="100" @bind-value="@translateX" @bind-value:event="oninput" />
      <input type="range" min="-100" max="100" @bind-value="@translateY" @bind-value:event="oninput" />
      @code {
          int angle = 0;
          int translateX = 0;
          int translateY = 0;
      }
      
      <style>
          .image-control {
              transform: translate(@($"{translateX}%"), @($"{translateY}%")) rotate(@($"{angle}deg"));
              width: 250px;
              height: 250px;
          }
      
          .image-container {
              width: 250px;
              height: 250px;
              overflow: hidden;
          }
      </style>
      

      BlazorFiddle

      【讨论】:

      • 这是什么? style Blazor 组件上的标签?这是从什么时候开始的?问题:如果您已经在其他地方定义了 .css,它是否是附加的,即您可以在组件中添加 transform 吗?
      • @Bennyboy1973 我这样做是为了将它们全部呈现在一个文件中。您也可以只使用它来定义 .css 文件可以看到的:root 变量。
      • 它会覆盖已经定义的变量吗?比如,我可以任意更改主题颜色的定义,甚至可以为其设置动画吗?
      • @Bennyboy1973 想看一些很酷的东西,将这个组件复制粘贴到一个 wasm 项目中。 github.com/BrianLParker/ComponentViewer/blob/master/…
      • 这很性感。我希望 Blazor 服务器中有一种方法可以强制某些块或变量仅在客户端运行。我即将重新使用旧的幻灯片制作应用程序,如果有平滑的旋转等等会很好,但由于延迟,我不认为没有 JS 可以做到这一点。
      猜你喜欢
      • 1970-01-01
      • 2011-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多