【问题标题】:Add image to SVG element in dart [duplicate]将图像添加到 dart 中的 SVG 元素 [重复]
【发布时间】:2014-01-12 15:55:59
【问题描述】:

我正在尝试将(可互换的)图像显示为椭圆的背景或稍后我将处理的其他各种事物。 问题是我找不到如何正确加载图像。

我的代码:

DivElement div = querySelector("#mainDiv");
svg.SvgElement svgElement = new svg.SvgSvgElement();
div.append(svgElement);
//div.setAttribute("background-color", "yellow");

svg.RectElement rec = new svg.RectElement();
rec.setAttribute("x", "0");
rec.setAttribute("y", "0");
rec.setAttribute("width",div.clientWidth.toString());
rec.setAttribute("height", div.clientHeight.toString());
//svgElement.children.add(rec);  

Ellipse ell = new Ellipse() //my class. shows and allow to move it
..cx = 100
..cy= 100
..rx= 50
..ry= 50;
svgElement.children.add(ell.ellipse);
ImageElement image = new ImageElement(src: "2.jpg");
image.onLoad.listen((e) {
  svgElement.children.add(image);
});

如您所见,有一个 Rectangle,这是我第一次尝试显示具有各种属性(背景图像、背景..)的图像,然后我想在 onLoad 之后直接添加 ImageElement。都失败了。

我的下一步应该是尝试使用模式,但我不确定是否可以在 Dart 中翻译我为 javascript 阅读的内容。

编辑:如果能够加载图像也很好,这样我就可以读取尺寸等属性。 Edit2:由于我无法添加答案,这是我通过检查“重复”原始代码制作的代码。

import 'dart:svg' as svg;
//...
DivElement div = querySelector("#mainDiv");
svg.SvgElement svgElement = new svg.SvgSvgElement();
div.append(svgElement);

Ellipse ell = new Ellipse()
..cx = 100
..cy= 100
..rx= 50
..ry= 50;

svg.ImageElement image = new svg.ImageElement();

svgElement.children.add(image);
image.setAttribute('x', '0');
image.setAttribute('y', '0');
image.setAttribute('width', '100%');
image.setAttribute('height', '100%');
image.getNamespacedAttributes('http://www.w3.org/1999/xlink')['href'] = '2.jpg';

svgElement.children.add(ell.ellipse);

【问题讨论】:

  • 得到了副本,但没有说明如何将其作为元素加载,例如获取宽度和高度。
  • 如果重复的问题解决了您的部分问题,则编辑您的问题以仅显示您当前的问题,即加载元素。

标签: dart dart-html


【解决方案1】:

我没有尝试过,但我认为onLoad 事件在元素未添加到 DOM 之前不会触发。 您应该在创建元素后添加图像。 您也可以设置 display: none 并在 onLoad 触发时更改为 display: auto

【讨论】:

  • 我注意到了一个大错误(我认为)。我正在使用 ImageElement 而不是 svg.ImageElement (dart:svg)
猜你喜欢
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 2018-01-29
  • 1970-01-01
  • 1970-01-01
  • 2015-04-14
  • 2012-05-07
  • 2013-11-24
相关资源
最近更新 更多