Easeljs, is it a recommend way to fix the size of container? - html

Maybe I am wrong, I learn Easeljs for a week only.
The Container has no width and height to set the size.
I have 2 questions:
1. is the size of Container, dynamically change with the child.
2. if question 1 answer is yes, can I add a big bitmap or shape to it, eg. a background image...etc. to control the size of Container?

Consider a container as a group of objects, not a physical container. Containers give you the ability to transform, translate, cache, and otherwise control multiple items as a single item. They do not really have a physical size, except that of their collective children.
There is no width or height mainly due to the cost of calculating the size, especially considering transformations, sub-containers, etc. There may be support added in the future for width/height, but for now its not available.
[UPDATE]: Containers do have bounds, based on the bounds of children (retrieved using container.getBounds() (docs) that have bounds. For example, a Container that has Sprite, Bitmap, Text, objects with manually set bounds, or cached DisplayObjects will report bounds using those children. Shapes do not have auto-calculated bounds currently, so will not contribute to container bounds.

This is a very bad hack, but if you absolutely must fix the size of the container, you can use something like this:
var blank = new createjs.Shape();
var width = 1;
var height = 400;
blank.setBounds(0, 0, width, height);
container.addChild(blank);
This will set the size of container to the blank image and now any background you draw on it will be visible.

Related

how to get the global width and height of movieClip after scaling parent(s)?

I do work on some already developped project.
myMovieClip has been scaled and is nested into many movieclips which may have been scaled themselves
When I trace his width and height, it does not give me the right width and size:
How can I get the absolute width and height ?
(the width and height it takes on the screen)
(a kind of localToGlobalWidth function)
regards
You can use the getBounds method of a display object to get the bounds (actual width/height and position) relative to whatever display object you pass to the method as an argument. (doucmentation)
myScaledObj.getBounds(stage); //would return a rectangle of where on the stage the display object is.
The width and height property of the returned rectangle would be what you'd use.
Do you know how many times it's been scaled, and by how much it's being scaled? In that case, you could trace the width * the scaling, for example:
mc1 has a width of 100, and is being scaled by 2:
mc1.scaleX = 2;
trace (mc1.width*2);
Extra information: if it's being scaled by a variable, replace 2 with the variable name.
This method should work even if it's being scaled multiple times, by using a variable to pile up the scaling:
var scaler1:int = 2;
var scaler2:int = 7;
var scalePiler:int = scaler1*scaler2;
mc1.scaleX = scalePiler;
trace (mc1.width*scalePiler);
Hope I helped and covered all possibilities, good luck with your program! ^^

scaling canvas directly VS scaling the CSS in HiDPI

Sorry, I am a beginner, sometimes i find people saying that I have to scale only the CSS, and the other examples i find that they multiply the size directly with the new scale, in other words canvas.width VS canvas.style.width
What is the difference?
Does latest Chrome behave like Safari (now in March 2014)?
Canvas consists of two parts: the element canvas which is what you see on screen. Then sort of "behind" the scenes there is the image bitmap which you draw onto.
Setting element.style will only affect the element itself, but not the behind the scene (internal) bitmap. This bitmap is simply stretched to fit the element size (like an image). If the size isn't specified it will default to 300 x 150 pixels.
The width and height properties (or attributes for the tag) are the ones setting the size of the internal bitmap.
An element without CSS will typically adopt to the size of the internal bitmap (there is pixel aspect ratio involved here as wel but normally the relationship is 1:1).
You can however override this by setting the element's CSS size. Again, it doesn't affect the internal bitmap but simply stretches it to fit the element.
All browsers should behave the same.

Any way to affect the original width of the stage, in modes other then StageScaleMode.NO_SCALE?

Documentation for the stageWidth property of the stage object states that:
If the value of the Stage.scaleMode property is set to
StageScaleMode.NO_SCALE when the user resizes the window, the Stage
content maintains its defined size while the stageWidth property
changes to reflect the new width size of the screen area occupied by
the SWF file. (In the other scale modes, the stageWidth property
always reflects the original width of the SWF file.)
But is there any way to affect that original width of the stage, without recompiling swf?
The stageWidth and stageHeight can not be set. They are based on the stagescalemode and the html container.
Or lets say there is a limited huge artboard what is the displayable area in flash(not regarding stageWidth).
If your swf has StageScaleMode.NO_SCALE its stageWidth will tell you the width of its container.And it will always fit its parent html element by showing you more or less of this artboard. But this can not directly be set by as3. To do so you need js.
If you have any StageScaleMode that scales the swf. Then its getting messy because that limited huge artboard gets streched and distrorted to meet the fittings.One pixel in as3 doesnt mean a pixel on screen anymore. But the part of that artbord that is visible will always be the same.

Is there a reason I can't change the width and height of an empty display object?

In the documentation for DisplayObject, it states that the width and height of the DisplayObject can not be changed if it is empty. Why is this restriction necessary? In every other framework I have used, you can resize containers that are empty. What is the reason for this madness?
In Flash, a DisplayObject is not solely a container for other DisplayObjects, though it can be. In Flash, a DisplayObject is just that, a displayable object... one that can be added to the DisplayList. What would be the original size of an empty sprite? 0x0? 1x1? The size is therefore determined by the contents being displayed.
That being said, you can change the scaleX and scaleY properties of an empty DisplayObject.
var c:Sprite = new Sprite();
c.scaleX = c.scaleY = 2;
c.graphics.beginFill(0);
c.graphics.drawRect(0, 0, 200, 80);
c.graphics.endFill();
addChild(c);
The sprite is 400x160 on the stage. :)
The reason you can't change an empty object's width and height is that they are ultimately derived properties. Along with the object's content, scaleX and scaleY are what really determines the object's size - width and height are really just convenient ways to adjust the scale.
In other words, when you change an object's width property, all Flash really does is to calculate what scaleX would be necessary to achieve the desired width, and then change scaleX to that value. When the object is empty, there is no scale value that would achieve any width except zero, so Flash ignores the request.

