Recently discovered KineticJS as I've been trying to convert my Flash skills to HTML Canvas. It is an extremely impressive tool! Question: If I'm using a front-end like Bootstrap and I have a page with several divs that contain KineticJS-generated canvases, can I get those canvases to scale along with everything else on the page? Thanks much.
----- Due to character limit of comments, I'm replying in the OP. ---------
Would you set it up so that there is a maximum size and then have it scale as when the browser is scaled? For example, in Actionscript I did this to track the size of the browser and scale the stage:
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
For the benefit of flash refugees (like me) who are crossing the complex border into HTML5 land, could you provide an example or 2?
Sure!
But, be careful about scaling the Kinetic container element with CSS (as Bootstrap does) because the browser will resize by "stretching" the pixels rather than letting Kinetic redraw the pixels.
Since html canvas (and therefore Kinetic objects) are really just pixels, the common warnings about resizing also apply when scaling the Kinetic container.
A better way to resize your kinetic stage is to:
.setWidth
and.setHeight
.setScaleX
andsetScaleY
followed bystage.draw
to scale the stage and all its components.Here's an implemetation of the method recommended by markE. It also incorporates techniques described in this article on auto-resizing HTML5 games.
Find a working demo here: http://jsfiddle.net/klenwell/yLDVz/