简体   繁体   English

使用 SVG 在 CSS 中制作自定义图标

[英]Making custom icons in CSS using SVG

I am trying to use my own icons in a css file to display icons on a react website.我正在尝试在 css 文件中使用我自己的图标在 React 网站上显示图标。 Below is the code that leads up to the point where I actually create the markers (I am using MapBox API. (See bottom of code)下面是导致我实际创建标记的代码(我使用的是 MapBox API。(见代码底部)

The problem is that a.) when i create my own SVG files in illustrator and either put them in the project directory or make a link for them for the file to reference, neither appear in my map.问题是 a.) 当我在 illustrator 中创建自己的 SVG 文件并将它们放在项目目录中或为它们创建链接以供文件参考时,它们都不会出现在我的地图中。 b.) I found someone else's icon and used that, and it can render that icon, but it is way too big. b.) 我找到了别人的图标并使用了它,它可以渲染那个图标,但它太大了。

import React from "react";
import mapboxgl, { LngLat } from "mapbox-gl";
import "./GoogleMap.css";

import db from "../../firebaseConfig";
import { Marker } from "react-simple-maps";
var image01 = require("../../assets/droneIcon.png");
var firebase = require("firebase/app");
require("firebase/auth");
require("firebase/database");

const auth = require("../../auth.json");

mapboxgl.accessToken =
  "pk.eyJ1IjoiamVycnkyMDE4IiwiYSI6ImNrNjA1N3d5NTA0Zmwza252OXNpcWpzYWEifQ.lBwSxATvYgUqiyGfIvC3tw";

class GoogleMap extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      lng: 50,
      lat: 50,
      zoom: 10
    };
  }

  componentDidMount = () => {
    const { lng, lat, zoom } = this.state;

    var droneList = [];

    const map = new mapboxgl.Map({
      container: this.mapContainer,
      style: "mapbox://styles/mapbox/streets-v9",
      center: [lng, lat],
      zoom: zoom
    });

    map.on("move", () => {
      const { lng, lat } = map.getCenter();

      this.setState({
        lng: lng.toFixed(4),
        lat: lat.toFixed(4),
        zoom: map.getZoom().toFixed(2)
      });
    });

    var el = document.createElement("div");
    el.className = 'marker';
    el.style.backgroundImage = 'url(https://cdn1.iconfinder.com/data/icons/appliance-1/100/Drone-07-512.png)';
    el.style.width = "500px";
    el.style.height = "500px";

I have tried creating my own SVG, making a raw git file for it to host and reference it, however I can't find anywhere to make that minified raw SVG file into a readable image for the javascript code.我曾尝试创建自己的 SVG,为它制作一个原始 git 文件来托管和引用它,但是我找不到任何地方可以将缩小的原始 SVG 文件制作成 javascript 代码的可读图像。 I have found that SVG files that are created by others and posted can be placed into the map api and successfully display.我发现别人创建并发布的SVG文件可以放入地图api并成功显示。

I need to either 1. Find a way to display SVG images, since the current methods I have been trying are resulting in blank images, or just the alpha channel of the image我需要 1. 找到一种显示 SVG 图像的方法,因为我一直在尝试的当前方法导致空白图像,或者只是图像的 alpha 通道

  1. Find a way to simply resize the icon that does render, so that I can keep it at the necessary pixel dimensions without having it be too large for my application.找到一种方法来简单地调整渲染的图标的大小,这样我就可以将它保持在必要的像素尺寸,而不会让它对于我的应用程序来说太大。

Any help is very much appreciated,很感谢任何形式的帮助,

Thank you.谢谢你。

I need an SVG file for an icon in my code.我的代码中的图标需要一个 SVG 文件。 There are two options for the program to reference that SVG file;程序有两个选项可以引用该 SVG 文件; either it is stored locally in the program's directory, or it simply looks at a url for it.它要么本地存储在程序的目录中,要么只是查看它的 url。

When referencing an SVG in CSS, you can use an inline Data URI .在 CSS 中引用 SVG 时,您可以使用内联数据 URI

Instead of the protocol:而不是协议:

https://

you'll need to begin your Data URI with MIME Type and encoding:您需要使用 MIME 类型和编码开始您的数据 URI:

data:image/svg+xml;utf8,

But after that, you can add the SVG without any changes:但在那之后,您可以不加任何更改地添加 SVG:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 120 120"><path d="M84.4 93.8V70.6h7.7v30.9H22.6V70.6h7.7v23.2z" fill="rgb(188, 187, 187)" /><path d="M38.8 68.4l37.8 7.9 1.6-7.6-37.8-7.9-1.6 7.6zm5-18l35 16.3 3.2-7-35-16.4-3.2 7.1zm9.7-17.2l29.7 24.7 4.9-5.9-29.7-24.7-4.9 5.9zm19.2-18.3l-6.2 4.6 23 31 6.2-4.6-23-31zM38 86h38.6v-7.7H38V86z" fill="rgb(244, 128, 35)" /></svg>

Working Example:工作示例:

 div { width: 180px; height: 180px; background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 120 120"><path d="M84.4 93.8V70.6h7.7v30.9H22.6V70.6h7.7v23.2z" fill="rgb(188, 187, 187)" /><path d="M38.8 68.4l37.8 7.9 1.6-7.6-37.8-7.9-1.6 7.6zm5-18l35 16.3 3.2-7-35-16.4-3.2 7.1zm9.7-17.2l29.7 24.7 4.9-5.9-29.7-24.7-4.9 5.9zm19.2-18.3l-6.2 4.6 23 31 6.2-4.6-23-31zM38 86h38.6v-7.7H38V86z" fill="rgb(244, 128, 35)" /></svg>'); }
 <div></div>

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

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