-
Notifications
You must be signed in to change notification settings - Fork 346
/
Copy pathphoto-viewer.jsx
56 lines (46 loc) · 1.73 KB
/
photo-viewer.jsx
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
// Copyright (c) 2017 PlanGrid, Inc.
import React, { Component } from 'react';
import 'styles/photo-viewer.scss';
export default class PhotoViewer extends Component {
componentDidMount() {
const { originalWidth, originalHeight } = this.props;
const imageDimensions = this.getImageDimensions.call(this, originalWidth, originalHeight);
this.props.texture.image.style.width = `${imageDimensions.width}px`;
this.props.texture.image.style.height = `${imageDimensions.height}px`;
this.props.texture.image.setAttribute('class', 'photo');
document.getElementById(this.getElementId()).appendChild(this.props.texture.image);
}
getElementId() {
return `pg-photo-container-${this.props.uniqIdentifier}`;
}
getImageDimensions(originalWidth, originalHeight) {
// Scale image to fit into viewer
let imgHeight;
let imgWidth;
const { height: viewerHeight, width: viewerWidth } = this.props;
if (originalHeight <= viewerHeight && originalWidth <= viewerWidth) {
imgWidth = originalWidth;
imgHeight = originalHeight;
} else {
const heightRatio = viewerHeight / originalHeight;
const widthRatio = viewerWidth / originalWidth;
if (heightRatio < widthRatio) {
imgHeight = originalHeight * heightRatio;
imgWidth = originalWidth * heightRatio;
} else {
imgHeight = originalHeight * widthRatio;
imgWidth = originalWidth * widthRatio;
}
}
return { height: imgHeight, width: imgWidth };
}
render() {
const containerStyles = {
width: `${this.props.width}px`,
height: `${this.props.height}px`,
};
return (
<div style={containerStyles} className="photo-viewer-container" id={this.getElementId()} />
);
}
}