|
|
@@ -60,8 +60,12 @@ export const BaseChart: React.FC<BaseChartProps> = (props) => {
|
|
|
*/
|
|
|
const { cWidth, cHeight, actualPixelRatio } = useMemo(() => {
|
|
|
const sysInfo = Taro.getSystemInfoSync();
|
|
|
- // 支付宝小程序需要使用实际的 pixelRatio,其他平台使用 1
|
|
|
- const pr = pixelRatio ?? (Taro.getEnv() === Taro.ENV_TYPE.ALIPAY ? sysInfo.pixelRatio : 1);
|
|
|
+ console.debug('sysInfo.pixelRatio', sysInfo.pixelRatio)
|
|
|
+ // Canvas 2D 需要使用实际的 pixelRatio 来匹配设备像素
|
|
|
+ // 这样绘制的内容才不会模糊或被放大
|
|
|
+ const pr = pixelRatio ?? sysInfo.pixelRatio;
|
|
|
+ // width 和 height 是逻辑像素(CSS 像素)
|
|
|
+ // Canvas 2D 的实际像素尺寸 = 逻辑像素 * pixelRatio
|
|
|
const cw = width ?? pr * sysInfo.windowWidth;
|
|
|
const ch = height ?? (500 / 750 * cw);
|
|
|
return { cWidth: cw, cHeight: ch, actualPixelRatio: pr };
|
|
|
@@ -71,6 +75,15 @@ export const BaseChart: React.FC<BaseChartProps> = (props) => {
|
|
|
* Canvas props 根据 platform 动态计算
|
|
|
*/
|
|
|
const canvasProps = useMemo(() => {
|
|
|
+ console.debug('[BaseChart] Canvas 尺寸配置:', {
|
|
|
+ inputWidth: width,
|
|
|
+ inputHeight: height,
|
|
|
+ cWidth,
|
|
|
+ cHeight,
|
|
|
+ actualPixelRatio,
|
|
|
+ platform: Taro.getEnv()
|
|
|
+ });
|
|
|
+
|
|
|
if (Taro.getEnv() === Taro.ENV_TYPE.ALIPAY) {
|
|
|
return {
|
|
|
width: String(cWidth),
|
|
|
@@ -78,10 +91,15 @@ export const BaseChart: React.FC<BaseChartProps> = (props) => {
|
|
|
style: { width: '100%', height: '100%' }
|
|
|
};
|
|
|
}
|
|
|
+ // Canvas 2D API:
|
|
|
+ // - width/height 属性:实际像素尺寸(逻辑像素 * pixelRatio),需要转为字符串
|
|
|
+ // - style.width/style.height:CSS 显示尺寸(逻辑像素)
|
|
|
return {
|
|
|
+ width: String(cWidth * actualPixelRatio),
|
|
|
+ height: String(cHeight * actualPixelRatio),
|
|
|
style: { width: `${cWidth}px`, height: `${cHeight}px` }
|
|
|
};
|
|
|
- }, [cWidth, cHeight]);
|
|
|
+ }, [cWidth, cHeight, actualPixelRatio]);
|
|
|
|
|
|
/**
|
|
|
* 初始化图表实例
|