Skip to content

Commit

Permalink
Add zoom calculation utilities 🚀 (#37)
Browse files Browse the repository at this point in the history
* docs: add basic documentation for zoom calculation utilities

* chore: add changeset
  • Loading branch information
willnguyen1312 authored May 6, 2023
1 parent f25238c commit fcbc00f
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/tiny-birds-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zoom-image/core": minor
---

Add zoom calculation utilities 🚀
2 changes: 2 additions & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export default defineConfig({
items: [
{ text: "createZoomImageHover", link: "/api/createZoomImageHover" },
{ text: "createZoomImageWheel", link: "/api/createZoomImageWheel" },
{ text: "makeCalculateZoom", link: "/api/makeCalculateZoom" },
{ text: "makeCalculatePercentage", link: "/api/makeCalculatePercentage" },
],
},
],
Expand Down
4 changes: 2 additions & 2 deletions docs/api/createZoomImageWheel.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cleanup()
### Type Declaration

```ts
type ZoomImageWheelProps = {
type ZoomImageWheelOptions = {
// Maximum zoom scale, default is 4
maxZoom?: number
// Zoom ratio when scrolling, default is 0.1
Expand All @@ -28,7 +28,7 @@ type StateUpdate = { enable: boolean }

function createZoomImageWheel(
container: HTMLElement,
options?: ZoomImageWheelProps,
options?: ZoomImageWheelOptions,
): {
// Remove all event listeners
cleanup: () => void
Expand Down
2 changes: 2 additions & 0 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

- [createZoomImageWheel](/api/createZoomImageWheel)
- [createZoomImageHover](/api/createZoomImageHover)
- [makeCalculateZoom](/api/makeCalculateZoom)
- [makeCalculatePercentage](/api/makeCalculatePercentage)
18 changes: 18 additions & 0 deletions docs/api/makeCalculatePercentage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# createZoomImageWheel

Calculate the current percentage based on the zoom level

### Basic Usage

```ts
const calculatePercentage = makeCalculatePercentage(maxZoom)
const currentPercentage = calculatePercentage(currentZoom)
```

### Type Declaration

```ts
function makeCalculatePercentage = (maxZoom: number) => {
return (currentZoom: number) => number
}
```
18 changes: 18 additions & 0 deletions docs/api/makeCalculateZoom.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# createZoomImageWheel

Calculate the current zoom level based on the percentage

### Basic Usage

```ts
const calculateZoom = makeCalculateZoom(percentage)
const currentZoom = calculateZoom(currentZoom)
```

### Type Declaration

```ts
function makeCalculateZoom = (maxZoom: number) => {
return (percentage: number) => number
}
```
6 changes: 3 additions & 3 deletions packages/core/src/createZoomImageWheel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { clamp, disableScroll, enableScroll, getPointersCenter, getSourceImage, makeMaybeCallFunction } from "./utils"
import type { PointerPosition } from "./utils"

export type ZoomImageWheelProps = {
export type ZoomImageWheelOptions = {
maxZoom?: number
wheelZoomRatio?: number
}
Expand All @@ -19,8 +19,8 @@ type ZoomImageWheelState = {
type Listener = (state: ZoomImageWheelState) => void
type StateUpdate = { enable: boolean }

export function createZoomImageWheel(container: HTMLElement, options: ZoomImageWheelProps = {}) {
const finalOptions: Required<ZoomImageWheelProps> = {
export function createZoomImageWheel(container: HTMLElement, options: ZoomImageWheelOptions = {}) {
const finalOptions: Required<ZoomImageWheelOptions> = {
maxZoom: options.maxZoom || 4,
wheelZoomRatio: options.wheelZoomRatio || 0.1,
}
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
// Start importing createZoomImageHover
import { createZoomImageHover } from "./createZoomImageHover"
import type { ZoomImageHoverOptions } from "./createZoomImageHover"

export { createZoomImageHover }
export type { ZoomImageHoverOptions }
// End importing createZoomImageHover

// Start importing createZoomImageWheel
import { createZoomImageWheel } from "./createZoomImageWheel"
import type { ZoomImageWheelOptions } from "./createZoomImageWheel"

export { createZoomImageWheel }
export type { ZoomImageWheelOptions }
// End importing createZoomImageWheel

// Start importing utils
import { makeCalculatePercentage, makeCalculateZoom } from "./utils"

export { makeCalculatePercentage, makeCalculateZoom }
// Stop importing utils
2 changes: 1 addition & 1 deletion packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ const scaleLinear =
export const makeCalculatePercentage = (maxZoom: number) =>
scaleLinear({ domainStart: 1, domainStop: maxZoom, rangeStart: 0, rangeStop: 100 })

export const makeCalculateCurrentZoom = (maxZoom: number) =>
export const makeCalculateZoom = (maxZoom: number) =>
scaleLinear({ domainStart: 0, domainStop: 100, rangeStart: 1, rangeStop: maxZoom })
12 changes: 6 additions & 6 deletions packages/core/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fc from "fast-check"
import { faker } from "@faker-js/faker"
import { clamp, makeCalculateCurrentZoom, makeCalculatePercentage, makeMaybeCallFunction } from "../src/utils"
import { clamp, makeCalculateZoom, makeCalculatePercentage, makeMaybeCallFunction } from "../src/utils"
import { it } from "vitest"

describe("clamp functions", () => {
Expand Down Expand Up @@ -56,11 +56,11 @@ describe("makeCalculatePercentage function", () => {
})
})

describe("makeCalculateCurrentZoom function", () => {
describe("makeCalculateZoom function", () => {
it("should work as expected", () => {
expect(makeCalculateCurrentZoom(4)(0)).toMatchInlineSnapshot("1")
expect(makeCalculateCurrentZoom(4)(33.33333333333333)).toMatchInlineSnapshot("1.9999999999999998")
expect(makeCalculateCurrentZoom(4)(66.66666666666666)).toMatchInlineSnapshot("2.9999999999999996")
expect(makeCalculateCurrentZoom(4)(100)).toMatchInlineSnapshot("4")
expect(makeCalculateZoom(4)(0)).toMatchInlineSnapshot("1")
expect(makeCalculateZoom(4)(33.33333333333333)).toMatchInlineSnapshot("1.9999999999999998")
expect(makeCalculateZoom(4)(66.66666666666666)).toMatchInlineSnapshot("2.9999999999999996")
expect(makeCalculateZoom(4)(100)).toMatchInlineSnapshot("4")
})
})

0 comments on commit fcbc00f

Please sign in to comment.