Skip to content

Commit

Permalink
chore: apply review comments - refactor fallback/native types
Browse files Browse the repository at this point in the history
  • Loading branch information
nd0ut committed Feb 26, 2024
1 parent 2015a79 commit 0daf7b2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
30 changes: 16 additions & 14 deletions packages/image-shrink/src/utils/render/fallback.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { testCanvasSize } from '../canvas/testCanvasSize'
import { canvasResize } from '../canvas/canvasResize'

type TFallback = {
img: HTMLImageElement
sourceW: number
targetW: number
targetH: number
step: number
}

/**
* Goes from target to source by step, the last incomplete step is dropped.
* Always returns at least one step - target. Source step is not included.
Expand All @@ -9,12 +17,12 @@ import { canvasResize } from '../canvas/canvasResize'
* Example with step = 0.71, source = 2000, target = 400 400 (target) <- 563 <-
* 793 <- 1117 <- 1574 (dropped) <- [2000 (source)]
*/
const calcShrinkSteps = function (
sourceW: number,
targetW: number,
targetH: number,
step: number
) {
const calcShrinkSteps = function ({
sourceW,
targetW,
targetH,
step
}: Omit<TFallback, 'img'>) {
const steps: Array<[number, number]> = []
let sW: number = targetW
let sH: number = targetH
Expand Down Expand Up @@ -47,14 +55,8 @@ export const fallback = ({
targetW,
targetH,
step
}: {
img: HTMLImageElement
sourceW: number
targetW: number
targetH: number
step: number
}): Promise<HTMLCanvasElement> => {
const steps = calcShrinkSteps(sourceW, targetW, targetH, step)
}: TFallback): Promise<HTMLCanvasElement> => {
const steps = calcShrinkSteps({ sourceW, targetW, targetH, step })

return steps.reduce(
(chain, [w, h]) => {
Expand Down
17 changes: 8 additions & 9 deletions packages/image-shrink/src/utils/render/native.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { canvasResize } from '../canvas/canvasResize'

type TNative = {
img: HTMLImageElement
targetW: number
targetH: number
}

/**
* Native high-quality canvas resampling
*
* Browser support:
* https://caniuse.com/mdn-api_canvasrenderingcontext2d_imagesmoothingenabled
* Target dimensions expected to be supported by browser.
*/
export const native = ({
img,
targetW,
targetH
}: {
img: HTMLImageElement
targetW: number
targetH: number
}) => canvasResize(img, targetW, targetH)
export const native = ({ img, targetW, targetH }: TNative) =>
canvasResize(img, targetW, targetH)

0 comments on commit 0daf7b2

Please sign in to comment.