Skip to content

Commit

Permalink
new version
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Herd committed Mar 23, 2024
1 parent 3e1e9b1 commit f92ab35
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 26 deletions.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<script type="module" crossorigin src="/diorama-2023/assets/index-0cd0196d.js"></script>
<script type="module" crossorigin src="/diorama-2023/assets/index-6703b6af.js"></script>
<link rel="stylesheet" href="/diorama-2023/assets/index-5d0ad71d.css">
</head>
<body>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "diorama",
"version": "2.0.15",
"version": "2.0.16",
"keywords": [
"gallery",
"webworker",
Expand Down
6 changes: 2 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { range } from 'ramda'

export const images = range(1, 18).map(n => `./images/${n}.jpg`)
export const maxComputationTime = 400 as Milliseconds
export const sizeHomogeneity = .7
export const lowSizeHomogeneity = 3
// the higher the value, the more pictures will approach the same size
export const randomizeThreshold = 8 // if amount of pics is bigger we have too many permutations to iterate through, switch to random strategy
export const sizeHomogeneity = .2
export const randomizeThreshold = 11 // if amount of pics is bigger we have too many permutations to iterate through, switch to random strategy
export const iconSize = 20
export const initialImageAmount = 8
17 changes: 9 additions & 8 deletions src/layout/evaluate-solutions.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import '../types/ramda'

import type { PositionedPicture, Config, NonEmptyArray, Solution } from '../types/types'
import type { PositionedPicture, NonEmptyArray, Solution } from '../types/types'
import { evolve, last, prop, sortBy } from 'ramda'
import type { Ord } from 'ramda'
import { sizeHomogeneity } from '../constants'

const aspectRatioAndSize = (config: Config) =>
const aspectRatioAndSize =
(solution: Solution): Ord => {
const sizeHomogeneity = solution.pictures.length < config.randomizeThreshold
? config.lowSizeHomogeneity
: config.sizeHomogeneity
return (solution.score + solution.sizeHomogeneity * sizeHomogeneity) / (sizeHomogeneity + 1)
const sh = solution.pictures.length < 4
? .1
: sizeHomogeneity + solution.pictures.length * 0.1
return (solution.score + solution.sizeHomogeneity * sh) / (sh + 1)
}

export const evaluateSolutions = (results: NonEmptyArray<Solution>, config: Config): Solution => {
const rated = sortBy(aspectRatioAndSize(config), results)
export const evaluateSolutions = (results: NonEmptyArray<Solution>): Solution => {
const rated = sortBy(aspectRatioAndSize, results)
const winner = last(rated)
return evolve({ pictures: sortBy<PositionedPicture>(prop('url')) }, winner)
}
8 changes: 3 additions & 5 deletions src/layout/find-solution.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { lowSizeHomogeneity, maxComputationTime, randomizeThreshold, sizeHomogeneity } from '../constants'
import { maxComputationTime, randomizeThreshold } from '../constants'
import type { Config, Dimension, Picture, Solution } from '../types/types'
import { isNotEmpty } from '../utils/isNotEmpty'
import { evaluateSolutions } from './evaluate-solutions'
Expand All @@ -9,9 +9,7 @@ import { resizeDimension } from '../utils/resize-dimension'

const defaultConfig: Config = {
maxComputationTime,
sizeHomogeneity,
randomizeThreshold,
lowSizeHomogeneity
randomizeThreshold
}

export const findSolution = (
Expand Down Expand Up @@ -41,5 +39,5 @@ export const findSolution = (
if (!isNotEmpty(solutions)) {
throw new Error('No solution')
}
return evaluateSolutions(solutions, config)
return evaluateSolutions(solutions)
}
4 changes: 2 additions & 2 deletions src/types/ramda.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type {CondPair, Ord } from 'ramda'
import type {NonEmptyArray} from './types'

type isNil = (value: any) => value is null | undefined
type CondPair<T extends any[], R> = [(...val: T) => boolean, (...val: T) => R]

declare module 'ramda' {
export function head<T>(arr: NonEmptyArray<T>): T
Expand All @@ -15,5 +15,5 @@ declare module 'ramda' {
whenFalseFn: (a: NonNullable<T>) => U,
a: T
): T | U
export function sortBy<T>(fn: (a: T) => Ord, list: NonEmptyArray<T>): NonEmptyArray<T>
export function sortBy<T>(fn: (a: T) => number, list: NonEmptyArray<T>): NonEmptyArray<T>
}
2 changes: 0 additions & 2 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,5 @@ export type NonEmptyArray<T> = [T, ...T[]]

export type Config = {
maxComputationTime: number
sizeHomogeneity: number // the higher the value, the more pictures will approach the same size
lowSizeHomogeneity: number // the higher the value, the more pictures will approach the same size
randomizeThreshold: number
}

0 comments on commit f92ab35

Please sign in to comment.