forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (61 loc) · 1.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
/**
* Internal dependencies
*/
import FocalPointPicker from '../';
export default {
title: 'Components/FocalPointPicker',
component: FocalPointPicker,
};
const Example = ( props ) => {
const [ focalPoint, setFocalPoint ] = useState( {
x: 0.5,
y: 0.5,
} );
return (
<FocalPointPicker
value={ focalPoint }
onChange={ setFocalPoint }
{ ...props }
/>
);
};
export const _default = () => {
return <Example />;
};
export const image = () => {
const url =
'https://i0.wp.com/themes.svn.wordpress.org/twentytwenty/1.3/screenshot.png?w=572&strip=al';
return <Example url={ url } />;
};
export const video = () => {
const url =
'https://interactive-examples.mdn.mozilla.net/media/examples/flower.webm';
return <Example url={ url } />;
};
export const snapping = () => {
const snapValues = {
x: [ 0, 0.33, 0.66, 1 ],
y: [ 0, 0.33, 0.66, 1 ],
};
const threshold = 0.05;
const maybeSnapFocalPoint = ( value ) => {
let x = parseFloat( value.x );
let y = parseFloat( value.y );
snapValues.x.forEach( ( snapValue ) => {
if ( snapValue - threshold < x && x < snapValue + threshold ) {
x = snapValue;
}
} );
snapValues.y.forEach( ( snapValue ) => {
if ( snapValue - threshold < y && y < snapValue + threshold ) {
y = snapValue;
}
} );
return { x, y };
};
return <Example resolvePoint={ maybeSnapFocalPoint } />;
};