how to scale a path in raphael.js - path

I don't understand why the following example is not working... It only works if I am resizing it once but if I try to resize it again it get's all messed up...
http://jsfiddle.net/JAVWN/1/

You would think that if you called scale() once and then called scale() again, the results would be cumulative, but this doesn't seem to be the case. It seems that if you call scale(), it remembers the original dimensions and scales those rather than the previously recalculated dimensions.
The way I got your example to work was to store the scale (one variable for each dimension) on your object (starting with scaleX = scaleY = 1). Then I multiplied the stored scale by the scale calculated from the final position of the resizer/cursor and stored those in your object. Finally, I used the stored scales in the scale() function call to resize your shapes.
See http://jsfiddle.net/donniec/JAVWN/2/

I fixed it... just use node.resetScale();

Related

What's the difference of setScale and size of SKSpriteNodes in SpriteKit and Swift?

I was trying to set the size of a SKSpriteNode from a PNG image (OK with that, just a:
test = SKSpriteNode(imageNamed: "myImage")
But when it comes to resize it, I'm not sure if I should use test.setScale or test.size.
What's the difference and when should I use each of them?
setScale():
Sets the xScale and yScale properties of the node. These two parameters are the scaling factor that multiplies the width (for xScale) and the height (for yScale) of a node and its children.
Code sample:
spaceship.setScale(0.50) // decreased scale to it's half size
size():
Expresses the dimensions of the sprite (width and height), in points.
Code sample:
spaceship.size = CGSizeMake(100.0, 70.0) // Re-size the dimensions to these values
When do you use setScale and when do you use size?
Usually setScale is used to increase or decrease the sprite about the scale factor, look for example the SKAction.scaleTo used to make a zoom in or zoom out.
size is frequently used because you can express in points the exactly values you want to do to your sprite.
You should use size when you want to change the specific size of a SKSpriteNode without affecting the size of any children of that sprite node.
Scaling a sprite node not only changes the size (scale) of the node, it also scales all that sprite node's children appropriately (proportionally) as well.
If a sprite node has no children, then there's no practical difference between size and scale.
Note: I find it helpful to use size when it doesn't matter so that I know that a scale of 1.0 is always the default size - handy for animating using scaleTo instead of scaleBy and back again to default size.
If you are using a spriteNode, I would use .setScale, or .xScale or .yScale , because those are specifically for sprites. .size can also be used for the size of labels, and text views in UIKit.
Lets say you want the sprite to be 2x bigger:
theNode.xScale = 2.0
theNode.yScale = 2.0
Note that they have to be floats, not Integers
So to sum it up, .setScale is for spriteNodes and images in general, and .size is for a vast amount of things that aren't images, like labels and more. So if you are using a sprite, use .setScale. If you are resizing a block use .size.
If you believe that this is the right answer, please mark it as so so it helps anyone else with this issue. Thanks!
I alway resize using the .size property since I believe a node's physics body does not automatically scale when you use the .setScale / .xScale / .yScale. If you scale the node, you will need to redo the size of the physicsBody.

Why is object.y not positioning the image in Corona SDK?

displaycontent = display.newImageRect (rawdata[currentpath][3], screenW*1.1, ((screenW*1.1/1654)*rawdata[currentpath][6]))
displaycontent.anchorY = 0
displaycontent.y = screenH*0.78
My program loads an image from a database to be displayed on the mobile phone's screen, everything works correctly apart from being able to position it with the y coordinates.
The only thing that changes its position is the anchor point 0 puts the top of the image in the centre of the screen, and values from 0.1 - 1 all position it higher. Changing the y position via object.y has zero effect regardless of what I set it as.
(the size settings probably look a bit weird in the first line, but this is because the images are different sizes and need to show the correct proportions on different screen types).
Btw I am using a tabbar widget as the UI (in case that is relevant)
Any help would be appreciated.
Edit: I am aware that displaycontent is bad name for a variable because of its similarity to things like display.contentCenterY for example, this will be changed to prevent any confusion when I look over the code in future.
I went through my code and tried disabling sections to find the culprit and a content mask was preventing me from setting the position of the loaded images within it.
I will look over my masking code and fix it (should be straight forward now I know where the problem started).
If anyone else has a similar problem (where an image or object wont position itself on given coordinates) check your content mask as that may be the issue!

Apply definitely CGAffineTransform* to a UIView

I'm having a problem with scale transformation I have to apply to UIViews on Swift (but it's the same in objective-c too)
I'm applying a CGAffineTransformMakeScale() to multiples views during a gestureRecognizer.
It's like a loop for a cards deck. I remove the one on top and the X others behind scale up and a new one is added in the back.
The first iteration works as expected. But when I try to swipe the new front one, all the cards reset to their initial frame size because i'm trying to apply a new transform, which seems to cancel the previous one and reset the view to its initial state.
How can I apply definitely/commit the first transform change to be able to apply a new one after that based on the UIView resulting new size ?
I tried a UIView.commitAnimations() but no change.
EDIT :
Here's a simple example to understand what I try to do :
Imagine I have an initial UIView of 100x100
I have a shrink factor of 0.95, which means next views behind will be 95x95, then 90.25, then 85.73, etc
If I remove the top one (100x100), I want to scale up the others, so the 95x95 will become 100x100, etc
This is done by applying the inverse of the shrink factor, here 1.052631...
First time I apply the inverse factor, all views are correctly resized.
My problem is, when I trigger again by a swipe on the new front UIView a new resize of all views (So, for example, the 90.25x90.25 which became 95x95 should now scale to 100x100).
At this moment, the same CGAffineTransformMakeScale() is apply to all views, which all instantly reset to their original frame size (so the now 95x95 reset to 90.25x90.25, and then begin to apply the transformation on this old size).
As suggested here or elsewhere, using UIView.commitAnimations() in the end of each transformation don't change anything, and using a CGAffineTransformConcat() is like powering over and over the scaling by himself and of course views become insanely big...
I hope I made myself more clear, that's not easy to explain, don't hesitate to ask if something is wrong here.
After a lot of reading and consulting colleagues who know better than me about iOS programmation, here's my conclusion :
Applying a CGAffineTransformMakeScale() only modify visually a view but not its properties, and since it's difficult (and costly) to modify afterward the bounds and/or frame of a view, I should avoid to try to make a transform, update bounds, make another transform, etc.
Applying the same CGAffineTransformMakeScale() only reset the effect and not apply to the previous one.
Applying a CGAffineTransformScale() with the same values on top of the previous CGAffineTransformMakeScale() (or with a CGAffineTransformConcat()) has some unpredictable effect and will be very difficult to calculate precisely the new values to apply each time to get the effect I want.
The best way I can go with this is only applying one CGAffineTransformMakeScale() that I will keep updating scales values all along the view's life.
It implies now for me to rework all my implementation logic in reverse, but that's the easiest way to do this right.
Thanks all for your tips.

