|
@@ -6,48 +6,54 @@ import { code128 } from './drawCode'
|
6
|
6
|
import './style.less'
|
7
|
7
|
|
8
|
8
|
export default (props) => {
|
9
|
|
-
|
10
|
9
|
const { code, ratio, height } = props
|
11
|
10
|
|
|
11
|
+ const id = useMemo(() => Math.random().toString(36).substring(2, 10), [])
|
12
|
12
|
const canvasRef = useRef()
|
13
|
13
|
const ctxRef = useRef()
|
14
|
14
|
|
15
|
|
- const sysInfo = useMemo(() => {
|
|
15
|
+ const { size, style } = useMemo(() => {
|
16
|
16
|
const { safeArea, pixelRatio } = Taro.getSystemInfoSync()
|
17
|
17
|
|
18
|
|
- return {
|
19
|
|
- width: safeArea.width,
|
20
|
|
- height: safeArea.height,
|
21
|
|
- dpr: pixelRatio,
|
|
18
|
+ const rpxSize = {
|
|
19
|
+ width: safeArea.width * ratio * pixelRatio,
|
|
20
|
+ height: height * pixelRatio,
|
|
21
|
+ }
|
|
22
|
+
|
|
23
|
+ const pxStyle = {
|
|
24
|
+ width: `${safeArea.width * ratio}px`,
|
|
25
|
+ height: `${height}px`
|
22
|
26
|
}
|
23
|
|
- }, [])
|
24
|
|
- const size = useMemo(() => ({ width: sysInfo.width * sysInfo.dpr * ratio, height: height * sysInfo.dpr }), [sysInfo])
|
25
|
27
|
|
26
|
|
- const style = useMemo(() => ({ width: `${size.width}rpx`, height: `${size.height}rpx` }), [size])
|
|
28
|
+ return {
|
|
29
|
+ size: rpxSize,
|
|
30
|
+ style: pxStyle,
|
|
31
|
+ }
|
|
32
|
+ }, [ratio, height])
|
27
|
33
|
|
28
|
34
|
useEffect(() => {
|
29
|
35
|
Taro.nextTick(() => {
|
30
|
36
|
const query = Taro.createSelectorQuery()
|
31
|
|
- query.select('#barCodeCanvas').fields({ node: true }).exec((res) => {
|
|
37
|
+ query.select(`#${id}`).fields({ node: true }).exec((res) => {
|
32
|
38
|
const canvas = res[0].node
|
33
|
39
|
const ctx = canvas.getContext('2d')
|
34
|
|
- canvas.width = sysInfo.width
|
35
|
|
- canvas.height = sysInfo.height
|
36
|
|
- // ctx.scale(sysInfo.dpr, sysInfo.dpr)
|
|
40
|
+ canvas.width = size.width
|
|
41
|
+ canvas.height = size.height
|
|
42
|
+ // ctx.scale(1, 1)
|
37
|
43
|
|
38
|
44
|
canvasRef.current = canvas;
|
39
|
45
|
ctxRef.current = ctx;
|
40
|
46
|
|
41
|
47
|
if (code) {
|
42
|
|
- code128(ctx, code, sysInfo.width, sysInfo.height);
|
|
48
|
+ code128(ctx, code, size.width, size.height);
|
43
|
49
|
}
|
44
|
50
|
})
|
45
|
51
|
})
|
46
|
|
- }, [code, sysInfo])
|
|
52
|
+ }, [code, size, id])
|
47
|
53
|
|
48
|
54
|
return (
|
49
|
|
- <View className='barcode-box' style={style}>
|
50
|
|
- <Canvas style={style} type='2d' id='barCodeCanvas'></Canvas>
|
|
55
|
+ <View className='barcode-box'>
|
|
56
|
+ <Canvas style={style} type='2d' id={id}></Canvas>
|
51
|
57
|
<CodeText code={code} />
|
52
|
58
|
</View>
|
53
|
59
|
)
|