<a id="image-focal-points"></a>

# 焦点

焦点用于告知 Wagtail 图片中包含主体的区域。
`fill` 过滤器会用它把裁剪聚焦在主体上，避免裁到主体本身。

焦点既可以由 Wagtail 用户手动定义，也可以通过人脸或特征检测自动定义。

<a id="rendition-background-position-style"></a>

## 基于焦点设置 `background-position` 行内样式

当你用 Wagtail 图片作为某个元素的背景时，可以在 Rendition 上使用 `.background_position_style`
属性，依据图片中的焦点来定位该 Rendition：

```html+django
{% image page.image width-1024 as image %}

<div style="background-image: url('{{ image.url }}'); {{ image.background_position_style }}">
</div>
```

对于强制实施内容安全策略（CSP）的站点，你可以通过带 `nonce` 属性的 `<style>` 标签来应用这些样式。

## 在模板中访问焦点

你可以通过访问 Rendition 的 `.focal_point` 属性，在模板中取得焦点：

```html+django
{% load wagtailimages_tags %}

{% image page.image width-800 as myrendition %}

<img
    src="{{ myrendition.url }}"
    alt="{{ myimage.title }}"
    {% if myrendition.focal_point %}
        data-focus-x="{{ myrendition.focal_point.centroid.x }}"
        data-focus-y="{{ myrendition.focal_point.centroid.y }}"
        data-focus-width="{{ myrendition.focal_point.width }}"
        data-focus-height="{{ myrendition.focal_point.height }}"
    {% endif %}
/>
```
