-
Notifications
You must be signed in to change notification settings - Fork 3
/
best-fit.js
77 lines (68 loc) · 1.41 KB
/
best-fit.js
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
70
71
72
73
74
75
76
77
'use strict';
// make the picture fit within the bounds as large as possible
//
// EXAMPLES
//
// Original 1: 900x500
// Original 2: 700x700
// Original 3: 500x900
//
// Aspect: 800x600
// N1: 800x?
// N2: ?x600
//
// Aspect: 800x?
// N1: 800x?
// N2: 700x?
//
// Aspect: ?x600
// N1: ?x500
// N2: ?x600
function bestMinFit(size, width, height) {
var newReal
, rw
, rh
;
rw = Math.min(1, (width || size.width) / size.width);
rh = Math.min(1, (height || size.height) / size.height);
if (rw < rh) {
//console.log(rw);
newReal = { w: size.width * rw, h: size.height * rw };
} else if (rh < rw) {
//console.log(rh);
newReal = { w: size.width * rh, h: size.height * rh };
} else {
//console.log(1);
newReal = { w: size.width, h: size.height };
}
return newReal;
}
/*
function bestMinFit2(size, width, height) {
var new1 = { w: 0, h: 0}
, new2 = { w: 0, h: 0}
, ratio = size.width / size.height
, newReal
;
if (width && (width < size.width)) {
new1.w = width;
new1.h = width * (1/ratio);
} else {
new1.w = size.width;
new1.h = size.height;
}
if (height && (height < size.height)) {
new2.w = height * (ratio);
new2.h = height;
} else {
new2.w = size.width;
new2.h = size.height;
}
if (new1.w < new2.w || new1.h < new2.h) {
newReal = new1;
} else {
newReal = new2;
}
}
*/
module.exports.fit = bestMinFit;