@11tyrocks/eleventy-plugin-objectfit-focalpoint 中文文档教程

发布于 3年前 浏览 24 项目主页 更新于 3年前

Eleventy Plugin: Object-Fit Focal Point

一个 Eleventy Nunjucks 短代码,用于提供生成图像的 object-position 值的功能,以保持焦点在视野中。 使用实用程序测试结果

此短代码与 CSS 属性 object-fit 结合使用,这使得 img 充当它自己的容器。 当分配了 cover 的值时,图像的行为类似于 background-size: cover

不熟悉object-fit 查看我的 2 分钟免费书呆子视频>

短代码使用 sharp package resize API 用于使用 香农熵 确定图像的焦点。 然后,它根据图像的纵横比将计算出的点作为百分比应用为 object-position 的值。 调整图像容器大小时,焦点不太可能*被裁剪出视野

为获得最佳效果,纵横比应与自然图像方向相似。 例如,对于 1024x768 的图像,5/3600x1200 的图像有更好的结果。

* 熵是不完美的,您可能无法在每张图像上都达到预期的效果,尤其是在强光/暗区

Usage

安装插件:

npm install @11tyrocks/eleventy-plugin-objectfit-focalpoint

然后,将其包含在您的 .eleventy.js 配置文件中:

const objectFitFocalPoint = require("@11tyrocks/eleventy-plugin-objectfit-focalpoint");

module.exports = (eleventyConfig) => {
  eleventyConfig.addPlugin(objectFitFocalPoint);
};

Required Image Styles

为了使短代码完全正常工作,您需要为相关图像包含以下样式。 默认类是 image,可以通过在插件配置中将新字符串传递给 imageClasses 来更改。

.image {
  /* Required */
  object-fit: cover;

  /* Recommended but not required */
  display: block;
  max-width: 100%;

  /* Optional: Force images to fill their parent container's width */
  width: 100%;
}

Using the Shortcode

因为短代码是异步的,所以它只适用于 Nunjucks。 如果您通常使用 Markdown 编写,则可以将以下内容添加到您的 frontmatter 中以便能够同时使用两者:

templateEngineOverride: njk, md

要使用短代码,请传入图像路径以及可选的 widthheight 值,或方面 ratio

// Local file - must start with `/`
{% objectFitFocalPoint image="/img/my-image.png", ratio="4/3" %}

// External file - must begin with http or https
// ⚠️ Note that the extra processing may slow down your build
{% objectFitFocalPoint image="https://source.unsplash.com/0kCrlrs8gXg/700x900", width="400", height="300" %}

注意:建议始终传递宽度和高度,因为浏览器现在会在图像创建空间时创建空间根据从这些值创建的预期宽高比加载。 这有助于减少页面内容的跳跃,并提高您的Cumulative Layout Shift Core Web Vitals 分数。

Config Options

OptionTypeDefault
defaultAspectRatiostring'5/3'
defaultWidthint800
defaultHeightint480
imageClassesstring'image'
siteInputPathstring'.'

如果您在 Eleventy 配置中自定义了输入目录,则至少可能需要更新 siteInputPath。 此值不应/ 结尾。

这是一个示例,如果您的输入目录是 src

eleventyConfig.addPlugin(objectFitFocalPoint, {
  siteInputPath: "./src",
});

New to Eleventy?

查看我在 11ty.Rocks 的其他资源

Eleventy Plugin: Object-Fit Focal Point

An Eleventy Nunjucks shortcode to provide the functionality of generating an image's object-position value in order to keep the focal point in view. Test drive the results by using the utilty app.

This shortcode works in combination with the CSS property object-fit which makes an img act as it's own container. When assigned the value of cover, the image behaves similar to background-size: cover.

Unfamilar with object-fit? Check out my 2 minute free egghead video >

The shortcode uses the sharp package resize API to determine the focal point of an image with Shannon entropy. It then applies the calculated point as a percentage based on the image's aspect ratio as the value of object-position. When your image container is resized, the focal point is less likely* to be cropped out of view.

For best results, an aspect-ratio should be similar to the natural image orientation. For example, 5/3 for an image naturally 1024x768 will have better results than for an image 600x1200.

* Entropy is imperfect and you may not achieve the desired results with every image, particularly with strong light/dark areas.

Usage

Install the plugin:

npm install @11tyrocks/eleventy-plugin-objectfit-focalpoint

Then, include it in your .eleventy.js config file:

const objectFitFocalPoint = require("@11tyrocks/eleventy-plugin-objectfit-focalpoint");

module.exports = (eleventyConfig) => {
  eleventyConfig.addPlugin(objectFitFocalPoint);
};

Required Image Styles

For the shortcode to fully work, you will need to include the following styles for the related images. The default class is image which can be changed by passing a new string to imageClasses within the plugin config.

.image {
  /* Required */
  object-fit: cover;

  /* Recommended but not required */
  display: block;
  max-width: 100%;

  /* Optional: Force images to fill their parent container's width */
  width: 100%;
}

Using the Shortcode

Because the shortcode is async, it is only available for Nunjucks. If you typically write in Markdown, you can add the following to your frontmatter to be able to use both:

templateEngineOverride: njk, md

To use the shortcode, pass in an image path and optionally width and height values, or an aspect ratio.

// Local file - must start with `/`
{% objectFitFocalPoint image="/img/my-image.png", ratio="4/3" %}

// External file - must begin with http or https
// ⚠️ Note that the extra processing may slow down your build
{% objectFitFocalPoint image="https://source.unsplash.com/0kCrlrs8gXg/700x900", width="400", height="300" %}

Note: It's recommended to always pass in width and height since browsers now create space while the image loads based on the expected aspect-ratio created from those values. This helps alleviate jumping of page content, and improves your Cumulative Layout Shift Core Web Vitals score.

Config Options

OptionTypeDefault
defaultAspectRatiostring'5/3'
defaultWidthint800
defaultHeightint480
imageClassesstring'image'
siteInputPathstring'.'

At minimum, you may need to update the siteInputPath if you have customized your input directory within your Eleventy config. This value should not end with /.

Here's an example if your input directory is src:

eleventyConfig.addPlugin(objectFitFocalPoint, {
  siteInputPath: "./src",
});

New to Eleventy?

Check out my additional resources at 11ty.Rocks

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文