【发布时间】:2016-07-11 05:30:38
【问题描述】:
我想将 SVG 生成为 HTMLImageElement,所以我可以将它放在 <canvas> 元素上。所以我在这里:
function makeButton(evt) {
var svgns = "http://www.w3.org/2000/svg";
var svg = document.createElementNS(svgns, "svg");
svg.setAttribute('style', 'border: 1px solid black');
svg.setAttribute('width', '59');
svg.setAttribute('height', '59');
svg.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xlink", "http://www.w3.org/1999/xlink");
var shape = document.createElementNS(svgns, "circle");
shape.setAttributeNS(null, "cx", 25);
shape.setAttributeNS(null, "cy", 25);
shape.setAttributeNS(null, "r", 20);
shape.setAttributeNS(null, "fill", "grey");
svg.appendChild(shape);
return svg;
}
然后:
var myButton = makeButton();
canvasContext.drawImage(myButton, offsetX, offsetY, width, height);
它说,myButton 不是 HTMLImageElement(它是 XML)吗?
【问题讨论】:
-
myButton 是一个 SVGSVGElement。 w3.org/TR/SVG/struct.html#InterfaceSVGSVGElement
标签: javascript xml dom canvas svg