Corona SDK - object:scale() scales object but width does not change?

I've scaled my an object by .99 every frame for a certain amount of time. Then I scale it 1/.99 for the same amount of time. Due to rounding errors, the object ends up bigger/smaller instead of the same size. To fix this, I save the original width and height in variables and set object.contentWidth and contentHeigth equal to these variables whenever necessary. However, the object continues to grow or shrink and is never reset. When I print the original width and height variables, the content widths and heights, and the regular widths and heights, they're all the same value, as if the object was never scaled.
I assume the problem here is me misunderstanding the Corona SDK object functions and properties, so I didn't post any code. If it's not a misunderstanding, I'll post a simplified version of my code here; just let me know.
I assume:
If you're trying to scale back to original size.
You should scale it from 0.99 to 1.0, not to 1/.99.
Use function object:scale() for relative scaling
Use property object.xScale,object.yScale for absolute scaling.
Try it with absolute scaling.

How to adjust GLCamera to show entire GLScene

I have a GLScene object of varying (but known) size. It is completely surrounded by a TGLDummyCube.
I want to position the GLCamera (with CameraStyle: glPerspective) so that the object is completely visible on screen. I got this running basically - the object is visible, but the distance is sometimes too far, or the object is larger than the screen and clipped.
How can I do that? I suppose that this can be done by a clever combination of camera distance and focal length, but I have not been successful so far.
This seems to be different in GLScene compared to OpenGL. I'm using GLScene and Delphi 2007.
Although varying the camera distance and focal length will change the object's visual size, it has the drawback of also changing the perspective, thus leading to a somewhat distorted view. I suggest to use the camera's SceneScale property instead.
Alas, I have no valid steps to calculate the correct value for that. In my case I have to scale to a cube with varying size, while the window size of the viewer is constant. So I placed two dummycubes at the position of the target cube, each sized to fit either the width or the height of the viewer with appropriate values for SceneScale, camera distance and FocalLength. During runtime I calculate the new SceneScale by the ratio of the target cube size in respect to the dummycube sizes. This works quite well in my case.
Edit: Here is some code I make for the calculations.
ZoomRefX and ZoomRefY are those DummyCubes
TargetDimX and TargetDimY give the size of the current object
DesignWidth and DesignHeight are the size of MyGLView at design time
DesignSceneScale is the camera's SceneScale at design time
The calculation code:
ScaleX := (ZoomRefX.CubeSize*MyGLView.Width)/(DesignWidth*TargetDimX);
ScaleY := (ZoomRefY.CubeSize*MyGLView.Height)/(DesignHeight*TargetDimY);
NewSceneScale := Min(ScaleX, ScaleY)*DesignSceneScale;
The DummyCubes ZoomRefX and ZoomRefY are sized so that they have a small margin to either the left-right or top-bottom edges of the viewing window. The are both positioned so that the front faces match. Also the target object is positioned to match its front face with those of these DummyCubes.
The formulas above allow the window size to be different from design time, but I actually didn't test this feature.
#Andreas if you've been playing with SceneScale (as you mentioned in comments) that means that you are looking for a proper way to fit object within camera view by either changing camera distance/focal length or by resizing object. If so, the easiest way to resize single object to fit the screen is to use its BoundingSphereRadius property like this:
ResizeMultiplier := 2; //play with it, it depends on your camera params
GLFreeForm1.Scale.Scale(ResizeMultiplier / GLFreeForm1.BoundingSphereRadius);
You can add GLDummyCube as root object for all other scene objects and then resize GLDummyCube with method mentioned above.

Resources