块编辑器开发文档

FocalPointPicker

💡 云策文档标注

概述

FocalPointPicker 是一个 WordPress 组件,用于创建 UI 以识别图像的关键视觉焦点,解决大背景图像在不同视口下裁剪不当的问题。它返回一个 0 到 1 之间的坐标值,可轻松转换为 CSS background-position 属性,确保焦点始终可见。

关键要点

  • 组件用于选择图像焦点,返回 { x: 0.5, y: 0.1 } 格式的坐标值
  • 坐标值可转换为 CSS background-position 属性,如 50% 10%
  • 适用于 React 环境,需从 @wordpress/components 导入
  • 支持图像和视频 URL,可配置自动播放等属性
  • 提供 onChange、onDrag 等回调函数处理交互事件

代码示例

import { useState } from 'react';
import { FocalPointPicker } from '@wordpress/components';

const Example = () => {
    const [ focalPoint, setFocalPoint ] = useState( {
        x: 0.5,
        y: 0.5,
    } );

    const url = '/path/to/image';

    const style = {
        backgroundImage: `url(${ url })`,
        backgroundPosition: `${ focalPoint.x * 100 }% ${ focalPoint.y * 100 }%`,
    };

    return (
        <>
            <FocalPointPicker
                url={ url }
                value={ focalPoint }
                onDragStart={ setFocalPoint }
                onDrag={ setFocalPoint }
                onChange={ setFocalPoint }
            />
            <div style={ style } />
        </>
    );
};

注意事项

  • url 属性为必填项,指定图像或视频的 URL
  • value 属性必须为包含 x 和 y 参数的对象
  • onChange 回调函数在焦点变化时调用,为必填项
  • onDrag、onDragStart、onDragEnd 为可选回调,用于处理拖拽事件
  • resolvePoint 函数可在内部状态更新前修改值

📄 原文内容

Focal Point Picker is a component which creates a UI for identifying the most important visual point of an image. This component addresses a specific problem: with large background images it is common to see undesirable crops, especially when viewing on smaller viewports such as mobile phones. This component allows the selection of the point with the most important visual information and returns it as a pair of numbers between 0 and 1. This value can be easily converted into the CSS background-position attribute, and will ensure that the focal point is never cropped out, regardless of viewport.

  • Example focal point picker value: { x: 0.5, y: 0.1 }
  • Corresponding CSS: background-position: 50% 10%;

Usage

import { useState } from 'react';
import { FocalPointPicker } from '@wordpress/components';

const Example = () => {
    const [ focalPoint, setFocalPoint ] = useState( {
        x: 0.5,
        y: 0.5,
    } );

    const url = '/path/to/image';

    /* Example function to render the CSS styles based on Focal Point Picker value */
    const style = {
        backgroundImage: `url(${ url })`,
        backgroundPosition: `${ focalPoint.x * 100 }% ${ focalPoint.y * 100 }%`,
    };

    return (
        <>
            <FocalPointPicker
                url={ url }
                value={ focalPoint }
                onDragStart={ setFocalPoint }
                onDrag={ setFocalPoint }
                onChange={ setFocalPoint }
            />
            <div style={ style } />
        </>
    );
};

Props

url

  • Type: Text
  • Required: Yes

URL of the image or video to be displayed

autoPlay

  • Type: Boolean
  • Required: No
  • Default: true

Autoplays HTML5 video. This only applies to video sources (url).

value

  • Type: Object
  • Required: Yes

The focal point. Should be an object containing x and y params.

onChange

  • Type: Function
  • Required: Yes

Callback which is called when the focal point changes.

onDrag

  • Type: Function
  • Required: No

Callback which is called repetitively during drag operations.

onDragEnd

  • Type: Function
  • Required: No

Callback which is called at the end of drag operations.

onDragStart

  • Type: Function
  • Required: No

Callback which is called at the start of drag operations.

resolvePoint

  • Type: Function
  • Required: No

Function which is called before internal updates to the value state. It receives the upcoming value and may return a modified one.