简体   繁体   English

无法从 SVG 图像元素加载图像

[英]Can't load image from SVG image element

I am trying to load a image as the svg background, but it only shows a broken image, I think the path is correct(the js and the image files are under the same directory) and I tried adding 'data:image/png;base64,' at the front it still didn't work, I also tried localhost, it didn't work neither.我试图加载一个图像作为 svg 背景,但它只显示一个损坏的图像,我认为路径是正确的(js 和图像文件在同一目录下),我尝试添加'data:image/png;base64,'在前面它仍然不起作用,我也尝试过localhost,它也不起作用。

  svg
  .append("image")
  .attr("width", "1352px")
  .attr("height", "721px")
  .attr("href", "img.png");

But I can load the image from <img> tag.但是我可以从<img>标签加载图像。 I change to canvas to display the image, and image still cannot be displayed.However, the application can display online image.我改成canvas来显示图片,还是不能显示图片,但是应用可以在线显示图片。

 let img = new Image();
img.onload = function() {
  context.drawImage(img, 0, 0);
};
img.src =
  "img.png";

I am running the application on Chromium and using Parcel bundle.我在 Chromium 上运行应用程序并使用 Parcel 包。

好的,我想问题是我使用的是 Parcel,所以我需要导入图像文件。

import img from "./img.png";

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

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