Created
January 23, 2015 04:39
-
-
Save marr/c11fecb733de43135d0c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var self = this; | |
var setupCanvas = function() { | |
var w = this.naturalWidth, | |
h = this.naturalHeight, | |
aspectRatio = w / h; | |
var scaled = (h != this.naturalHeight || w != this.naturalWidth); | |
var sixteenByNine = 16 / 9; | |
if (aspectRatio > sixteenByNine) { | |
// scale down to match container height (landscape) | |
w = self.props.width; | |
h = Math.min(self.props.width / aspectRatio, this.naturalWidth); | |
} else if (aspectRatio <= sixteenByNine) { | |
// scale down to match container width (portrait) | |
h = Math.min(w / aspectRatio, self.props.height); | |
w = self.props.width; | |
} | |
// for a dynamically shown and sized canvas element | |
self.setState({ scaled: scaled, width: w, height: h }, function() { | |
var canvas = self.refs.bg.getDOMNode(); | |
var canvasContext = canvas.getContext('2d'); | |
canvasContext.drawImage(this, 0, 0, w, h); | |
if (scaled) { | |
// get original and blur it | |
var imageData = canvasContext.getImageData(0, 0, w, h); | |
stackblur(imageData.data, w, h, self.props.blurRadius); | |
canvasContext.putImageData(imageData, 0, 0); | |
var destX = canvas.width / 2 - this.naturalWidth / 2; | |
// draw cropped original back in | |
canvasContext.drawImage(this, 0, 0, w, h, destX, 0, w, h); | |
} | |
}.bind(this)); | |
}; | |
setupCanvas.call(e.target); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment