@@ -2,13 +2,13 @@ import React, { useCallback, useEffect, useRef, useState } from 'react';
2
2
import { useQRCode } from './hooks/useQRCode' ;
3
3
import type { QRPropsCanvas } from './interface' ;
4
4
import {
5
- DEFAULT_BGCOLOR ,
6
- DEFAULT_FGCOLOR ,
7
- DEFAULT_INCLUDEMARGIN ,
5
+ DEFAULT_BACKGROUND_COLOR ,
6
+ DEFAULT_FRONT_COLOR ,
7
+ DEFAULT_NEED_MARGIN ,
8
8
DEFAULT_LEVEL ,
9
9
DEFAULT_MINVERSION ,
10
10
DEFAULT_SIZE ,
11
- SUPPORTS_PATH2D ,
11
+ isSupportPath2d ,
12
12
excavateModules ,
13
13
generatePath ,
14
14
} from './utils' ;
@@ -19,9 +19,9 @@ const QRCodeCanvas = React.forwardRef<HTMLCanvasElement, QRPropsCanvas>(
19
19
value,
20
20
size = DEFAULT_SIZE ,
21
21
level = DEFAULT_LEVEL ,
22
- bgColor = DEFAULT_BGCOLOR ,
23
- fgColor = DEFAULT_FGCOLOR ,
24
- includeMargin = DEFAULT_INCLUDEMARGIN ,
22
+ bgColor = DEFAULT_BACKGROUND_COLOR ,
23
+ fgColor = DEFAULT_FRONT_COLOR ,
24
+ includeMargin = DEFAULT_NEED_MARGIN ,
25
25
minVersion = DEFAULT_MINVERSION ,
26
26
marginSize,
27
27
style,
@@ -32,7 +32,6 @@ const QRCodeCanvas = React.forwardRef<HTMLCanvasElement, QRPropsCanvas>(
32
32
const _canvas = useRef < HTMLCanvasElement | null > ( null ) ;
33
33
const _image = useRef < HTMLImageElement > ( null ) ;
34
34
35
- // Set the local ref (_canvas) and also the forwarded ref from outside
36
35
const setCanvasRef = useCallback (
37
36
( node : HTMLCanvasElement | null ) => {
38
37
_canvas . current = node ;
@@ -45,9 +44,7 @@ const QRCodeCanvas = React.forwardRef<HTMLCanvasElement, QRPropsCanvas>(
45
44
[ forwardedRef ] ,
46
45
) ;
47
46
48
- // We're just using this state to trigger rerenders when images load. We
49
- // Don't actually read the value anywhere. A smarter use of useEffect would
50
- // depend on this value.
47
+
51
48
const [ , setIsImageLoaded ] = useState ( false ) ;
52
49
53
50
const { margin, cells, numCells, calculatedImageSettings } = useQRCode ( {
@@ -61,8 +58,6 @@ const QRCodeCanvas = React.forwardRef<HTMLCanvasElement, QRPropsCanvas>(
61
58
} ) ;
62
59
63
60
useEffect ( ( ) => {
64
- // Always update the canvas. It's cheap enough and we want to be correct
65
- // with the current state.
66
61
if ( _canvas . current != null ) {
67
62
const canvas = _canvas . current ;
68
63
@@ -89,23 +84,17 @@ const QRCodeCanvas = React.forwardRef<HTMLCanvasElement, QRPropsCanvas>(
89
84
}
90
85
}
91
86
92
- // We're going to scale this so that the number of drawable units
93
- // matches the number of cells. This avoids rounding issues, but does
94
- // result in some potentially unwanted single pixel issues between
95
- // blocks, only in environments that don't support Path2D.
96
87
const pixelRatio = window . devicePixelRatio || 1 ;
97
88
canvas . height = canvas . width = size * pixelRatio ;
98
89
const scale = ( size / numCells ) * pixelRatio ;
99
90
ctx . scale ( scale , scale ) ;
100
- // console.log('scale', scale, 'size', size, 'numCells', numCells, 'pixelRatio', pixelRatio);
101
91
102
- // Draw solid background, only paint dark modules.
92
+
103
93
ctx . fillStyle = bgColor ;
104
94
ctx . fillRect ( 0 , 0 , numCells , numCells ) ;
105
95
106
96
ctx . fillStyle = fgColor ;
107
- if ( SUPPORTS_PATH2D ) {
108
- // $FlowFixMe: Path2D c'tor doesn't support args yet.
97
+ if ( isSupportPath2d ) {
109
98
ctx . fill ( new Path2D ( generatePath ( cellsToDraw , margin ) ) ) ;
110
99
} else {
111
100
cells . forEach ( function ( row , rdx ) {
@@ -133,8 +122,6 @@ const QRCodeCanvas = React.forwardRef<HTMLCanvasElement, QRPropsCanvas>(
133
122
}
134
123
} ) ;
135
124
136
- // Ensure we mark image loaded as false here so we trigger updating the
137
- // canvas in our other effect.
138
125
useEffect ( ( ) => {
139
126
setIsImageLoaded ( false ) ;
140
127
} , [ imgSrc ] ) ;
@@ -151,6 +138,8 @@ const QRCodeCanvas = React.forwardRef<HTMLCanvasElement, QRPropsCanvas>(
151
138
setIsImageLoaded ( true ) ;
152
139
} }
153
140
ref = { _image }
141
+ // when crossOrigin is not set, the image will be tainted
142
+ // and the canvas cannot be exported to an image
154
143
crossOrigin = { calculatedImageSettings ?. crossOrigin }
155
144
/>
156
145
) ;
0 commit comments