kineticjs,应该是stage.setWidth(),stage.setHeight()和stage.draw();调整一切?

时间:2014-03-18 09:24:48

标签: resize kineticjs

我试图在窗口调整大小时调整所有内容。 我有这个功能,但似乎只调整了一层(graphLayer.add(drawGraph);) 我认为下面的代码应该调整一切吗?

function onResize(){ 
        var widowWidth = (window.innerWidth) -yPadding; // width - the padding
        var widowHeight = (window.innerHeight) -xPadding;  // Height - the padding
        stage.setWidth((window.innerWidth) -yPadding);
        stage.setHeight((window.innerHeight) -xPadding);
        stage.draw();
    }

这是我的代码的基础

$(window).resize(onResize);
    var graph;
    var graphLayer = new Kinetic.Layer();
    var BubbleLayer = new Kinetic.Layer();
    var tooltipLayer = new Kinetic.Layer();
    var widowWidth = (window.innerWidth) -yPadding; // width - the padding
    var widowHeight = (window.innerHeight) -xPadding;  // Height - the padding


    var stage = new Kinetic.Stage({
      container: 'graph',
      width: widowWidth,  // width - the padding
      height: widowHeight,  // Height - the padding
    });

    var tooltip = new Kinetic.Label({
      opacity: 0.75,
      visible: false,
      listening: false
    });

    tooltip.add(new Kinetic.Tag({
      .........
    }));

    tooltip.add(new Kinetic.Text({
      .........
    }));

    var drawGraph = new Kinetic.Shape ({
      sceneFunc: function(ctx){
          .........
        }
        ctx.fillStrokeShape(this);
      },
      stroke: 'black',
      strokeWidth: 1
    });     

    var drawGraphquarter = new Kinetic.Shape ({
      sceneFunc: function(ctx){
        .........
        }
      },
      stroke: 'red',
      strokeWidth: 3
    });     

    // build data

    $.getJSON( "bubble_data.json", function( data ) {

        $.each( data.projects, function(i) {
            var bubbleData = [];

            .........

            bubbleData.push({
              .........
            });

            .........

            for(var n = 0; n < bubbleData.length; n++) {
              addBubble(bubbleData[n], BubbleLayer);
              stage.add(BubbleLayer);
            }

        });

        graphLayer.add(drawGraph);
        graphLayer.add(drawGraphquarter);
        stage.add(BubbleLayer);
        stage.add(graphLayer); 
        graphLayer.moveToBottom(); 
        tooltipLayer.add(tooltip);
        stage.add(tooltipLayer);
        });
    }); 

    // add bubles to layer 
    function addBubble(obj, BubbleLayer) {
      var bubble = new Kinetic.Shape({
        sceneFunc: function(ctx) {
          .........
      });
      BubbleLayer.add(bubble);
    } 

    // calendar quarter
    function getQuarter(d) {
      .........
    }

     function onResize(){ 
        var widowWidth = (window.innerWidth) -yPadding; // width - the padding
        var widowHeight = (window.innerHeight) -xPadding;  // Height - the padding
        stage.setWidth((window.innerWidth) -yPadding);
        stage.setHeight((window.innerHeight) -xPadding);
        stage.draw();
    }

1 个答案:

答案 0 :(得分:0)

将我的代码更新为此。

var graph;
    var xPadding = 10;
    var yPadding = 10;
    var graphLayer = new Kinetic.Layer();
    var graphLayerQuater = new Kinetic.Layer();
    var BubbleLayer = new Kinetic.Layer();
    var tooltipLayer = new Kinetic.Layer();
    var widowWidth =  (window.innerWidth) -yPadding; // width - the padding
    var widowHeight = (window.innerHeight) -xPadding;  // Height - the padding
    var stage = new Kinetic.Stage({
      container: 'graph',
      width: widowWidth,  
      height: widowHeight,  
    });

    var initialScale = stage.scale(); //returns {x: 1, y: 1} 
    var initialWidth = (window.innerWidth) -yPadding; // width - the padding
    var initialHeight = (window.innerHeight) -xPadding;  // Height - the padding

onresize功能

window.onresize = function(event) { 
      var width = (window.innerWidth) -yPadding;
      var height =(window.innerHeight) -xPadding; 
      var xScale =  (width  / initialWidth) * initialScale.x; 
      var yScale = (height / initialHeight) * initialScale.y;
      var newScale = {x: xScale, y: yScale};
      stage.setAttr('width', width);
      stage.setAttr('height', height);    
      stage.setAttr('scale', newScale ); 
      stage.draw();
    }