最近发现了KineticJS,因为我一直在尝试将我的Flash技能转换为HTML Canvas。这是一个令人印象深刻的工具! 题: 如果我使用像Bootstrap这样的前端,并且我有一个包含KineticJS生成的画布的几个div的页面,我可以将这些画布与页面上的其他内容一起扩展吗? 非常感谢。
-----由于评论的字符限制,我在OP中回复。 ---------
您是否会将其设置为最大尺寸,然后将其缩放为缩放浏览器时的缩放比例? 例如,在Actionscript中,我这样做是为了跟踪浏览器的大小并缩放阶段:
stageListener.onResize = function():Void {
// Calculate % of total allowable change for each dimension
var percentWScale:Number = (Stage.width - minStageWSize)/stageWResizeRange;
var percentHScale:Number = (Stage.height - minStageHSize)/stageHResizeRange;
// Test to see if the stage size is in the rescale range
if ((Stage.width < maxStageWSize) || (Stage.height < maxStageHSize)) {
pfarm.stageChanged = true;
// Calculate the scale factor for each dimension, but don't go below the minimum
var percentHScale:Number = Math.max(minimumScale, Stage.width/1024);
var percentVScale:Number = Math.max(minimumScale, Stage.height/768);
//trace ("H Scale Factor: "+percentHScale+". V Scale factor: "+percentVScale);
// Determine which window dimension to use for scaling -
// Use the one which is the most scaled down from the maximum stage size.
var getScaleFrom:String = (percentHScale << percentVScale)? "hScale" : "vScale";
// Scale the object
if (getScaleFrom == "hScale") {
baseParent._width = projectContentXSize * percentHScale;
baseParent._height = projectContentYSize * percentHScale;
} else {
baseParent._width = projectContentXSize * percentVScale;
baseParent._height = projectContentYSize * percentVScale;
}
} // END RESIZE OF SCROLLPANE CONTENT
为了帮助闪电难民(像我一样)越过复杂的边界进入HTML5土地,你能提供一个例子还是2?
答案 0 :(得分:7)
当然!
但是,要注意用CSS缩放Kinetic容器元素(如Bootstrap那样),因为浏览器会通过&#34;拉伸&#34;来调整大小。像素而不是让Kinetic重绘像素。
由于html canvas(以及Kinetic对象)实际上只是像素,因此在缩放Kinetic容器时,有关调整大小的常见警告也适用。
调整动力学阶段的更好方法是:
.setWidth
和.setHeight
.setScaleX
和setScaleY
后跟stage.draw
缩放舞台及其所有组件。 答案 1 :(得分:3)
这是the method recommended by markE的实现。它还包含this article on auto-resizing HTML5 games中描述的技术。
在此处查找有效的演示:http://jsfiddle.net/klenwell/yLDVz/
var iPadLandscapeDimensions = {
width: 1024,
height: 768
};
var KineticScreenAutoSize = (function() {
var self = {
$container: null,
stage: null,
baseWidth: 0,
baseHeight: 0
};
var init = function(selector, baseWidth, baseHeight) {
self.$container = $(selector);
self.baseWidth = baseWidth;
self.baseHeight = baseHeight;
initStage();
};
var initStage = function($container) {
self.stage = new Kinetic.Stage({
container: self.$container.attr('id'),
width: self.baseWidth,
height: self.baseHeight
});
};
/*
* Screen-Sizing Methods
*/
var autoSize = function() {
// Style required for resizeScreen below
self.$container.css({
position: 'absolute',
left: '50%',
top: '50%',
width: '100%',
height: '100%'
});
// Resize automatically
window.addEventListener('resize', resizeStageToFitScreen, false);
window.addEventListener('orientationchange', resizeStageToFitScreen, false);
// Resize now
resized = resizeStageToFitScreen();
};
var resizeStageToFitScreen = function() {
/*
* Following directions here: https://stackoverflow.com/a/19645405/1093087
*/
var resized = calculateResize();
// Resize the kinetic container element proportionally
resized.cssSettings = {
left: resized.xToCenter + 'px',
top: resized.yToCenter + 'px',
width: resized.width,
height: resized.height,
}
self.$container.css(resized.cssSettings);
// Let Kinetic know its container is resizing with .setWidth and .setHeight
self.stage.setSize(resized);
// Use .setScaleX and setScaleY followed by stage.draw to scale the stage
// and all its components.
self.stage.scaleX(resized.xScale);
self.stage.scaleY(resized.yScale);
self.stage.draw();
return resized;
};
var calculateResize = function() {
var resized = {
width: 0,
height: 0,
xScale: 0,
yScale: 0,
xToCenter: 0,
yToCenter: 0
}
var windowWidth = window.innerWidth,
windowHeight = window.innerHeight,
desiredWidthToHeightRatio = self.baseWidth / self.baseHeight,
currentWidthToHeightRatio = windowWidth / windowHeight;
if ( currentWidthToHeightRatio > desiredWidthToHeightRatio ) {
resized.width = windowHeight * desiredWidthToHeightRatio;
resized.height = windowHeight;
}
else {
resized.width = windowWidth;
resized.height = windowWidth / desiredWidthToHeightRatio;
}
resized.xToCenter = (window.innerWidth - resized.width) / 2;
resized.yToCenter = (window.innerHeight - resized.height) / 2;
resized.xScale = resized.width/self.baseWidth,
resized.yScale = resized.height/self.baseHeight;
return resized;
};
/*
* Public API
*/
var publicAPI = {
init: init,
stage: function() { return self.stage },
autoSize: autoSize
}
return publicAPI;
})();
// Launch Resize
$(document).ready(function() {
KineticScreenAutoSize.init(
'div#stage-container',
iPadLandscapeDimensions.width,
iPadLandscapeDimensions.height
);
KineticScreenAutoSize.autoSize();
});