Skip to content

Commit 692ff39

Browse files
authored
Merge pull request #81 from Glazzes/dev
Pre 4.0.1
2 parents 386be43 + 7c8e578 commit 692ff39

File tree

4 files changed

+51
-61
lines changed

4 files changed

+51
-61
lines changed

docs/docs/guides/skia.md

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ basic structure with Skia first.
4949
import React from 'react';
5050
import { View, useWindowDimensions } from 'react-native';
5151
import { Canvas, Image, useImage } from '@shopify/react-native-skia';
52-
import { getAspectRatioSize } from 'react-native-zoom-toolkit';
52+
import { fitContainer } from 'react-native-zoom-toolkit';
5353
5454
const uri =
5555
'https://assets-global.website-files.com/63634f4a7b868a399577cf37/64665685a870fadf4bb171c2_labrador%20americano.jpg';
@@ -62,12 +62,8 @@ const App = () => {
6262
return null;
6363
}
6464
65-
const resolution = { width: image.width(), height: image.height() };
66-
// This is the size the image will take on the screen, not its resolution.
67-
const imageSize = getAspectRatioSize({
68-
aspectRatio: resolution.width / resolution.height,
69-
width: width,
70-
});
65+
const aspectRatio = image.width() / image.height();
66+
const imageSize = fitContainer(aspectRatio, { width, height });
7167
7268
const x = 0;
7369
const y = (height - imageSize.height) / 2;
@@ -108,11 +104,11 @@ With our basic structure in place, let's address the two missing steps to make t
108104

109105
Let's see how the end result looks like:
110106

111-
```tsx{12,39,43-53}
107+
```tsx{12,40-48}
112108
import React from 'react';
113109
import { View, useWindowDimensions } from 'react-native';
114110
import { Canvas, Image, useImage } from '@shopify/react-native-skia';
115-
import { getAspectRatioSize, useTransformationState } from 'react-native-zoom-toolkit';
111+
import { fitContainer, ResumableZoom, useTransformationState } from 'react-native-zoom-toolkit';
116112
117113
const uri =
118114
'https://assets-global.website-files.com/63634f4a7b868a399577cf37/64665685a870fadf4bb171c2_labrador%20americano.jpg';
@@ -126,11 +122,8 @@ const App = () => {
126122
return null;
127123
}
128124
129-
const resolution = { width: image.width(), height: image.height() };
130-
const imageSize = getAspectRatioSize({
131-
aspectRatio: resolution.width / resolution.height,
132-
width: width,
133-
});
125+
const aspectRatio = image.width() / image.height();
126+
const imageSize = fitContainer(aspectRatio, { width, height });
134127
135128
const x = 0;
136129
const y = (height - imageSize.height) / 2;
@@ -151,17 +144,15 @@ const App = () => {
151144
/>
152145
</Canvas>
153146
154-
{/* Same size and position (0, 0) as the canvas above */}
155-
<View style={{ width, height, position: 'absolute' }}>
156-
<ResumableZoom
157-
maxScale={resolution}
158-
extendGestures={true}
159-
onUpdate={onUpdate}
160-
>
161-
{/* Nested child is the same size as the Skia image */}
162-
<View style={{ width: imageSize.width, height: imageSize.height }} />
163-
</ResumableZoom>
164-
</View>
147+
<ResumableZoom
148+
style={{ width, height, position: 'absolute' }}
149+
maxScale={resolution}
150+
extendGestures={true}
151+
onUpdate={onUpdate}
152+
>
153+
{/* Nested child is the same size as the Skia image */}
154+
<View style={{ width: imageSize.width, height: imageSize.height }} />
155+
</ResumableZoom>
165156
</View>
166157
);
167158
};

docs/docs/installation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ This library relies on both Reanimated and Gesture Handler making part of your p
99

1010
| React Native Version | React Native Zoom Toolkit Version | Gesture Handler version |
1111
| -------------------- | --------------------------------- | ----------------------- |
12-
| `<= 0.76` | `3.x.x` | 2.16.0 and beyond. |
13-
| `>= 0.76` | `4.x.x` | 2.19.0 and beyond. |
12+
| `<= 0.76` | `>= 3.0.0` | 2.16.0 and beyond. |
13+
| `>= 0.76` | `>= 4.0.0` | 2.19.0 and beyond. |
1414

1515
::: code-group
1616

