简体   繁体   English

计算Javascript中背景图片片段的边界

[英]Calculate the bounds of a segment of a background-image in Javascript

Is there a way to calculate the rectangle bounds of a segment of a background-image (eg a red circle on the white background of the image) which is scaled by background-size: cover, even if it has a negativ value and is out of the mobile device viewport? 有没有一种方法可以计算背景图像(例如,图像的白色背景上的红色圆圈)的一部分的矩形范围,该矩形范围由background-size缩放:覆盖,即使它具有负值并且超出移动设备视口的角度? Another solution would be to pin ax/y coordinate on the background picture position. 另一种解决方案是将ax / y坐标固定在背景图片位置上。

界线

CSS: CSS:

  .background{
     position: absolute;
     width: 100%;
     height:100%;
     background: url('img.png') no-repeat;
     background-size: cover;
     background-position: 50% 0;

} }

Here is the pen i made. 这是我做的笔。 You have to scale the browser to see the behaviour of the image- 您必须缩放浏览器才能查看图像的行为-

http://codepen.io/kmpkt/pen/MbzxvM http://codepen.io/kmpkt/pen/MbzxvM

Does anyone have an idea to solve this problem? 有谁有解决这个问题的想法?

CSS background-size: cover CSS background-size: cover

A background-size value of cover; cover; background-sizecover; applied to an element causes background images to be scaled, keeping their aspect ratio, so that the scaled image matches either the width or height of the element and is not smaller than it. 应用于元素会导致背景图像缩放,并保持其长宽比,从而使缩放后的图像与元素的宽度或高度匹配,并且不小于元素的宽度或高度。

The scaling value required can be found by comparing aspect ratios of the element and background image, using a width/height quotient in the comparison. 可以通过在比较中使用width/height商,通过比较元素和背景图像的纵横比来找到所需的缩放值。

If the element's aspect ratio is higher than the image's, the image is widened or narrowed to fit using a scaling value of: 如果元素的宽高比高于图像的宽高比,则使用以下缩放值将图像放大或缩小以适合:

    scale = element.width/image.width; // the element is more "landscape" than the image

If not the image is heightened or shortened to fit using a scaling value of: 如果不是,则使用以下缩放值来放大或缩小图像以适合:

   scale = element.height/image.height; // the element is more "portrait" then the image.

Note both width and height of the background image are scaled using the same factor. 请注意,背景图像的宽度和高度都使用相同的因子缩放。

Dimensions and offsets for a region of interest (ROI) within the background image must be scaled by the same scaling factor determined above. 背景图像内感兴趣区域(ROI)的尺寸和偏移量必须按上面确定的相同比例因子进行缩放。

CSS background-position: 50% 0; CSS background-position: 50% 0;

I understand this makes no change to the y position of the background, but centers the background along the x-axis if it needs to be clipped on either side. 我知道这不会改变背景的y位置,但是如果需要在任一侧修剪背景,都可以使背景沿x轴居中。 Without x-axis clipping a scaled background image is centered by definition. 如果没有x轴剪切,则缩放后的背景图像将按定义居中。

If the scaled image width is greater than the element's, the background needs to be moved left by half the difference: 如果缩放的图像宽度大于元素的宽度,则需要将背景向左移动差值的一半:

xTranslation = (elementWidth - scaledImageWidth) / 2  // scaledImageWidth > elementWidth,

If not, no translation is required: 如果不是,则不需要翻译:

xTranslation = 0 // scaled image width == element width

A previously scaled x-axis offset for a region of interest within the background image needs to have the x translation determined above applied to it. 背景图像内感兴趣区域的先前缩放的x轴偏移量需要对其应用上面确定的x平移。

Finding the size of the backgrounded element. 查找背景元素的大小。

element.getClientBorderRect()

returns an object containing width and height values for the element. 返回一个包含元素的宽度和高度值的对象。

Avoid using document.clientWidth and document.clientHeight to obtain the dimensions of the viewport. 避免使用document.clientWidthdocument.clientHeight获取视口的尺寸。 clientHeight is returned with a maximum value of the the height of the body element. 返回clientHeight ,其值为body元素的高度的最大值。 This can be less than the height of the viewport. 这可以小于视口的高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM