focal_points.rst 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. You can access the focal point in the template by accessing the ``.focal_point`` attribute of a rendition:
  18. .. code-block:: html+Django
  19. {% load wagtailimages %}
  20. {% image myimage width-800 as myrendition %}
  21. <img
  22. src="{{ myrendition.url }}"
  23. alt="{{ myimage.title }}"
  24. {% if myrendition.focal_point %}
  25. data-focus-x="{{ myrendition.focal_point.centroid.x }}"
  26. data-focus-y="{{ myrendition.focal_point.centroid.y }}"
  27. data-focus-width="{{ myrendition.focal_point.width }}"
  28. data-focus-height="{{ myrendition.focal_point.height }}"
  29. {% endif %}
  30. />