Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent images from being cropped

I'm trying to create an animated picture which contains a lot of layers. In order to have images with right proportions on different screens I use cover css property value(I've tried it for both object-fit for images and background-size for background images). That's why my images on wide screen are croped by the browser.

The problem is that my layers are transformed(mostly rotated and moved) during the animation so there are moments when it is seen the cropped image.

Please see my example below.

How it can be prevented? Or it there some other technique?

body {
  margin: 0;
  padding: 0;
  /*Just to imitate wide screen*/
  width: 1000px;
  height: 450px;
}

#container {
  width: 100%;
  height: 100vh;
  /*Just to imitate wide screen*/
  height: 100%;
  overflow: hidden;
  position: relative;
}

.layer {
  height: 100%;
  position: absolute;
  width: calc(100% + 20px);
}

.layer img {
  height: 100%;
  width: 100%;
  object-fit: cover;
}

.gulls {  
  animation: gulls ease-in-out 13s infinite alternate;
}

@keyframes gulls {
  from {
    transform: rotate(3deg) scaleX(0.95) skew(-10deg, -10deg);
  }
  to {
    transform: rotate(-3deg) scaleX(1.05) skew(10deg, 10deg);
  }
}
<div id="container">
  <div class="layer">
    <img src="https://firebasestorage.googleapis.com/v0/b/wedding-42174.appspot.com/o/animation%2Fsky.png?alt=media&token=25033588-d58c-4616-94e9-4974ec4157a4" alt="">
  </div>
  <div class="layer gulls">
    <img src="https://firebasestorage.googleapis.com/v0/b/wedding-42174.appspot.com/o/animation%2Fgulls5.png?alt=media" />
  </div>
</div>

Currently I have this: https://jsfiddle.net/koljada/c08qdw1m/

like image 635
Roman Koliada Avatar asked Apr 24 '18 20:04

Roman Koliada


People also ask

Why are my photos getting cropped?

In most cases, the aspect ratio of the photo taken is the cause of cropping. Traditional cameras and photo sizes are in a 3:2 aspect ratio, making them perfect for a 4x6 photo sizes; however, cell phones take photos at a 4:3 aspect ratio, which shortens the photo.

How do you prevent an image from being cropped in CSS?

The top and left css attribute of the image has to be set a negative value. The image exact values can be found by using sine and cosine functions.

How do I crop a picture and keep a ratio?

To maintain the aspect ratio of the current crop region, either hold down the SHIFT key while dragging any handle, or specify an aspect ratio in the Aspect ratio box. To maintain the center point of the crop region, hold down the CTRL key while dragging any handle.

Why is Instagram cropping my pictures?

Instagram Auto-CroppingWhenever you upload an image that is of an incompatible size, Instagram will automatically crop and resize it to fit its dimensions. A lot of people don't bother about editing images and just let Instagram do it for them. However, it might make your Instagram feed look less visually appealing.


2 Answers

first of all i have to say that your birds image is a lot bigger then the birds therself (many padding around) as i see the whole image is 2048/1934...

anyway, when you use

object fit:cover he crop;

the image for save the proportion. you can using

object-fit:scale-down;

for save the proportion by scale down the image until he come inside the parent space. i add a quick exemple about how it works down here:

enter image description here

hope it is what you search for..

like image 197
Yosef Tukachinsky Avatar answered Oct 12 '22 13:10

Yosef Tukachinsky


Images can be cropped with a container div with overflow: hidden; position: relative, So you can position the image inside with position: absolute. The top and left css attribute of the image has to be set a negative value.

The image exact values can be found by using sine and cosine functions.

body {
  margin: 0;
  padding: 0;
  /*Just to imitate wide screen*/
  width: 200px;
  height: 100px;
}

#container {
  width: 100%;
  height: 100vh;
  /*Just to imitate wide screen*/
  height: 100%;
  overflow: hidden;
  position: relative;
}

.layer {
  height: 100%;
  position: absolute;
  top: -20px;
  left: -20px;
  width: calc(100% + 20px);
}

.layer img {
  height: 150%;
  width: 150%;
}

.gulls {  
  animation: gulls ease-in-out 3s infinite alternate;
}

@keyframes gulls {
  from {
    transform: rotate(3deg) scaleX(0.95) skew(-10deg, -10deg);
  }
  to {
    transform: rotate(-3deg) scaleX(1.05) skew(10deg, 10deg);
  }
}
<div id="container">
  <div class="layer gulls">
    <img src="http://via.placeholder.com/350x150/000000" />
  </div>
</div>
like image 36
androbin Avatar answered Oct 12 '22 12:10

androbin