如何使用 Raphael 动态更新 svg?
我正在尝试编写一些东西来简化 SVG 路径的实验,但我有一个问题,当我可以验证 svg 代码是否已更新时,它实际上并没有更新屏幕。基本上工作流程如下:我在输入框中输入一些内容(路径字符串),然后单击“绘制”,它将清除画布,然后根据输入的路径字符串绘制形状。
经过更多测试后,我意识到这似乎是 beta chrome 中的一个错误。因为下面的代码在 Firefox 中似乎运行良好。如果有人有任何见解,将不胜感激!
Javascript:
$(function(){
var paper = Raphael($('#paper')[0], 500, 500);
$('#path').change(function(){
console.log($(this).val());
var rect = paper.rect(10, 10, 50, 50);
console.log(rect);
var path = paper.path($(this).val());
});
});
HTML:
<div id="content">
<div id="paper"></div>
<div id="pathDiv">
<input id="path" type="text" name="path" />
</div>
</div>
如果有人有任何建议,我们将不胜感激!
贾森
I'm trying to write something to simplify experimentation with SVG path, but i have a problem where it doesn't actually update the screen when i can verify that the svg code is updated. Basically the work flow is as follows: I type something into the input box (A path string), and click draw, it will clear the canvas, then draw the shape according to the typed path string.
After some more testing, i realized that it seem to be a bug in beta chrome. As the following code seem to work fine in firefox. If anyone have any insights, it would be much appreciated!
Javascript:
$(function(){
var paper = Raphael($('#paper')[0], 500, 500);
$('#path').change(function(){
console.log($(this).val());
var rect = paper.rect(10, 10, 50, 50);
console.log(rect);
var path = paper.path($(this).val());
});
});
HTML:
<div id="content">
<div id="paper"></div>
<div id="pathDiv">
<input id="path" type="text" name="path" />
</div>
</div>
If anyone have any suggestions, it would be much appreciated!
Jason
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您遇到的问题是,每次执行
onchange
函数时,您都会编写一个新路径,而您真正想要做的是更新现有路径。为此,请在事件处理函数之外声明rect
和path
,然后更新函数内的现有对象,如下所示:我已 在 Firefox 和 IE 中尝试过这个,似乎工作正常。如果这不是您的意思,请更新您的问题。
I think the problem you're experiencing is that you're writing a new path each time you execute your
onchange
function, whereas what you really want to do is update your existing path. To do this take the declarations forrect
andpath
outside of your event handler function, then update the existing objects inside the function like this:I've tried this in Firefox and IE and it seems to work fine. If this isn't what you meant please update your question.