简体   繁体   English

为什么我不能从图像地图制作SVG?

[英]Why can't I make an SVG from an image map?

I have an img tag as the sole child of a <div id="container"> tag. 我有一个img标记作为<div id="container">标记的唯一子代。 width , height , and usemap are all set on the image, as are style="position:absolute;" widthheightusemap都设置在图像上,就像style="position:absolute;" and usemap="#amap" . usemap="#amap"

My quest: to write Javascript that adds an SVG overlay to the image showing the areas of the imagemap, preserving title tooltips. 我的追求:编写Javascript,将SVG叠加层添加到显示图像映射区域的图像上,同时保留title工具提示。 To that end, I have written the following code in a script element in the head : 为此,我在headscript元素中编写了以下代码:

function makeCircle(area)
{
  var c = document.createElement("circle");
  var coords = area.coords.split(",");

  c.setAttribute("cx", coords[0]);
  c.setAttribute("cy", coords[1]);
  c.setAttribute("r", coords[2]);

  c.className = "svg_shape";

  var t = document.createElement("title");
  t.innerHTML = area.title;
  c.appendChild(t);

  return c;
}

function makeRect(area)
{
  var r = document.createElement("rect");
  var coords = area.coords.split(",");

  r.setAttribute("x", coords[0]);
  r.setAttribute("y", coords[1]);

  r.setAttribute("width", coords[2] - coords[0]);
  r.setAttribute("height", coords[3] - coords[1]);

  r.className = "svg_shape";

  var t = document.createElement("title");
  t.innerHTML = area.title;
  r.appendChild(t);

  return r;
}

function makePoly(area)
{
  var p = document.createElement("polygon");
  var coords = area.coords.split(",");

  var coords2 = [];
  coords2.push(coords[0]);
  for(var i = 1; i < coords.length; i++)
  {
    if(i % 2 === 0)
      coords2.push(" ");
    else
      coords2.push(",");

    coords2.push(coords[i]);
  }

  p.setAttribute("points", coords2.join(""));

  p.className = "svg_shape";

  var t = document.createElement("title");
  t.innerHTML = area.title;
  p.appendChild(t);

  return p;
}

function makeSVG()
{
  var container = document.getElementById("container");
  var image = container.firstElementChild;
  var map = document.getElementsByName("amap")[0];
  var svg = document.createElement("svg");

  container.appendChild(svg);

  svg.setAttribute("width", image.width);
  svg.setAttribute("height", image.height);
  svg.style.position="absolute";
  //svg.style.width=image.width;
  //svg.style.height=image.height;

  var children = map.children;
  for(var i = 0; i < children.length; i++)
  {
    var area = children[i];
    var type = area.shape;
    if(type==="circle")
    {
      svg.appendChild(makeCircle(area));
    }
    else if(type==="rect")
    {
      svg.appendChild(makeRect(area));
    }
    else if(type==="poly")
    {
      svg.appendChild(makePoly(area));
    }
  }
}

window.onload = makeSVG;

svg_shape is a CSS class that basically just makes the shape visible. svg_shape是一个CSS类,基本上只是使形状可见。

Here's the thing: Even though Chrome's DevTools shows that the svg element is being generated with all the appropriate children (copying the body HTML it generates into a new static page produces the right results), it doesn't appear on the page. 事情是这样的:即使Chrome的DevTools显示svg元素是由所有适当的子元素生成的(将其生成的主体HTML复制到新的静态页面中也会产生正确的结果),但它不会出现在页面上。 Further inspection shows that the SVG has dimensions 0×0, even though it has nonzero width and height attributes. 进一步检查显示,SVG的尺寸为0×0,即使它的widthheight属性不为零。 What gives? 是什么赋予了?

You need to use createElementNS to create SVG elements. 您需要使用createElementNS创建SVG元素。

For classNames it's 对于classNames

r.className.baseVal = "svg_shape"

to allow for SMIL in SVG, animated and base (non-animated) values are separate. 为了允许在SVG中使用SMIL,动画值和基本(非动画)值是分开的。

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

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