简体   繁体   English

Dart从画布上的SVG文件中绘制id

[英]Dart draw id from SVG file on canvas

I'd like to load an svg, extract a group by the id, and draw it to a canvas. 我想加载一个svg,通过id提取一个组,然后将它绘制到画布上。

If the file was an image I would do: 如果文件是图像,我会这样做:

_image = new html.ImageElement()
        ..src = src;
_canvas.context2D.drawImageScaledFromSource(_image, ox, oy, ow, oh, x, y, w, h)

I'd like to do the same thing but with an svg file, and I'd like to pick and choose which id from the svg file to draw. 我想做同样的事情但是使用svg文件,我想从svg文件中选择要绘制的id。 With multiple ids draw to different parts of the canvas. 使用多个ID绘制到画布的不同部分。

Now I have: 我现在有:

var groupToDraw;
HttpRequest.getString("img.svg").then((text) {
    svg.SvgElement pimage = new svg.SvgElement(text);
    groupToDraw = pimage.querySelector("#ID");
}

But I'm not sure how to draw just this element on the screen. 但我不知道如何在屏幕上绘制这个元素。 Also some of the groups reference patterns in other parts of the file, although I could skip them for now. 另外一些组参考文件其他部分的模式,虽然我现在可以跳过它们。

If it helps to do this in pyQt I did: 如果它有助于在pyQt中执行此操作,我做了:

renderer = QSvgRenderer(path)
pix = QtGui.QPixmap(width, height)
pix.fill(Qt.transparent)
painter = QtGui.QPainter(pix)
renderer.render(painter, id)

If you load some svg text from file and create an SvgElement from that string, you will be able to see the ID on the element: 如果从文件加载一些svg文本并从该字符串创建一个SvgElement,您将能够在该元素上看到ID:

var fileID;
HttpRequest.getString('file.svg').then((text) {
  svg.SvgElement temp = new svg.SvgElement.svg(text);
  fileID = temp.id;
});  

You can then create a group element by querying the group's id, surround the GElement with SVG tags in a string, create a blob from that string and a URL from that blob, then create an ImageElement from the URL and draw the image on the canvas: 然后,您可以通过查询组的ID创建组元素,使用字符串中的SVG标记包围GElement,从该字符串创建blob以及从该blob创建URL,然后从URL创建ImageElement并在画布上绘制图像:

svg.GElement g = temp.querySelector("#id2");

var str = '<svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" width="32px"   
  height="32px" viewBox="0 0 32 32">${g.innerHtml}</svg>';

var blob = new Blob([str], "image/svg+xml;charset=utf-8");
var url = Url.createObjectUrlFromBlob(blob);

img = new ImageElement(src: url)
    ..onLoad.listen((Event e) { ctx.drawImage(img, 0, 0); });

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

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