UNPKG

1.97 kBJavaScriptView Raw
1/*
2 * Copyright (c) 2019 American Express Travel Related Services Company, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14
15const getMaxImageSize = (images) => {
16 let maxWidth = 0;
17 let maxHeight = 0;
18
19 images.forEach((image) => {
20 if (image.imageWidth > maxWidth) {
21 maxWidth = image.imageWidth;
22 }
23
24 if (image.imageHeight > maxHeight) {
25 maxHeight = image.imageHeight;
26 }
27 });
28
29 return {
30 maxWidth,
31 maxHeight,
32 };
33};
34
35const ImageComposer = function ImageComposer(options = {}) {
36 this.direction = options.direction || 'horizontal';
37 this.images = [];
38
39 return this;
40};
41
42ImageComposer.prototype.addImage = function addImage(imageData, imageWidth, imageHeight) {
43 this.images.push({
44 imageData,
45 imageWidth,
46 imageHeight,
47 });
48
49 return this;
50};
51
52ImageComposer.prototype.getParams = function getParams() {
53 const { maxWidth, maxHeight } = getMaxImageSize(this.images);
54
55 const compositeWidth = maxWidth * (this.direction === 'horizontal' ? this.images.length : 1);
56 const compositeHeight = maxHeight * (this.direction === 'vertical' ? this.images.length : 1);
57 const offsetX = this.direction === 'horizontal' ? maxWidth : 0;
58 const offsetY = this.direction === 'vertical' ? maxHeight : 0;
59
60 return {
61 direction: this.direction,
62 images: this.images,
63 imagesCount: this.images.length,
64 compositeWidth,
65 compositeHeight,
66 offsetX,
67 offsetY,
68 };
69};
70
71module.exports = ImageComposer;