Skip to content

Commit

Permalink
Fix image display
Browse files Browse the repository at this point in the history
  • Loading branch information
Filipe Corrêa committed Jul 9, 2019
1 parent c1e3013 commit 5b672b3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 32 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-image-hotspots",
"version": "1.0.2",
"version": "1.0.3",
"description": "React component for rendering images with hotspots",
"main": "dist/ImageHotspots.js",
"files": [
Expand Down
49 changes: 19 additions & 30 deletions src/ImageHotspots.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react'
// import './ImageHotspots.css'

class ImageHotspots extends React.Component {
constructor (props) {
Expand Down Expand Up @@ -39,11 +38,11 @@ class ImageHotspots extends React.Component {
const imageLoaded = image.initialWidth && image.initialHeight

const containerStyle = {
width: '100vw',
height: '100vh',
width: '100%',
height: '100%',
position: 'relative',
overflow: 'hidden',
textAlign: 'center',
background: '#eee'
textAlign: 'center'
}

let imageStyle = {}
Expand Down Expand Up @@ -72,26 +71,16 @@ class ImageHotspots extends React.Component {

if (imageLoaded) {
if (image.orientation === 'landscape') {
imageStyle.height = image.height

minimapStyle.width = 100 * image.ratio + 'px'
minimapStyle.height = 100 + 'px'

guideStyle.width = (container.width > image.width)
? 100 * image.ratio + 'px'
: (100 * image.ratio) / (image.width / container.width) + 'px'
guideStyle.height = 100 / image.scale + 'px'
} else {
imageStyle.width = image.width

minimapStyle.width = 100 + 'px'
minimapStyle.height = 100 * image.ratio + 'px'

guideStyle.width = 100 / image.scale + 'px'
guideStyle.height = (container.height > image.height)
? 100 * image.ratio + 'px'
: (100 * image.ratio) / (image.height / container.height) + 'px'
} else {
imageStyle.height = image.height
}
minimapStyle.width = 100 * image.ratio
minimapStyle.height = 100
guideStyle.width = (container.width > image.width)
? 100 * image.ratio
: (100 * image.ratio) / (image.width / container.width)
guideStyle.height = 100 / image.scale
}

return (
Expand Down Expand Up @@ -122,12 +111,12 @@ class ImageHotspots extends React.Component {
...prevState.image,
initialWidth,
initialHeight,
width: (orientation === 'portrait')
width: (orientation === 'landscape')
? container.width
: container.height * ratio,
: container.height / ratio,
height: (orientation === 'landscape')
? container.height
: container.width * ratio,
? container.width / ratio
: container.height,
scale: 1,
ratio,
orientation
Expand All @@ -138,12 +127,12 @@ class ImageHotspots extends React.Component {
zoom (scale) {
if (scale > 0) {
const { container, image } = this.state
const width = (image.orientation === 'portrait')
const width = (image.orientation === 'landscape')
? container.width * scale
: container.height * image.ratio * scale
const height = (image.orientation === 'landscape')
? container.height * scale
: container.width * image.ratio * scale
? container.width * image.ratio / scale
: container.height * scale

if (width < image.initialWidth && height < image.initialHeight) {
this.setState((prevState) => ({
Expand Down
8 changes: 7 additions & 1 deletion src/ImageHotspots.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ import imageFile from './sample.jpeg'
const image = { src: imageFile, alt: 'my image' }

storiesOf('ImageHotspots', module)
.add('default', () => <ImageHotspots src={image.src} alt={image.alt} />)
.add('default', () => {
return (
<div style={{ width: '400px', height: '300px', padding: 20, background: 'black', display: 'flex' }}>
<ImageHotspots src={image.src} alt={image.alt} />
</div>
)
})

0 comments on commit 5b672b3

Please sign in to comment.