1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | const 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 |
|
35 | const ImageComposer = function ImageComposer(options = {}) {
|
36 | this.direction = options.direction || 'horizontal';
|
37 | this.images = [];
|
38 |
|
39 | return this;
|
40 | };
|
41 |
|
42 | ImageComposer.prototype.addImage = function addImage(imageData, imageWidth, imageHeight) {
|
43 | this.images.push({
|
44 | imageData,
|
45 | imageWidth,
|
46 | imageHeight,
|
47 | });
|
48 |
|
49 | return this;
|
50 | };
|
51 |
|
52 | ImageComposer.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 |
|
71 | module.exports = ImageComposer;
|