[英]Kinetic.js Load Issues
I'm trying to load a saved stage from a database and when I load it, it looks fine, but after loading it I cannot draw on the stage. 我正在尝试从数据库加载一个已保存的阶段,当我加载它时,它看起来很好,但加载后我无法在舞台上绘制。 I'm aware that I might not be able to use the loaded objects like I could before saving, but how can I draw onto the stage after loading?
我知道在保存之前我可能无法使用加载的对象,但是如何在加载后绘制到舞台上?
I am using Kinetic.Node.create(json, 'container');
我正在使用
Kinetic.Node.create(json, 'container');
but afterwards I can no longer add to the stage with layer.draw();
但之后我再也无法使用
layer.draw();
添加到舞台上layer.draw();
. 。
Does anyone have any suggestions? 有没有人有什么建议? And what is a good way to re-bind event handlers.
什么是重新绑定事件处理程序的好方法。 Are there any good tutorials?
有没有好的教程? Thank you!
谢谢!
EDIT: 编辑:
Here is my code for creating and editing the canvas. 这是我创建和编辑画布的代码。
var stage = new Kinetic.Stage({
container: 'container',
width:screen.width*.98,
height: screen.height*.70,
id:'stage',
name: 'stage'
});
var layer = new Kinetic.Layer();
layer.on('mousedown',function(e){
var node = e.targetNode;
//select(node);
});
//load
var json= load();
stage = Kinetic.Node.create(json, 'container');
Q1: Re-instantiating a Kinetic.Stage. Q1:重新实例化Kinetic.Stage。
Your Kinetic.Node.create
command is the correct command to re-instantiate a stage. 您的
Kinetic.Node.create
命令是重新实例化阶段的正确命令。 Further diagnosis was not possible since you provided no code. 由于您未提供代码,因此无法进一步诊断。
var stage = Kinetic.Node.create(yourJSON, 'container');
stage.draw();
Q2: Re-binding event handlers. Q2:重新绑定事件处理程序。
One good way of rewiring event handlers is to provide a .js file along with your .json file. 重新布线事件处理程序的一个好方法是提供.js文件和.json文件。
For example: 例如:
When creating your nodes/containers, add properties to indicate which event handler(s) need to be rewired: 创建节点/容器时,添加属性以指示需要重新连接的事件处理程序:
var circle1 = new Kinetic.Circle({
x:100,
y:100,
radius: 30,
fill: 'red',
// this circle should be rewired with myClickHandler1
clickEvent:"myClickHandler1"
});
Put all your event handlers in a .js file that's provided along with your .json file: 将所有事件处理程序放在与.json文件一起提供的.js文件中:
var eventHandlers={
myClickHandler1:function(e){alert("Fired clickHandler1");},
}
And then you can rewire the event handlers from the .js file like this: 然后你可以从.js文件重新连接事件处理程序,如下所示:
function rewireHandlers(node){
var handler;
// rewire click handler
handler=node.getAttr("clickEvent");
if(handler && eventHandlers[handler]){
node.on("click",eventHandlers[handler])
}
// rewire other event handlers the same way
}
// rewire all nodes and containers
stage.getChildren().each(function(node){
node.rewireHandlers(node);
});
首先在你的json字符串中你需要将所有“className”:“Kinetic.Group”替换为“className”:“Group”然后尝试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.