focal_points.rst 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. Focal points
  2. ============
  3. Focal points are used to indicate to Wagtail the area of an image that contains the subject.
  4. This is used by the ``fill`` filter to focus the cropping on the subject, and avoid cropping into it.
  5. Focal points can be defined manually by a Wagtail user, or automatically by using face or feature detection.
  6. .. _rendition_background_position_style:
  7. Setting the ``background-position`` inline style based on the focal point
  8. -------------------------------------------------------------------------
  9. When using a Wagtail image as the background of an element, you can use the ``.background_position_style``
  10. attribute on the rendition to position the rendition based on the focal point in the image:
  11. .. code-block:: html+Django
  12. {% image page.image width-1024 as image %}
  13. <div style="background-image: url('{{ image.url }}'); {{ image.background_position_style }}">
  14. </div>
  15. Accessing the focal point in templates
  16. --------------------------------------
  17. .. versionadded:: 2.14
  18. You can access the focal point in the template by accessing the ``.focal_point`` attribute of a rendition:
  19. .. code-block:: html+Django
  20. {% load wagtailimages %}
  21. {% image myimage width-800 as myrendition %}
  22. <img
  23. src="{{ myrendition.url }}"
  24. alt="{{ myimage.title }}"
  25. {% if myrendition.focal_point %}
  26. data-focus-x="{{ myrendition.focal_point.centroid.x }}"
  27. data-focus-y="{{ myrendition.focal_point.centroid.y }}"
  28. data-focus-width="{{ myrendition.focal_point.width }}"
  29. data-focus-height="{{ myrendition.focal_point.height }}"
  30. {% endif %}
  31. />