Files
Iris/src/js/components/Parallax.js

144 lines
3.9 KiB
JavaScript
Raw Normal View History

2016-10-25 17:01:37 +13:00
import React, { PropTypes } from 'react'
import * as helpers from '../helpers'
export default class Parallax extends React.Component{
constructor(props) {
super(props);
this.state = {
2016-10-25 21:36:33 +13:00
scrollTop: 0,
windowWidth: 0,
windowHeight: 0,
canvas: {
width: 0,
height: 0
},
2016-10-26 08:51:28 +13:00
original: {},
image: {},
imageObject: {}
2016-10-25 17:01:37 +13:00
}
}
componentDidMount(){
2016-10-26 08:51:28 +13:00
var url = helpers.SizedImages( this.props.images ).huge;
this.loadImage( url )
2016-10-25 21:36:33 +13:00
.then(
2016-10-26 08:51:28 +13:00
response => {
this.setState({ image: response })
this.updateCanvas( response )
}
2016-10-25 21:36:33 +13:00
)
2016-10-26 08:51:28 +13:00
$(window).resize(() => this.updateCanvas( this.state.image ));
2016-10-25 21:36:33 +13:00
}
loadImage( url ){
return new Promise( (resolve, reject) => {
var imageObject = new Image();
imageObject.src = url;
imageObject.onload = function(){
2016-10-26 08:51:28 +13:00
var image = {
width: imageObject.naturalWidth,
height: imageObject.naturalHeight,
original_width: imageObject.naturalWidth,
original_height: imageObject.naturalHeight,
object: imageObject
}
resolve( image )
2016-10-25 21:36:33 +13:00
}
2016-10-25 17:01:37 +13:00
})
2016-10-25 21:36:33 +13:00
}
2016-10-26 08:51:28 +13:00
updateCanvas( image ){
2016-10-25 21:36:33 +13:00
var canvasWidth = $('.parallax').outerWidth();
var canvasHeight = $('.parallax').outerHeight();
if( this.state.canvas.width != canvasWidth || this.state.canvas.height != canvasHeight ){
this.setState({
canvas: {
width: canvasWidth,
height: canvasHeight
}
})
}
2016-10-26 08:51:28 +13:00
this.renderCanvas( image );
2016-10-25 17:01:37 +13:00
}
2016-10-26 08:51:28 +13:00
renderCanvas( image ){
2016-10-25 21:36:33 +13:00
let self = this;
var canvasDOM = document.getElementById('parallax-canvas');
var context = canvasDOM.getContext('2d');
// zoom image to fill canvas, widthwise
2016-10-26 08:51:28 +13:00
if( image.width < this.state.canvas.width || image.width > this.state.canvas.width ){
var scale = this.state.canvas.width / image.width;
2016-10-25 21:36:33 +13:00
image.width = image.width * scale;
image.height = image.height * scale;
}
// now check for fill heightwise, and zoom in if necessary
2016-10-26 08:51:28 +13:00
if( image.height < this.state.canvas.height ){
var scale = this.state.canvas.height / image.height;
2016-10-25 21:36:33 +13:00
image.width = image.width * scale;
image.height = image.height * scale;
}
// figure out where we want the image to be, based on scroll position
2016-10-26 08:51:28 +13:00
var percent = Math.round( self.state.scrollTop / this.state.canvas.height * 100 );
var position = Math.round( (this.state.canvas.height / 2) * (percent/100) ) - 100;
2016-10-25 21:36:33 +13:00
2016-10-26 08:51:28 +13:00
image.x = ( this.state.canvas.width / 2 ) - ( image.width / 2 );
image.y = ( ( this.state.canvas.height / 2 ) - ( image.height / 2 ) ) + ( ( percent / 100 ) * 100);
2016-10-25 21:36:33 +13:00
// actually draw the image on the canvas
2016-10-26 08:51:28 +13:00
context.drawImage(image.object, image.x, image.y, image.width, image.height);
2016-10-25 21:36:33 +13:00
2016-10-26 08:51:28 +13:00
// now update our component
2016-10-25 21:36:33 +13:00
self.setState({ image: image });
// poll for scroll changes
/*
var animateInterval = $interval(
function(){
window.requestAnimationFrame(function( event ){
var bannerPanel = $(document).find('.intro');
// if we've scrolled or resized
if(
state.scrollTop != $(document).scrollTop() ||
state.windowWidth != $(window).width() ||
state.windowHeight != $(window).height() ){
// update our state
state.scrollTop = $(document).scrollTop();
state.windowWidth = $(window).width();
state.windowHeight = $(window).height();
var bannerHeight = bannerPanel.outerHeight();
// and if we're within the bounds of our document
// this helps prevent us animating when the objects in question are off-screen
if( state.scrollTop < bannerHeight ){
positionArtistBackground( image );
}
}
});
},
10
);*/
2016-10-25 17:01:37 +13:00
}
render(){
return (
<div className="parallax">
2016-10-25 21:36:33 +13:00
<canvas id="parallax-canvas" width={this.state.canvas.width} height={this.state.canvas.height}></canvas>
2016-10-25 17:01:37 +13:00
</div>
);
}
}