-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathautoLayout.ts
More file actions
69 lines (63 loc) · 1.98 KB
/
Copy pathautoLayout.ts
File metadata and controls
69 lines (63 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import type { CanvasPreset, PhotoConfig, BorderConfig } from '../types';
export function pickPresetForImage(w: number, h: number): CanvasPreset {
const ratio = w / h;
if (ratio > 1.4) return 'landscape';
if (ratio < 0.85) return 'portrait';
return 'square';
}
export function defaultBorder(): BorderConfig {
return {
type: 'solid',
color: '#ffffff',
color2: '#000000',
gradientAngle: 45,
blurAmount: 40,
};
}
let idCounter = 0;
export function nextId(): string {
idCounter += 1;
return `p${Date.now().toString(36)}_${idCounter}`;
}
/** Whether this image should be split into left/right carousel halves. */
export function canSplitForCarousel(photo: PhotoConfig): boolean {
const imgRatio = photo.naturalW / photo.naturalH;
// Landscape image in portrait/square canvas → split left/right (horizontal split of the image into 2 portrait halves)
return imgRatio > 1.4 && photo.preset !== 'landscape';
}
export function makeSplitPair(photo: PhotoConfig): [PhotoConfig, PhotoConfig] | null {
if (!canSplitForCarousel(photo)) return null;
const base: Omit<PhotoConfig, 'id' | 'splitOf'> = {
fileName: photo.fileName,
bitmap: photo.bitmap,
naturalW: photo.naturalW,
naturalH: photo.naturalH,
preset: photo.preset,
border: { ...photo.border },
offsetX: 0,
offsetY: 0,
scale: 1,
crop: { x: 0, y: 0, w: 1, h: 1 },
};
return [
{ ...base, id: nextId(), splitOf: { sourceId: photo.id, half: 'left' } },
{ ...base, id: nextId(), splitOf: { sourceId: photo.id, half: 'right' } },
];
}
export async function initialPhotoConfig(file: File): Promise<PhotoConfig> {
const bitmap = await createImageBitmap(file);
return {
id: nextId(),
fileName: file.name,
bitmap,
naturalW: bitmap.width,
naturalH: bitmap.height,
preset: pickPresetForImage(bitmap.width, bitmap.height),
border: defaultBorder(),
offsetX: 0,
offsetY: 0,
scale: 1,
crop: { x: 0, y: 0, w: 1, h: 1 },
splitOf: undefined,
};
}