制作一个简单的覆盖类?
我想在 GWT 中制作一些非常简单的覆盖类来包装一些 SVG 内容。我基本上想绘制一个矩形,这就是我在 javascript 中所做的:
var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svg.setAttribute('width', '100%');
svg.setAttribute('height', '100%');
document.body.appendChild(svg);
var rect = document.createElementNS('http://www.w3.org/2000/svg','rect');
rect.setAttribute("width","300");
rect.setAttribute("height","100");
svg.appendChild(rect);
现在我在将其转换为 GWT 时遇到了麻烦。我希望我可以在所有这些调用周围做一个非常薄的覆盖,就像这样:
public class SVGPanel extends JavaScriptObject {
protected SVGPanel() {}
public static native SVGPanel create(String width, String height) /*-{
var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svg.setAttribute('width', width);
svg.setAttribute('height', height);
return svg;
}-*/;
}
public MyProject implements EntryPoint {
public void onModuleLoad() {
SVGPanel panel = SVGPanel.create("100%", "100%");
Document.get().getBody().appendChild(panel);
}
}
是的,但我不知道如何从 SVG 内容的 javascript 表示跳转到 GWT java 类。首先,SVGPanel 类扩展了 JavaScriptObject,但我不能简单地将其添加到 Document 主体类,因为它需要 Element 类型。如果有人能指出建造这座桥的正确方法,我应该能够继续前进。
另外,我不确定这是否是合并一些简单 SVG 类的最佳方法,我是否应该使用 DOM 类对它们进行建模,而不是尝试使用 JSNI ?
谢谢
I'd like to make some really simple overlay classes in GWT to wrap some SVG stuff. I'd basically like to get a rectangle drawn, this is how I do it in javascript:
var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svg.setAttribute('width', '100%');
svg.setAttribute('height', '100%');
document.body.appendChild(svg);
var rect = document.createElementNS('http://www.w3.org/2000/svg','rect');
rect.setAttribute("width","300");
rect.setAttribute("height","100");
svg.appendChild(rect);
and now I'm having trouble translating that to GWT. I was hoping I could do a really thin overlay around all those calls, something like this:
public class SVGPanel extends JavaScriptObject {
protected SVGPanel() {}
public static native SVGPanel create(String width, String height) /*-{
var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svg.setAttribute('width', width);
svg.setAttribute('height', height);
return svg;
}-*/;
}
public MyProject implements EntryPoint {
public void onModuleLoad() {
SVGPanel panel = SVGPanel.create("100%", "100%");
Document.get().getBody().appendChild(panel);
}
}
yeah but I do not have a grasp on how we can jump from the javascript representation of the SVG stuff to GWT java classes. For one, the SVGPanel class extends JavaScriptObject, but I can't simply add it to the Document body class because it's expecting an Element type. If someone could just point out the right way to do that bridge I should be able to get going after that.
Also, I'm not sure if this the optimal way to incorporate some simple SVG classes, should I be modeling them using the DOM classes instead of trying to use JSNI ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要给appendChild一个Element。因此,只需让您的覆盖类扩展 Element 而不是 JavaScriptObject。
请注意,Element 是 JavaScriptObject 的子类。
You need to give an Element to appendChild. So just make your overlay class extend Element instead of JavaScriptObject.
Note that Element is a subclass of JavaScriptObject.
我认为值得一看 gwt-svg - 虽然看起来他们停止了开发2007 年左右,代码库看起来足够可靠,很好地说明了你想要做什么,而且它还有一些不错的改进,比如针对(邪恶的)IE6 的特殊实现。即使代码不能按原样工作,希望它能给您一个开始的想法(也许您甚至可以将您的工作开源,以便其他人可以从中受益)。
还有 GWTCanvas - 这可能就是您想要的正在尝试实现:) 如果没有,至少值得检查一下他们的 API,看看他们是如何处理事情的。
祝你好运!
I think it's worth checking out gwt-svg - while it looks like they stopped development around 2007, the codebase seems solid enough, nicely illustrating what you want to do, plus it has some nice touches, like special implementations for the (evil) IE6. Even if the code doesn't work they way it is, hopefully it will give you an idea were to start (and maybe you'd even release your work open-source, so that others can benefit from it).
And there's also GWTCanvas - which might be what you are trying to implement :) If not, it's worth checking out at least their API, to see how they handled things.
Good luck!