Skip to content

Commit

Permalink
chore: replace dpr to constant
Browse files Browse the repository at this point in the history
  • Loading branch information
Wangtaofeng committed Mar 6, 2024
1 parent 2b9887f commit 1f81010
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DPR = window.devicePixelRatio
16 changes: 6 additions & 10 deletions apps/builder/src/widgetLibrary/SignatureWidget/signatureCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "react"
import useMeasure from "react-use-measure"
import { RefreshIcon, getSpecialThemeColor } from "@illa-design/react"
import { DPR } from "./constants"
import { ICustomRef, WrappedSignatureProps } from "./interface"
import {
canvasContainerStyle,
Expand Down Expand Up @@ -47,7 +48,6 @@ const SignatureCanvas = forwardRef<ICustomRef, SignatureCanvasProps>(
const ctx = useRef<CanvasRenderingContext2D | null>()
const startX = useRef(0)
const startY = useRef(0)
const [dpr, setDpr] = useState(1)

useImperativeHandle(ref, () => ({
clear: () => handleReset(),
Expand All @@ -72,7 +72,7 @@ const SignatureCanvas = forwardRef<ICustomRef, SignatureCanvasProps>(
touch.clientX - touchTarget.getBoundingClientRect().left
startY.current = touch.clientY - touchTarget.getBoundingClientRect().top
}
ctx.current.lineWidth = 2 * dpr
ctx.current.lineWidth = 2 * DPR
ctx.current.lineCap = "round"
ctx.current.strokeStyle = getSpecialThemeColor(penColor)
ctx.current.beginPath()
Expand All @@ -94,8 +94,8 @@ const SignatureCanvas = forwardRef<ICustomRef, SignatureCanvasProps>(
offsetX = touch.clientX - touchTarget.getBoundingClientRect().left
offsetY = touch.clientY - touchTarget.getBoundingClientRect().top
}
currentCtx.moveTo(startX.current * dpr, startY.current * dpr)
currentCtx.lineTo(offsetX * dpr, offsetY * dpr)
currentCtx.moveTo(startX.current * DPR, startY.current * DPR)
currentCtx.lineTo(offsetX * DPR, offsetY * DPR)
startX.current = offsetX
startY.current = offsetY
currentCtx.stroke()
Expand Down Expand Up @@ -135,10 +135,6 @@ const SignatureCanvas = forwardRef<ICustomRef, SignatureCanvasProps>(
}
}, [])

useEffect(() => {
setDpr(window.devicePixelRatio)
}, [])

return (
<div css={canvasContainerStyle(disabled)} ref={boundsRef}>
{!value && !isDrawing && (
Expand All @@ -152,8 +148,8 @@ const SignatureCanvas = forwardRef<ICustomRef, SignatureCanvasProps>(
<canvas
ref={canvasRef}
css={canvasStyle}
width={dpr * bound.width}
height={dpr * bound.height}
width={DPR * bound.width}
height={DPR * bound.height}
onMouseDownCapture={handleStartDraw}
onMouseMoveCapture={handleDraw}
onMouseUp={handleEndDraw}
Expand Down

0 comments on commit 1f81010

Please sign in to comment.