如何在每次KineticJS事件中保存舞台?

How do I save the stage upon every KineticJS event?

本文关键字:保存 舞台 事件 KineticJS      更新时间:2023-09-26

我知道KineticJS舞台可以保存为JSON与舞台。toJSON函数。只要舞台有任何变化,我都愿意表演这个动作。例如,如果一个形状通过拖拽移动,形状的大小被改变,内部内容被动态改变,形状被动态添加,等等,等等,我想要运行toJSON函数。我真的不希望专门监听所有可能的事件,并为每个事件运行相同的代码。我更喜欢在一次调用中捕获所有事件。有什么东西可以帮我做到这一点吗?我意识到可能会影响性能。在对阶段进行任何更改时保存阶段是业务需求。幸运的是,这个功能仅限于少数用户。谢谢你。

如何在每次KineticJS事件上保存舞台?

答案:当任何KineticJS事件发生时调用stage.toJSON

是否有任何KineticJS事件处理程序监听任何和所有事件?

答案:实际上,有一个Draconian选项

你可以要求监听KineticJS可以生成的每个事件,因为.on()方法可以接受多个空格分隔的事件类型。

您将剩下的任务是根据eventType和您的设计考虑来决定是否要stage.toJSON

节点可以监听这些事件类型:

// a space-delimited list of all node events that KineticJS can listen for
var allNodeEvents="mouseover mousemove mouseout mouseenter mouseleave mousedown mouseup click dblclick touchstart touchmove touchend tap dbltap dragstart dragmove dragend";

舞台可以监听这些事件类型:

// a space-delimited list of all stage events that KineticJS can listen for
var allStageEvents="contentMouseover contentMousemove contentMouseout contentMousedown contentMouseup contentClick contentDblclick contentTouchstart contentTouchmove contentTouchend contentTap contentDblTap";

你可以像这样监听任何和所有的Node+Stage事件:

// listen for all possible Node events
node.on(allNodeEvents,function(event){
    // get the type of this event
    var eventType=event.type;
    // Do stage.toJSON 
    //(or not based on eventType and other design specific considerations
});

// listen for all possible Stage events
stage.on(allStageEvents,function(event){
    // get the type of this event
    var eventType=event.type;
    // Do stage.toJSON 
    //(or not based on eventType and other design specific considerations
});