Skip to content

Commit 8a34f62

Browse files
committed
feat: optimize code
1 parent d820c8e commit 8a34f62

File tree

5 files changed

+207
-218
lines changed

5 files changed

+207
-218
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@kiner/rc-qrcode",
2+
"name": "@react-component/qrcode",
33
"version": "1.0.2",
44
"description": "base abstract trigger component for react",
55
"engines": {

src/QRCodeCanvas.tsx

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import React, { useCallback, useEffect, useRef, useState } from 'react';
22
import { useQRCode } from './hooks/useQRCode';
33
import type { QRPropsCanvas } from './interface';
44
import {
5-
DEFAULT_BGCOLOR,
6-
DEFAULT_FGCOLOR,
7-
DEFAULT_INCLUDEMARGIN,
5+
DEFAULT_BACKGROUND_COLOR,
6+
DEFAULT_FRONT_COLOR,
7+
DEFAULT_NEED_MARGIN,
88
DEFAULT_LEVEL,
99
DEFAULT_MINVERSION,
1010
DEFAULT_SIZE,
11-
SUPPORTS_PATH2D,
11+
isSupportPath2d,
1212
excavateModules,
1313
generatePath,
1414
} from './utils';
@@ -19,9 +19,9 @@ const QRCodeCanvas = React.forwardRef<HTMLCanvasElement, QRPropsCanvas>(
1919
value,
2020
size = DEFAULT_SIZE,
2121
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,
2525
minVersion = DEFAULT_MINVERSION,
2626
marginSize,
2727
style,
@@ -32,7 +32,6 @@ const QRCodeCanvas = React.forwardRef<HTMLCanvasElement, QRPropsCanvas>(
3232
const _canvas = useRef<HTMLCanvasElement | null>(null);
3333
const _image = useRef<HTMLImageElement>(null);
3434

35-
// Set the local ref (_canvas) and also the forwarded ref from outside
3635
const setCanvasRef = useCallback(
3736
(node: HTMLCanvasElement | null) => {
3837
_canvas.current = node;
@@ -45,9 +44,7 @@ const QRCodeCanvas = React.forwardRef<HTMLCanvasElement, QRPropsCanvas>(
4544
[forwardedRef],
4645
);
4746

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+
5148
const [, setIsImageLoaded] = useState(false);
5249

5350
const { margin, cells, numCells, calculatedImageSettings } = useQRCode({
@@ -61,8 +58,6 @@ const QRCodeCanvas = React.forwardRef<HTMLCanvasElement, QRPropsCanvas>(
6158
});
6259

6360
useEffect(() => {
64-
// Always update the canvas. It's cheap enough and we want to be correct
65-
// with the current state.
6661
if (_canvas.current != null) {
6762
const canvas = _canvas.current;
6863

@@ -89,23 +84,17 @@ const QRCodeCanvas = React.forwardRef<HTMLCanvasElement, QRPropsCanvas>(
8984
}
9085
}
9186

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.
9687
const pixelRatio = window.devicePixelRatio || 1;
9788
canvas.height = canvas.width = size * pixelRatio;
9889
const scale = (size / numCells) * pixelRatio;
9990
ctx.scale(scale, scale);
100-
// console.log('scale', scale, 'size', size, 'numCells', numCells, 'pixelRatio', pixelRatio);
10191

102-
// Draw solid background, only paint dark modules.
92+
10393
ctx.fillStyle = bgColor;
10494
ctx.fillRect(0, 0, numCells, numCells);
10595

10696
ctx.fillStyle = fgColor;
107-
if (SUPPORTS_PATH2D) {
108-
// $FlowFixMe: Path2D c'tor doesn't support args yet.
97+
if (isSupportPath2d) {
10998
ctx.fill(new Path2D(generatePath(cellsToDraw, margin)));
11099
} else {
111100
cells.forEach(function (row, rdx) {
@@ -133,8 +122,6 @@ const QRCodeCanvas = React.forwardRef<HTMLCanvasElement, QRPropsCanvas>(
133122
}
134123
});
135124

136-
// Ensure we mark image loaded as false here so we trigger updating the
137-
// canvas in our other effect.
138125
useEffect(() => {
139126
setIsImageLoaded(false);
140127
}, [imgSrc]);
@@ -151,6 +138,8 @@ const QRCodeCanvas = React.forwardRef<HTMLCanvasElement, QRPropsCanvas>(
151138
setIsImageLoaded(true);
152139
}}
153140
ref={_image}
141+
// when crossOrigin is not set, the image will be tainted
142+
// and the canvas cannot be exported to an image
154143
crossOrigin={calculatedImageSettings?.crossOrigin}
155144
/>
156145
);

src/QRCodeSVG.tsx

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React from 'react';
22
import type { QRPropsSVG } from './interface';
33
import {
4-
DEFAULT_BGCOLOR,
5-
DEFAULT_FGCOLOR,
6-
DEFAULT_INCLUDEMARGIN,
4+
DEFAULT_BACKGROUND_COLOR,
5+
DEFAULT_FRONT_COLOR,
6+
DEFAULT_NEED_MARGIN,
77
DEFAULT_LEVEL,
88
DEFAULT_MINVERSION,
99
DEFAULT_SIZE,
@@ -18,9 +18,9 @@ const QRCodeSVG = React.forwardRef<SVGSVGElement, QRPropsSVG>(
1818
value,
1919
size = DEFAULT_SIZE,
2020
level = DEFAULT_LEVEL,
21-
bgColor = DEFAULT_BGCOLOR,
22-
fgColor = DEFAULT_FGCOLOR,
23-
includeMargin = DEFAULT_INCLUDEMARGIN,
21+
bgColor = DEFAULT_BACKGROUND_COLOR,
22+
fgColor = DEFAULT_FRONT_COLOR,
23+
includeMargin = DEFAULT_NEED_MARGIN,
2424
minVersion = DEFAULT_MINVERSION,
2525
title,
2626
marginSize,
@@ -57,18 +57,13 @@ const QRCodeSVG = React.forwardRef<SVGSVGElement, QRPropsSVG>(
5757
y={calculatedImageSettings.y + margin}
5858
preserveAspectRatio="none"
5959
opacity={calculatedImageSettings.opacity}
60-
// Note: specified here always, but undefined will result in no attribute.
60+
// when crossOrigin is not set, the image will be tainted
61+
// and the canvas cannot be exported to an image
6162
crossOrigin={calculatedImageSettings.crossOrigin}
6263
/>
6364
);
6465
}
6566

66-
// Drawing strategy: instead of a rect per module, we're going to create a
67-
// single path for the dark modules and layer that on top of a light rect,
68-
// for a total of 2 DOM nodes. We pay a bit more in string concat but that's
69-
// way faster than DOM ops.
70-
// For level 1, 441 nodes -> 2
71-
// For level 40, 31329 -> 2
7267
const fgPath = generatePath(cellsToDraw, margin);
7368

7469
return (

0 commit comments

Comments
 (0)