reactjs - 无法在 React 中缩放 SVG
问题描述
我在 React 功能组件中有一个 SVG:
import React from 'react';
function mySVG({ colour, width, height, scale }) {
const scaleFactor = 'scale:(' + { scale } + ')';
return (
<svg width={width} height={height}>
>
<g transform={scaleFactor}>
<path d="M150 0 L75 200 L225 200 Z"
fill={colour}
stroke={colour}
strokeLinecap="round"
strokeLinejoin="round"
/>
</g>
</svg>
);
}
export default mySVG;
这怎么不是根据 scale 道具缩放的?
解决方案
问题出在您的语法中,应该没有:
in'scale(' + { scale } + ')';
import React from 'react';
function mySVG({ colour, width, height, scale }) {
const scaleFactor = 'scale(' + { scale } + ')';
return (
<svg width={width} height={height}>
<g transform={scaleFactor}>
<path d="M150 0 L75 200 L225 200 Z"
fill={colour}
stroke={colour}
strokeLinecap="round"
strokeLinejoin="round"
/>
</g>
</svg>
);
}
export default mySVG;
我为它添加了代码沙盒: https ://codesandbox.io/s/winter-architecture-uw45f
推荐阅读
- java - java的SecureRandom类中getSeed()和generateSeed()函数的区别?
- javascript - 无法更改 Electron 中的默认 TTS 语音
- go - 将多个“参数”映射到单个可变参数的成语
- javascript - 如何在 VueJs 中显示我的 url 参数的详细信息?
- javascript - 如何修复else语句未激活
- javascript - 如何从 NodeJs http 代理读取响应正文
- php - Codeigniter - 当您提交的 URI 包含不允许的字符时重定向
- multithreading - 调试器扩展发送线程事件无法更新线程 UI
- javascript - 选择使用特定字体的字符串
- javascript - 是否可以用曲线(S曲线)做范围条形图