简体   繁体   English

如何在 React Native 中将 svg 路径放入 svg 视图框中

[英]How to fit svg path into svg viewbox in React Native

I am trying to implement barcode scanner viewFinder and I want to use svg icon to make it look nice, but I have a problem with forcing the path element inside the svg to take up the full svg width and height.我正在尝试实现条形码扫描仪取景器,并且我想使用 svg 图标使其看起来不错,但是我在强制 svg 内的路径元素占据完整的 svg 宽度和高度时遇到问题。 I am using react native and to generate icon i use SVGR https://react-svgr.com/playground/?native=true&typescript=true in the scan handler I set the dimensions of the svg like so:我正在使用本机反应并生成图标,我在扫描处理程序中使用 SVGR https://react-svgr.com/playground/?native=true&typescript=true我设置了 svg 的尺寸,如下所示:

  const handleBarCodeScanned = ({ type, data, bounds }: BarCodeEvent) => {
    if (!bounds) return;
    const { origin, size } = bounds;
    setX(origin.x);
    setY(origin.y);
    setWidth(size.width);
    setHeight(size.height);
  };

and the I ise them inside the svg which looks like so我把它们放在 svg 里面,看起来像这样

import * as React from "react";
import Svg, { SvgProps, Path } from "react-native-svg";

export const ViewFinder = (props: SvgProps & { top: number; left: number }) => {
  const { width, height, top, left } = props;
  return (
    <Svg
      width={width}
      height={height}
      style={{
        borderColor: "green",
        borderWidth: 2,
        position: "absolute",
        left: 0,
        top: 0,
        width: "100%",
        height: "100%",
      }}
      fill="none"
      stroke="green"
      preserveAspectRatio="none"
      viewBox={`0 0 ${width} ${height}`}
    >
      <Path d="M6.13 1L6 16a2 2 0 0 0 2 2h15"></Path>
      <Path d="M1 6.13L16 6a2 2 0 0 1 2 2v15"></Path>
    </Svg>
  );
};

original icon is a featerIcons crop icon https://feathericons.com/ and the original code of the icon is:原始图标是一个featerIcons crop icon https://feathericons.com/图标的原始代码是:

<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-crop"><path d="M6.13 1L6 16a2 2 0 0 0 2 2h15"></path><path d="M1 6.13L16 6a2 2 0 0 1 2 2v15"></path></svg>

as you can see I set the border color and borderWidth on the svg itself, and it scales to fit the container so here everything seems to be ok.如您所见,我在 svg 本身上设置了边框颜色和边框宽度,并且它可以缩放以适合容器,所以这里一切似乎都很好。 I have viewBox and preserveAspectRatio set up its just the inner path not scaling with the svg, and it is not just this icon I have tries several and the issue is still this same so there must be something wrong with my understanding of svg.我有 viewBox 和 preserveAspectRatio 设置了它的内部路径,不使用 svg 进行缩放,而且不仅仅是这个图标,我尝试了几次,问题仍然相同,所以我对 svg 的理解一定有问题。

Thanks a lot for any help.非常感谢您的帮助。

Normally a viewBox would be 4 fixed numbers, ie unrelated to width and height.通常一个viewBox 是4 个固定数字,即与宽度和高度无关。 That should give you the result you want.那应该会给您想要的结果。

Your content doesn't change in size so your viewBox shouldn't change either.您的内容的大小不会改变,因此您的 viewBox 也不应该改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM