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