Skip to content

Commit

Permalink
qrcode: Support prop type for rendering as img. Close #900
Browse files Browse the repository at this point in the history
  • Loading branch information
airyland committed Feb 13, 2017
1 parent c14c011 commit af9aa2b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
21 changes: 18 additions & 3 deletions src/components/qrcode/index.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<template>
<canvas
<div>
<canvas
:style="{height: size, width: size}"
:height="size"
:width="size"></canvas>
:width="size" v-show="type === 'canvas'" ref="canvas"></canvas>
<img :src="imgData" v-if="type === 'img'">
</div>
</template>

<script>
Expand All @@ -27,13 +30,22 @@ export default {
fgColor: {
type: String,
default: '#000000'
},
type: {
type: String,
default: 'img'
}
},
mounted () {
this.$nextTick(() => {
this.render()
})
},
data () {
return {
imgData: ''
}
},
watch: {
value () {
this.render()
Expand All @@ -57,7 +69,7 @@ export default {
qrcode.addData(this.value)
qrcode.make()
const canvas = this.$el
const canvas = this.$refs.canvas
const ctx = canvas.getContext('2d')
const cells = qrcode.modules
Expand All @@ -75,6 +87,9 @@ export default {
ctx.fillRect(Math.round(cdx * tileW), Math.round(rdx * tileH), w, h)
})
})
if (this.type === 'img') {
this.imgData = canvas.toDataURL('image/png')
}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/components/qrcode/metas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,12 @@ props:
default: '#000000'
en: front color
zh-CN: 二维码着色
type:
type: String
default: img
en: 'render type, can be `img` or `canvas`'
zh-CN: '渲染类型,可以为`img`(适合需要在微信需要长按识别的场景)和`canvas`'
changes:
v2.1.0-rc.47:
zh-CN:
- '[feature] 支持渲染类型为图片 #900 @keepgoingwm'
13 changes: 9 additions & 4 deletions src/demos/Qrcode.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<template>
<div style="text-align:center;margin-top:15px;">
<divider>{{ $t('Notice: It cannot be extracted by Wechat') }}</divider>
<qrcode value="https://vux.li"></qrcode>
<divider>{{ $t('default type = img') }}</divider>
<qrcode value="https://vux.li?x-page=demo_qrcode" type="img"></qrcode>
<br>
<br>
<divider>{{ $t('type = canvas') }}</divider>
<qrcode value="https://vux.li?x-page=demo_qrcode"></qrcode>
<br>
<qrcode :value="value" :fg-color="fgColor"></qrcode>
<br>
<span>{{ $t('current url') }}: {{value}}</span>
Expand All @@ -13,8 +16,10 @@
</template>

<i18n>
'Notice: It cannot be extracted by Wechat':
zh-CN: 注意:canvas不能在微信里长按识别
default type = img:
zh-CN: '默认类型为 img,可以在微信里长按识别'
type = canvas:
zh-CN: '类型为 canvas'
current url:
zh-CN: 当前url
current fgColor:
Expand Down

0 comments on commit af9aa2b

Please sign in to comment.