Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image clip centre

I want my image to be clipped in the centre and be able to move around left right up down while being on cover to fit the whole screen The user should be able to see only a certain portion of the image and be able to move around just like the link below but his viewpoint is the little rectangle in a fit to screen perspective What i get so far is just the clipped upleft of my image

In case i am not clear what I am trying to achieve this effect but the user can't see move than the square https://www.w3schools.com/howto/howto_js_image_zoom.asp

I will soon close this ticket if you happen to stumble on this check out this q I believe I am as as clear as I can here How to clip your image freely

<style>
img {
    background-position: cover;
    position: absolute;
    clip:rect(0px,500px,500px,0px);
}
.image1 { 
background: url(images/bg.jpg) no-repeat center center; 
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
</style>
</head>
<body>

<div class='clipper-div'>
   <img class='image1' src='office.gif'/>  
</div>
like image 722
scriver Avatar asked Sep 22 '19 17:09

scriver


People also ask

How to center center image in image editor?

Have a circular clipping of radius 40 pixels (both horizontal and vertical). If image is > 90x90, it should center within the 90x90 space and clip a 40x40 circle from the middle.

Does the clipping circle have any effect on the image?

The clipping circle should have no effect since the entire image is contained within the clip area. I have my XAML code below. This works as expected for pictures that are exactly 90x90 (i.e. they don't stretch, they center the image and the clipping works).

How do I Center a 90x90 image on a page?

Have a circular clipping of radius 40 pixels (both horizontal and vertical). If image is > 90x90, it should center within the 90x90 space and clip a 40x40 circle from the middle. If image is < 90x90, it should center within the 90x90 space. The clipping circle should have no effect since the entire image is contained within the clip area.

Why is the image on the top-left portion of the image?

For images < 90x90, the image gets centered but the clipping seems to place the image in the top-left area of the Image content so, the clipping clips the top-left portion of the image.


1 Answers

The kind you were looking for is an inset clipping:

clip-path: inset(top bottom left right);

You can listen to the mouse move event to update the clipping. In the example below, I used CSS custom properties I added to the clipper-element style definition.

These custom properties are used as CSS variables for the clipping definition.

// Globals variables (we could store them into an object,
// which would be a cleaner way
var clipperDiv = document.getElementById("clipper-div");
var hoveringClippedImg = document.getElementById("hovering-clipped");
var imgBoundingRect = hoveringClippedImg.getBoundingClientRect();
var clippingSize = 40;

// Surrouding DIV element mouse move event callback
clipperDiv.onmousemove = clipHoveredArea;

// Update image clipping

function clipHoveredArea(e) {

  // First step: getting clipping coordinates from mouse position
  var pos = getMousePos(e);
  var top = (pos.y - clippingSize / 2);
  var bottom = (imgBoundingRect.height - pos.y - (clippingSize / 2));
  var left = (pos.x - clippingSize / 2);
  var right = (imgBoundingRect.width - pos.x - clippingSize / 2);

  // Second step: CSS custom properties
  hoveringClippedImg.style.setProperty("--top", top + "px");
  hoveringClippedImg.style.setProperty("--bottom", bottom + "px");
  hoveringClippedImg.style.setProperty("--left", left + "px");
  hoveringClippedImg.style.setProperty("--right", right + "px");

};

// Get mouse position relative to an element
// Source: //stackoverflow.com/a/42111623/4375327

function getMousePos(e) {
  // e = Mouse click event.
  var rect = e.currentTarget.getBoundingClientRect();
  var x = e.clientX - Math.round(rect.left);
  var y = e.clientY - Math.round(rect.top);
  return {
    x: x,
    y: y
  };
}
#clipper-div {
  border: 2px solid black;
  width: 200px;
  height: 200px;
}

#hovering-clipped {
  padding: 0;
  margin: 0;
  width: 200px;
  height: 200px;
  clip-path: inset(var(--top) var(--right) var(--bottom) var(--left));
  --top: 0px;
  --right: 0px;
  --bottom: 0px;
  --left: 0px;
  cursor: crosshair;
}
<div id='clipper-div'>
  <img id="hovering-clipped"
       src="//placehold.it/200x200/d0d8f8/000000" />
</div>

Note: I used Clippy. It's a handy tool to design the clipping you want.

like image 83
Amessihel Avatar answered Oct 12 '22 08:10

Amessihel