-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
218 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"label": "Algorithms", | ||
"position": 7 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
title: Resize Algorithm | ||
sidebar_position: 7 | ||
--- | ||
import { ResizeAlgorithm } from '../../src/components/algorithms/ResizeAlgorithm'; | ||
|
||
# Advanced Recipes | ||
|
||
## Resize Algorithm | ||
|
||
<ResizeAlgorithm/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
example/src/components/algorithms/ResizeAlgorithm/index.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.resize-algorithm { | ||
width: 100%; | ||
height: 400px; | ||
&__boundary { | ||
position: relative; | ||
} | ||
&__stencil { | ||
position: absolute; | ||
color: var(--ifm-color-primary); | ||
&-line { | ||
border-color: var(--ifm-color-primary); | ||
} | ||
} | ||
&__reference { | ||
position: absolute; | ||
border: solid 1px var(--ifm-color-primary-dark) | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
example/src/components/algorithms/ResizeAlgorithm/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import React, { useEffect, useRef, useState } from 'react'; | ||
import { BoundingBox } from 'react-advanced-cropper'; | ||
import './index.scss'; | ||
import { coordinatesToStyle } from '@site/src/components/algorithms/utils'; | ||
import { | ||
approximateSize, | ||
Coordinates, | ||
createAspectRatio, | ||
moveToPositionRestrictions, | ||
anchoredResizeCoordinatesAlgorithm, | ||
ResizeOptions, | ||
} from 'advanced-cropper'; | ||
import { useWindowResize } from '../../../service/useWindowResize'; | ||
|
||
export const ResizeAlgorithm = () => { | ||
const exampleRef = useRef<HTMLDivElement>(null); | ||
|
||
const [aspectRatio] = useState(1); | ||
const [boundary, setBoundary] = useState({ width: 0, height: 0 }); | ||
const [coordinates, setCoordinates] = useState({ width: 100, height: 100, left: 0, top: 0 }); | ||
|
||
const [reference, setReference] = useState<Coordinates | null>(null); | ||
|
||
const coordinatesStyle = coordinatesToStyle(coordinates); | ||
const referenceStyle = coordinatesToStyle(reference); | ||
|
||
const onResize = (anchor, directions, options: ResizeOptions) => { | ||
setReference(options.reference || null); | ||
setCoordinates( | ||
anchoredResizeCoordinatesAlgorithm(coordinates, anchor, directions, options, { | ||
aspectRatio: createAspectRatio(aspectRatio), | ||
sizeRestrictions: { | ||
maxWidth: boundary.width, | ||
maxHeight: boundary.height, | ||
minWidth: 0, | ||
minHeight: 0, | ||
}, | ||
positionRestrictions: { | ||
left: 0, | ||
top: 0, | ||
right: boundary.width, | ||
bottom: boundary.height, | ||
}, | ||
}), | ||
); | ||
}; | ||
|
||
const onResizeEnd = () => { | ||
setReference(null); | ||
}; | ||
|
||
const updateBoundary = () => { | ||
if (exampleRef.current) { | ||
const updatedBoundary = { | ||
width: exampleRef.current.clientWidth, | ||
height: exampleRef.current.clientHeight, | ||
}; | ||
setBoundary(updatedBoundary); | ||
setCoordinates((coordinates) => { | ||
if (coordinates) { | ||
const updatedCoordinates = { | ||
...coordinates, | ||
...approximateSize({ | ||
width: coordinates.width, | ||
height: coordinates.height, | ||
aspectRatio, | ||
sizeRestrictions: { | ||
maxWidth: updatedBoundary.width, | ||
maxHeight: updatedBoundary.height, | ||
minHeight: 0, | ||
minWidth: 0, | ||
}, | ||
}), | ||
}; | ||
return moveToPositionRestrictions(updatedCoordinates, { | ||
left: 0, | ||
top: 0, | ||
bottom: updatedBoundary.height, | ||
right: updatedBoundary.width, | ||
}); | ||
} | ||
return coordinates; | ||
}); | ||
} | ||
}; | ||
|
||
useWindowResize(updateBoundary); | ||
|
||
useEffect(() => { | ||
if (exampleRef.current) { | ||
setCoordinates({ | ||
width: 100, | ||
height: 100, | ||
left: exampleRef.current.clientWidth / 2 - 50, | ||
top: exampleRef.current.clientHeight / 2 - 50, | ||
}); | ||
} | ||
updateBoundary(); | ||
}, []); | ||
|
||
return ( | ||
<div className={'resize-algorithm'} ref={exampleRef}> | ||
<div | ||
className={'resize-algorithm__boundary'} | ||
style={{ | ||
width: `${boundary.width}px`, | ||
height: `${boundary.height}px`, | ||
}} | ||
> | ||
<BoundingBox | ||
reference={coordinates} | ||
style={coordinatesStyle} | ||
className={'resize-algorithm__stencil'} | ||
onResize={onResize} | ||
onResizeEnd={onResizeEnd} | ||
lineClassNames={{ | ||
default: 'resize-algorithm__stencil-line', | ||
}} | ||
/> | ||
<div className={'resize-algorithm__reference'} style={referenceStyle} /> | ||
</div> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Coordinates } from 'advanced-cropper'; | ||
|
||
export function coordinatesToStyle(coordinates: Coordinates | null) { | ||
return coordinates | ||
? { | ||
width: `${coordinates.width}px`, | ||
height: `${coordinates.height}px`, | ||
left: `${coordinates.left}px`, | ||
top: `${coordinates.top}px`, | ||
} | ||
: {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.