scaling logo in html5 <canvas>?

Having trouble scaling with . It seems to make sense to code up a drawing in canvas to a fixed size (ie 800x600) then scale it for specific locations - but sizing occurs in 4 places: 1) in the context definition (ie ctx.width = 800 2) with ctx.scale; 3) in html with
I can scale it with ctx.scale(0.25,0.25) and use but this doesn't appear right - it seems to want the scale to be proportional.
css sizing simply makes it fuzzy so not a good way to go. Any ideas?
Actually, you can resize a canvas using stylesheets. The results may vary across browsers as HTML5 is still in the process of being finalized.
There is no width or height property for a drawing context, only for canvas. A context's scale is used to resize the unit step size in x or y dimensions and it doesn't have to be proportional. For example,
context.scale(5, 1);
changes the x unit size to 5, and y's to 1. If we draw a 30x30 square now, it will actually come out to be 150x30 as x has been scaled 5 times while y remains the same. If you want the logo to be larger, increase the context scale before drawing your logo.
Mozilla has a good tutorial on scaling and transformations in general.
Edit: In response to your comment, the logo's size and canvas dimensions will determine what should be the scaling factor for enlarging the image. If the logo is 100x100 px in size and the canvas is 800x600, then you are limited by canvas height (600) as its smaller. So the maximum scaling that you can do without clipping part of the logo outside canvas will be 600/100 = 6
context.scale(6, 6)
These numbers will vary and you can do your own calculations to find the optimal size.
You could convert the logo to svg and let the browser do the scaling for you, with or without adding css mediaqueries.
Check out Andreas Bovens' presentation and examples.
You can resize the image when you draw it
imageobject=new Image();
imageobject.src="imagefile";
imageobject.onload=function(){
context.drawImage(imageobject,0,0,imageobject.width,imageobject.height,0,0,800,600);
}
The last 2 arguments are the width an height to resize the image
http://www.w3.org/TR/html5/the-canvas-element.html#dom-context-2d-drawimage
If you set the element.style.width and element.style.height attributes (assuming element is a canvas element) you are stretching the contents of the canvas. If you set the element.width and element.height you are resizing the canvas itself not the content. The ctx.scale is for dynamic resizing whenever you drawing something with javascript and gives you the same stretching effect as element.style.