docs/docs/utilities/usetransformationstate.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ your choice, for a hands on example see [How to use with Skia Components](../gui
1010

1111
## How to use
1212

13-
When calling this hook it will provide you with the three following properties:
13+
When calling this hook you will receive the three following properties:
1414

1515
- `onUpdate` is a worklet function which must be passed as a property to the zoom component's onUpdate
1616
callback property, this way the zoom component will update transform and state properties.
17-
- `transform` is a shared value containing the transformation array of the zoom component.
18-
- `state` is an object holding the shared values describing the current transformation state in case
17+
- `transform` is a shared value describing zoom component's current transformations as an array.
18+
- `state` is an object holding the shared values describing the current transformation state, in case
1919
you need them.
2020

2121
```tsx{9,21}

src/components/resumable/ResumableZoom.tsx

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { clamp } from '../../commons/utils/clamp';
1212
import { useVector } from '../../commons/hooks/useVector';
1313
import { getMaxScale } from '../../commons/utils/getMaxScale';
1414
import { useSizeVector } from '../../commons/hooks/useSizeVector';
15-
import { pinchTransform } from '../../commons/utils/pinchTransform';
1615
import { usePanCommons } from '../../commons/hooks/usePanCommons';
1716
import { usePinchCommons } from '../../commons/hooks/usePinchCommons';
1817
import { useDoubleTapCommons } from '../../commons/hooks/useDoubleTapCommons';
@@ -250,36 +249,6 @@ const ResumableZoom: React.FC<ResumableZoomPropsWithRef> = (props) => {
250249
set(toX, toY, toScale, animate);
251250
};
252251

253-
const zoom = (multiplier: number, xy?: Vector<number>) => {
254-
let originX = -1 * (translate.x.value / scale.value);
255-
let originY = -1 * (translate.y.value / scale.value);
256-
const toScale = clamp(scale.value * multiplier, minScale, maxScale.value);
257-
258-
if (xy !== undefined) {
259-
const diffX = xy.x / childSize.width.value;
260-
const diffY = xy.y / childSize.height.value;
261-
const actualX = extendGestures ? diffX * extendedSize.width.value : xy.x;
262-
const actualY = extendGestures ? diffY * extendedSize.height.value : xy.y;
263-
264-
originX = actualX - extendedSize.width.value / 2;
265-
originY = actualY - extendedSize.height.value / 2;
266-
}
267-
268-
const { x, y } = pinchTransform({
269-
toScale,
270-
fromScale: scale.value,
271-
origin: { x: originX, y: originY },
272-
offset: { x: translate.x.value, y: translate.y.value },
273-
delta: { x: 0, y: 0 },
274-
});
275-
276-
const { x: boundX, y: boundY } = boundsFn(toScale);
277-
const toX = clamp(x, -1 * boundX, boundX);
278-
const toY = clamp(y, -1 * boundY, boundY);
279-
280-
set(toX, toY, toScale, true);
281-
};
282-
283252
const getVisibleRect = (): Rect => {
284253
return getRect({
285254
scale: scale.value,
@@ -295,6 +264,36 @@ const ResumableZoom: React.FC<ResumableZoomPropsWithRef> = (props) => {
295264
});
296265
};
297266

267+
const zoom = (multiplier: number, xy?: Vector<number>) => {
268+
const toScale = scale.value * multiplier;
269+
270+
let focal = xy;
271+
if (focal === undefined) {
272+
const frame = getVisibleRect();
273+
focal = {
274+
x: frame.x + frame.width / 2,
275+
y: frame.y + frame.height / 2,
276+
};
277+
}
278+
279+
const centerX = childSize.width.value / 2;
280+
const centerY = childSize.height.value / 2;
281+
282+
const originX = focal.x - centerX;
283+
const originY = focal.y - centerY;
284+
const signedDistanceCenterX = centerX - focal.x;
285+
const signedDistanceCenterY = centerY - focal.y;
286+
287+
const translateX = signedDistanceCenterX + (originX - originX * toScale);
288+
const translateY = signedDistanceCenterY + (originY - originY * toScale);
289+
290+
const { x: boundX, y: boundY } = boundsFn(toScale);
291+
const toX = clamp(translateX, -1 * boundX, boundX);
292+
const toY = clamp(translateY, -1 * boundY, boundY);
293+
294+
set(toX, toY, toScale, true);
295+
};
296+
298297
useImperativeHandle(reference, () => ({
299298
reset: (animate = true) => set(0, 0, minScale, animate),
300299
requestState: requestState,

0 commit comments

Comments
 (0)