-
Notifications
You must be signed in to change notification settings - Fork 86
/
resize.js
40 lines (33 loc) · 1.13 KB
/
resize.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
/*
* svgexport
* Copyright (c) 2015 Ali Shakiba
* Available under the MIT license
* @license
*/
module.exports = function(input, output) {
var resize = {};
if (output.width && output.height) {
var fn = (output.mode === 'pad' || output.mode === 'meet') ? Math.min
: Math.max;
resize.scale = fn(output.width / input.width, output.height / input.height);
resize.width = output.width;
resize.height = output.height;
} else if (output.width) {
resize.scale = output.width / input.width;
resize.width = output.width;
resize.height = input.height * resize.scale;
} else if (output.height) {
resize.scale = output.height / input.height;
resize.width = input.width * resize.scale;
resize.height = output.height;
} else {
resize.scale = output.scale || 1;
resize.height = input.height * output.scale;
resize.width = input.width * output.scale;
}
resize.left = (input.left || 0) * resize.scale
+ (input.width * resize.scale - resize.width) / 2;
resize.top = (input.top || 0) * resize.scale
+ (input.height * resize.scale - resize.height) / 2;
return resize;
};