Skip to content

Commit

Permalink
Feat: Use new QRCode implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Miaonster committed Jun 5, 2019
1 parent 03459ba commit 0666940
Show file tree
Hide file tree
Showing 11 changed files with 1,744 additions and 801 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ dist/
.rn_temp/
node_modules/
.DS_Store
yarn-error.log
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "taro-code",
"version": "1.1.0",
"version": "1.2.0",
"description": "Taro.js barcode & qrcode",
"scripts": {
"build": "export TARO_BUILD_TYPE=ui && taro build --ui",
Expand All @@ -17,9 +17,9 @@
"dev:h5": "npm run build:h5 -- --watch",
"dev:rn": "npm run build:rn -- --watch"
},
"files": ["dist"],
"author": "Miaonster <[email protected]>",
"license": "MIT",
"files": ["dist"],
"main": "dist/index.js",
"dependencies": {
"@tarojs/components": "1.2.26",
Expand Down
2 changes: 1 addition & 1 deletion src/components/Barcode/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Barcode extends Component {
}

Barcode.defaultProps = {
text: '',
text: 'HELLO',
width: 375,
height: 80,
}
Expand Down
25 changes: 11 additions & 14 deletions src/components/Qrcode/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@ import './style.css'
class QRCode extends Component {
componentDidMount () {
const ctx = Taro.createCanvasContext('qrcode', this)

utils.qrcode({
utils.qrcode(this.props.text, {
ctx,
text: this.props.text,
width: this.props.width * 4,
height: this.props.height * 4,
size: this.props.size * 2,
padding: 0,
})
ctx.draw()
}

render () {
const style = {
width: this.props.width * 4 + 'px',
height: this.props.height * 4 + 'px',
width: this.props.size * 2 + 'px',
height: this.props.size * 2 + 'px',
}

const wrapStyle = {
width: this.props.width + 'px',
height: this.props.height + 'px',
width: this.props.size + 'px',
height: this.props.size + 'px',
}

return (
Expand All @@ -36,15 +35,13 @@ class QRCode extends Component {
}

QRCode.defaultProps = {
text: '',
width: 300,
height: 300,
text: 'HELLO',
size: 300,
}

QRCode.propTypes = {
text: PropTypes.string,
width: PropTypes.number,
height: PropTypes.number,
size: PropTypes.number,
}

export default QRCode
4 changes: 2 additions & 2 deletions src/pages/index/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export default class Index extends Component {
render () {
return (
<View className='index'>
<Barcode text='hello' width={320} />
<QRCode text='hello' />
<Barcode text='hello' width={320} height={60} />
<QRCode text='hello world' size={300} />
</View>
)
}
Expand Down
12 changes: 4 additions & 8 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Taro from '@tarojs/taro'
import { code128 } from './barcode';
import qrcode from './qrcode';
import { code128 } from './barcode'
import { drawQRCodeToCanvas } from './qrcode'

function convertLength(length) {
return Math.round(Taro.getSystemInfoSync().windowWidth * length / 750);
Expand All @@ -10,12 +10,8 @@ function barc({ ctx, text, width, height }) {
code128(ctx, text, convertLength(width), convertLength(height))
}

function qrc({ ctx, text, width, height }) {
qrcode.api.draw(text, {
ctx,
width: convertLength(width),
height: convertLength(height)
})
function qrc(text, options) {
drawQRCodeToCanvas(text, options)
}

export default {
Expand Down
Loading

0 comments on commit 0666940

Please sign in to comment.