计算文字宽度
export const getTextWidth = (text: string, font: string): number => {
const canvas = document.createElement('canvas')
const context = canvas.getContext('2d')
context.font = font
const metrics = context.measureText(text)
return metrics.width
}
const benchmarkWidth = 542;
this.benchmarkRef = React.createRef();
this.myRef = React.createRef();
const benchmarkRatio =
this.benchmarkRef.current ? benchmarkWidth / this.benchmarkRef.current.offsetWidth : "";
const width =
benchmarkRatio && this.myRef.current ? this.myRef.current.offsetWidth * benchmarkRatio : "";