通用:命名结构和绘制KineticJS

时间:2013-12-02 21:57:47

标签: javascript canvas air kineticjs

这个需要更多的描述,抱歉较短的标题。

我目前没有在我面前的代码,但可能会更新它的详细信息,包括它的工作原理和问题所在。

基本上我注意到,如果没有为形状/组做一个或多或少的全局名称(window.whatever),它将永远不会绘制它们。我已经完成了对象的日志,看它们是否正确并且没有看到它们的任何问题。

完整范围首先是一个层,然后传递给一个函数,然后我以逻辑方式创建形状和组,而不将组传回,而是将图层作为参数发送,以从函数中添加它。当我这样做时,似乎我永远无法将它绘制到容器(舞台)。

要添加它,我正在循环创建形状,因为我使它们变量和基于精确像素的更多百分比,所以我将对象保存在通过循环生成的数组中。

我先做了这个单位并且它有效,但是在将它移动到它停止工作的功能后,是否有任何理由我可能会丢失?

如果它是相关的另一个注释,我使用的是Adobe AIR。

如果有任何想法让我知道,我会尽可能发布实际代码(距离此帖子几个小时)。

更新:

基本上我遇到的问题是,当我将形状/组分成它们自己的功能时,它根本不想绘制它们。我试图将绘图函数内联调用,并且稍后将它们添加到舞台上,我的理解都会触发绘制。

这是代码

var forms = {
    selector:function(params,bind){

        //globals
        var parent,obj,lastWidth=0,items=[];

        //setup defaults
        var params = params || {};
        params.x = params.x || 0;
        params.y = params.y || 0;
        params.active = params.active || 0;
        params.height = params.height || 200;
        params.children = params.children || [{
            fill:'yellow'
        }];
        params.width = params.width || bind.getWidth();
        params.margins = params.margins || 5;

        //container for selector
        parent = new Kinetic.Group({
            x:params.x,
            y:params.y,
            height:params.height,
            width:params.width
        });
        air.Introspector.Console.log(params);

        var totalMargins = params.margins*(params.children.length+1),
            activeWidth = (params.width-totalMargins)/2,
            subItems = params.children.length-1,
            subWidth = activeWidth/subItems,
            itemHeight = (params.height-params.margins)/2;
        //loop all children
        for(var i=0;i<params.children.length;i++){
            //use an array to store objects
            items[i]={};
            //create default object for rectangle
            obj = {};
            obj.y = params.margins;
            obj.fill = params.children[i].fill;
            if(params.active==i){
                obj.width = activeWidth;
            }else{
                obj.width = subWidth;
            }
            obj.x = params.margins+lastWidth;
            obj.height = itemHeight;
            lastWidth = lastWidth+(params.margins+obj.width);
            //create group for text
            items[i].text = new Kinetic.Group({
                x:0,
                y:itemHeight+params.margins
            });
            //create box for text
            items[i].box = new Kinetic.Rect({
                x: params.margins,
                y: params.margins,
                width:params.width-(params.margins*2),
                height:(params.height-itemHeight)-(params.margins*2),
                fill:'yellow'
            });
            air.Introspector.Console.log(items[i].box);
            //add box to text groups
            items[i].text.add(items[i].box);
            //create item
            items[i].item = new Kinetic.Rect(obj);

            //add groups to parent
            parent
                .add(items[i].item)
                .add(items[i].text);
        }
        //add parent to the bound container
        bind.add(parent);
    }
}

2 个答案:

答案 0 :(得分:0)

从你的问题来看,我假设bind是你的Kinetic.Layer。

  1. 确保已将绑定添加到舞台:stage.add(bind);

  2. 将您的组/ rects添加到bind后,也可以执行bind.draw();

答案 1 :(得分:0)

我已经弄清楚了。

问题是在添加任何其他内容之前必须将该图层添加到舞台中。它不在我的代码中,因为我没有把它视为一个问题,因为它在其他地方工作。

基本上,如果您向图层添加任何内容,则将图层添加到舞台中将失败。它必须是这样的:

创建阶段

创建层

将舞台添加到舞台

向群体添加群组

如果需要画画

最初的方式是这样的:

创建阶段

创建层

向群体添加群组

将舞台添加到舞台

如果需要画画

这个问题将会更新以使其显而易见,这是文档中不存在的问题(据我所知),我可以将其视为令人困惑的一些。

相关问题