1- import { defineComponent , h , onMounted , onUpdated , PropType , ref } from 'vue'
1+ import { defineComponent , Fragment , h , onMounted , onUpdated , PropType , ref } from 'vue'
22import QR from './qrcodegen'
33
44type Modules = ReturnType < QR . QrCode [ 'getModules' ] >
@@ -98,7 +98,7 @@ function getImageSettings(
9898 w : number
9999 excavation : Excavation | null
100100} {
101- const { width, height, x : imageX , y : imageY } = imageSettings
101+ const { width, height, x : imageX , y : imageY } = imageSettings
102102 const numCells = cells . length + margin * 2
103103 const defaultSize = Math . floor ( size * 0.1 )
104104 const scale = numCells / size
@@ -178,7 +178,7 @@ const QRCodeVueProps = {
178178 } ,
179179}
180180
181- const QRCodeSvg = defineComponent ( {
181+ export const QRCodeSvg = defineComponent ( {
182182 name : 'QRCodeSvg' ,
183183 props : QRCodeProps ,
184184 setup ( props ) {
@@ -195,8 +195,8 @@ const QRCodeSvg = defineComponent({
195195 if ( props . imageSettings . src ) {
196196 const imageSettings = getImageSettings ( cells , props . size , margin , props . imageSettings )
197197 imageProps = {
198- x : imageSettings . x ,
199- y : imageSettings . y ,
198+ x : imageSettings . x + margin ,
199+ y : imageSettings . y + margin ,
200200 width : imageSettings . w ,
201201 height : imageSettings . h ,
202202 }
@@ -245,11 +245,12 @@ const QRCodeSvg = defineComponent({
245245 } ,
246246} )
247247
248- const QRCodeCanvas = defineComponent ( {
248+ export const QRCodeCanvas = defineComponent ( {
249249 name : 'QRCodeCanvas' ,
250250 props : QRCodeProps ,
251- setup ( props ) {
251+ setup ( props , ctx ) {
252252 const canvasEl = ref < HTMLCanvasElement | null > ( null )
253+ const imageRef = ref < HTMLImageElement | null > ( null )
253254
254255 const generate = ( ) => {
255256 const { value, level, size, margin, background, foreground } = props
@@ -266,9 +267,27 @@ const QRCodeCanvas = defineComponent({
266267 return
267268 }
268269
269- const cells = QR . QrCode . encodeText ( value , ErrorCorrectLevelMap [ level ] ) . getModules ( )
270+ let cells = QR . QrCode . encodeText ( value , ErrorCorrectLevelMap [ level ] ) . getModules ( )
270271 const numCells = cells . length + margin * 2
271272
273+ const image = imageRef . value
274+ let imageProps = { x : 0 , y : 0 , width : 0 , height : 0 }
275+ const showImage = props . imageSettings . src && image != null && image . naturalWidth !== 0 && image . naturalHeight !== 0
276+
277+ if ( showImage ) {
278+ const imageSettings = getImageSettings ( cells , props . size , margin , props . imageSettings )
279+ imageProps = {
280+ x : imageSettings . x + margin ,
281+ y : imageSettings . y + margin ,
282+ width : imageSettings . w ,
283+ height : imageSettings . h ,
284+ }
285+
286+ if ( imageSettings . excavation ) {
287+ cells = excavateModules ( cells , imageSettings . excavation )
288+ }
289+ }
290+
272291 const devicePixelRatio = window . devicePixelRatio || 1
273292
274293 const scale = ( size / numCells ) * devicePixelRatio
@@ -291,17 +310,41 @@ const QRCodeCanvas = defineComponent({
291310 } )
292311 } )
293312 }
313+
314+ if ( showImage ) {
315+ ctx . drawImage (
316+ image ,
317+ imageProps . x ,
318+ imageProps . y ,
319+ imageProps . width ,
320+ imageProps . height
321+ ) ;
322+ }
294323 }
295324
296325 onMounted ( generate )
297326 onUpdated ( generate )
298327
328+ const { style } = ctx . attrs
329+
299330 return ( ) => h (
300- 'canvas' ,
301- {
302- ref : canvasEl ,
303- style : { width : `${ props . size } px` , height : `${ props . size } px` } ,
304- } ,
331+ Fragment ,
332+ [
333+ h (
334+ 'canvas' ,
335+ {
336+ ...ctx . attrs ,
337+ ref : canvasEl ,
338+ style : { ...( style as Object ) , width : `${ props . size } px` , height : `${ props . size } px` } ,
339+ } ,
340+ ) ,
341+ props . imageSettings . src && h ( 'img' , {
342+ ref : imageRef ,
343+ src : props . imageSettings . src ,
344+ style : { display : 'none' } ,
345+ onLoad : generate ,
346+ } )
347+ ] ,
305348 )
306349 } ,
307350} )
0 commit comments