Skip to content
This repository has been archived by the owner on Apr 23, 2019. It is now read-only.

Commit

Permalink
Add logoWidth/logoHeight image size props
Browse files Browse the repository at this point in the history
  • Loading branch information
scmx committed May 26, 2017
1 parent ea1c2d3 commit 229f451
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/index.js
Expand Up @@ -32,6 +32,8 @@ type Props = {
bgColor: string,
fgColor: string,
logo: string,
logoHeight: number,
logoWidth: number,
};

class QRCode extends React.Component {
Expand All @@ -51,6 +53,9 @@ class QRCode extends React.Component {
level: PropTypes.oneOf(['L', 'M', 'Q', 'H']),
bgColor: PropTypes.string,
fgColor: PropTypes.string,
logo: PropTypes.string,
logoHeight: PropTypes.number,
logoWidth: PropTypes.number,
};

shouldComponentUpdate(nextProps: Props) {
Expand Down Expand Up @@ -112,11 +117,16 @@ class QRCode extends React.Component {
var size = this.props.size;
var image = document.createElement('img');
image.src = this.props.logo;
image.onload = function() {
var dx = size / 2 - size * 0.1;
var dwidth = size * 0.2;
image.onload = () => {
var dwidth = this.props.logoWidth || size * 0.2;
var dheight =
this.props.logoHeight || image.height / image.width * dwidth;
var dx = (size - dwidth) / 2;
var dy = (size - dheight) / 2;
image.width = dwidth;
image.height = dheight;
if (ctx) {
ctx.drawImage(image, dx, dx, dwidth, dwidth);
ctx.drawImage(image, dx, dy, dwidth, dwidth);
}
};
}
Expand Down

0 comments on commit 229f451

Please sign in to comment.