如何使KineticJS画布舞台响应

时间:2014-02-06 02:03:27

标签: html5 canvas html5-canvas kineticjs

在向页面添加canvas元素时,我希望画布及其内容在浏览器窗口缩小时缩小。

在这里似乎有几个问题,每一个问题,我都找不到我想要的答案。我似乎有一些有用的东西,并且认为我会发布它给其他人拍摄或改进。

在这个例子中,我有一个最大宽度为1000px宽的页面。 canvas元素的最大宽度仅为800px宽。

HTML:

<div id="container"></div>

CSS:

#container {
    background-color: #888888;
    display: inline-block;
    max-width: 1000px;
}

JavaScript的:

var maxStageWidth = 800;
var maxStageHeight = 500;
var maxPageWidth = 1000;

// Check to see if window is less than desired width and calls sizing functions
function setStageWidth() {
    if (window.innerWidth < maxPageWidth) {

        resizeStage();

    } else {

        maxStageSize();

    };
};

// Sets scale and dimensions of stage in relation to window size
function resizeStage() {

    var scalePercentage = window.innerWidth / maxPageWidth;

    stage.setAttr('scaleX', scalePercentage);
    stage.setAttr('scaleY', scalePercentage);
    stage.setAttr('width', maxStageWidth * scalePercentage);
    stage.setAttr('height', maxStageHeight * scalePercentage);
    stage.draw();
};

//Sets scale and dimensions of stage to max settings
function maxStageSize() {
    stage.setAttr('scaleX', 1);
    stage.setAttr('scaleY', 1);
    stage.setAttr('width', maxStageWidth);
    stage.setAttr('height', maxStageHeight);
    stage.draw();
};

var stage = new Kinetic.Stage({
    container: 'container',
    width: maxStageWidth,
    height: maxStageHeight,
    scaleX: 1
});

var circles = new Kinetic.Layer();

var circleRed = new Kinetic.Circle({
    x: stage.getWidth() / 2,
    y: stage.getHeight() / 2,
    radius: 100,
    stroke: 'black',
    strokeWidth: 2,
    fill: 'red'
});

var circleBlue = new Kinetic.Circle({
    x: stage.getWidth() / 2 + 120,
    y: stage.getHeight() / 2 + 175,
    radius: 50,
    stroke: 'black',
    strokeWidth: 2,
    fill: 'blue'
});

var circleOrange = new Kinetic.Circle({
    x: stage.getWidth() / 2 - 175,
    y: stage.getHeight() / 2 + 100,
    radius: 75,
    stroke: 'black',
    strokeWidth: 2,
    fill: 'orange'
});

circles.add(circleRed);
circles.add(circleBlue);
circles.add(circleOrange);

stage.add(circles);

// On load we set stage size based on window size
setStageWidth();

// On window resize we resize the stage size
window.addEventListener('resize', setStageWidth);

1 个答案:

答案 0 :(得分:1)

您的功能似乎没有任何问题。你能解释一下你想要实现的更多内容吗?您的Max页面宽度和最大舞台宽度不应该是相同的数字吗?

您的功能正在做两件事:缩放和调整大小,但您正在关注基于水平变化的比例。尝试将其更改为对角线缩放系数或分别垂直和水平缩放。

// Sets scale and dimensions of stage in relation to window size
function resizeStage() {

   var horizScalePercentage = window.innerWidth / maxPageWidth; 
   var vertiScalePercentage = window.innerHeight/ maxPageHeight; 

   stage.setAttr('scaleX', horizScalePercentage );
   stage.setAttr('scaleY', vertiScalePercentage );
   stage.setAttr('width', maxStageWidth * horizScalePercentage );
   stage.setAttr('height', maxStageHeight * vertiScalePercentage );
   stage.draw();
};

//Sets scale and dimensions of stage to max settings
function maxStageSize() {
    stage.setAttr('scaleX', 1);
    stage.setAttr('scaleY', 1);
    stage.setAttr('width', maxStageWidth);
    stage.setAttr('height', maxStageHeight);
    stage.draw();
};

http://jsfiddle.net/8cw7J/1/检查这个小提琴