Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add img (logo) and download api #72

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ prop | type | default value
`fgColor` | `string` (CSS color) | `"#000000"`
`level` | `string` (`'L' 'M' 'Q' 'H'`) | `'L'`
`includeMargin` | `boolean` | `false`
`ref` | `function` [react's ref](https://reactjs.org/docs/refs-and-the-dom.html) |
`img` | see below | see below

### `img` Props

prop | type | default value
----------|-----------------------|--------------
`src` | `string` |
`top` | `number` (percentage) | `50`
`left` | `number` (percentage) | `50`
`width` | `number` (percentage) | `10`
`height` | `number` (percentage) | `10`

## Custom Styles

Expand All @@ -40,5 +52,18 @@ prop | type | default value

<img src="qrcode.png" height="256" width="256">

## Download (or Export) as File

You need to set `ref={ref => this.qrcode = ref}` to get the component reference first, then call:

```javascript
this.qrcode.download('QR Code.png');
```

related methods:

* `genCanvas(overwritingProps): Promise<HTMLCanvasElement>`
* `genCanvasDataURL(type, overwritingProps): Promise<string>`
* `download(filename, type, overwritingProps)`

## LICENSE [ISC](LICENSE)
107 changes: 104 additions & 3 deletions examples/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
var QRCode = require('..');
var React = require('react');
var ReactDOM = require('react-dom');
var qrCodeDownloadCode = `this.qrcode.download('QR Code.png');`;

// TODO: live update demo
class Demo extends React.Component {
state = {
value: 'http://picturesofpeoplescanningqrcodes.tumblr.com/',
Expand All @@ -14,9 +14,28 @@ class Demo extends React.Component {
level: 'L',
renderAs: 'svg',
includeMargin: false,
withImg: false,
img: {
src: './logo.png',
top: 50,
left: 50,
width: 10,
height: 10,
},
};

downloadQRCode = () => {
this.qrcode.download('QR Code.png');
};

setImgState(state) {
this.setState({img: {...this.state.img, ...state}});
}

render() {
var imgCode = this.state.withImg
? ` img={${JSON.stringify(this.state.img)}}\n`
: '';
var code = `<QRCode
value={"${this.state.value}"}
size={${this.state.size}}
Expand All @@ -25,7 +44,8 @@ class Demo extends React.Component {
level={"${this.state.level}"}
includeMargin={${this.state.includeMargin}}
renderAs={"${this.state.renderAs}"}
/>`;
ref={ref => this.qrcode = ref}
${imgCode}/>`;
return (
<div>
<div>
Expand Down Expand Up @@ -100,6 +120,80 @@ class Demo extends React.Component {
</select>
</label>
</div>
<div>
<label>
With Img:
<br />
<input
type="checkbox"
checked={this.state.withImg}
onChange={(e) => this.setState({withImg: e.target.checked})}
/>
</label>
</div>
<div style={this.state.withImg ? {} : {display: 'none'}}>
<div>
<label>
Img src:
<br />
<input
onChange={(e) => this.setImgState({src: e.target.value})}
value={this.state.img.src}
/>
</label>
</div>
<div>
<label>Img Coordinate:</label>
<br />
<div style={{marginLeft: '20px'}}>
<label>
top (percent):
<input
type="number"
onChange={(e) =>
this.setImgState({top: parseInt(e.target.value, 10) || 50})
}
value={this.state.img.top}
/>
</label>
<label>
, left (percent):
<input
type="number"
onChange={(e) =>
this.setImgState({left: parseInt(e.target.value, 10) || 50})
}
value={this.state.img.left}
/>
</label>
<br />
<label>
width (percent):
<input
type="number"
onChange={(e) =>
this.setImgState({
width: parseInt(e.target.value, 10) || 10,
})
}
value={this.state.img.width}
/>
</label>
<label>
height (percent):
<input
type="number"
onChange={(e) =>
this.setImgState({
height: parseInt(e.target.value, 10) || 10,
})
}
value={this.state.img.height}
/>
</label>
</div>
</div>
</div>
<div>
<label>
Value:
Expand All @@ -117,7 +211,7 @@ class Demo extends React.Component {
<label>
Use it:
<br />
<textarea rows="6" cols="80" disabled={true} value={code} />
<textarea rows="11" cols="80" disabled={true} value={code} />
</label>
</div>

Expand All @@ -129,7 +223,14 @@ class Demo extends React.Component {
level={this.state.level}
renderAs={this.state.renderAs}
includeMargin={this.state.includeMargin}
{...(this.state.withImg ? {img: this.state.img} : {})}
ref={(ref) => (this.qrcode = ref)}
/>

<br />
<button onClick={this.downloadQRCode} style={{fontFamily: 'monospace'}}>
{qrCodeDownloadCode}
</button>
</div>
);
}
Expand Down
Binary file added examples/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading