Increase an Image's clickable area - libgdx

Edit: I am using LibGDX framework.
There is an Image Actor, which is:
Attached to a Stage.
Has an OnClickListener, e.g.:image.addListener(new OnClickListener() { ... });
This Image's touchable area is fixed on the image's width and height.
I want to increase the touchable area by N pixels.
How can I achieve this?
Here's an illustration:
(red rectangle = touchable/clickable area)

Image already supports this out of the box. The actor can be bigger than the drawn image itself. You can supply a Scaling strategy for the drawn picture and in case you use Scaling.none, the drawn picture will be independent of the actor's size.
image.setScaling(Scaling.none)
int N = 30;
image.setSize(image.getImageWidth() + N, image.getImageHeight() + N);

The way I would approach it, is to have a custom image view, with the actual Image View inside a RelativeLayout. The relative layout has padding and/or margin set, so that it is bigger than the imageView. then, when you set the onClicklistener, set it on the relative layout as well as the image layout (in your custom class)

Related

Cocos-2dx v3 centering background sprite problems

Here is my background image:
And here is some code that I would assume scales this image to fully fit the screen.
Size visibleSize = Director::getInstance()->getVisibleSize();
auto bg = Sprite::create("grad.png");
bg->setScale(visibleSize.width / bg->getContentSize().width, visibleSize.height / bg->getContentSize().height);
bg->setAnchorPoint(Vec2(0,0));
addChild(bg);
I would expect those 4 lines to create a background sprite that would cover the entire screen size. However, here's a screenshot of what I am actually getting on my iPhone6+:
If I change the first line to
Size visibleSize = Director::getInstance()->getWinSize();
Then this is what I get, which isn't quite right either:
Using VisibleSize is correct, you just need one more change:
bg->setPosition(director->getVisibleOrigin());
By default, cocos2d-x uses ResolutionPolicy::NO_BORDER, so the bottom part of winSize is likely to be cropped.
getVisibleSize() returns the visible origin in Point rather then pixel.

How to make flash animation responsive

i'm starting to build this bootstrap site where I have flash animation on my fontpage. --> http://testi9.aada.fi/index.php?cID=180
My animation size is 1472px x 485px. Now I want it to be 100% width on bootstrap site so that it would strech nicely on different screen sizes.
I added this css code on bootstrap css file:
#swfcontent169 {
border:solid thin #f00;
width:100%;
}
Now my flash animation is 100% but somehow those "symbols" that are
on my flash animation on each side are visible too and animation is not stretched as i would like it to be (full width).
Can someone help me out with this?
// Mika
You need to set the proper scaling mode for the flash object. (the scale embed property) or stage.scaleMode through AS3 code.
Here is a visual rundown of the options: (you will likely want exact fit or no border)
Let's say this is your document:
We have four boxes (one per corner), and some gray boxes that are off the stage.
Here are you options:
Show All:
Show all scales the content so the whole stage fits, but then you get a gutter if it's not the right size and objects off stage may be visible.
No Border:
This will scale it so the whole content will fill the bounds (keeping aspect), but then if it's not the exact size, you'll get cropping of the stage, see how the boxes are being cropped on the top bottom? You can change the way it aligns the cropped stage with the stageAlign property.
Exact Fit
This will just make the whole stage fit in the area defined, but it will not honor your aspect ratio and things could looked squished/stretched.
No Scale
This won't scale the stage AT ALL. So if the defined area is smaller than the stage, it will crop it, if larger, you'll get gutters.
You probably want exact fit or no border - depending on if you need the aspect ratio to be fixed. You can also use no-scale and use code to align your contents the way you want.
You can align the content several ways too using the align embed parameter.
You can adjust these in your embed code. There are also options in the FlashPro publish settings (when you click on the HTML Wrapper Format), or you can use an online tool like: http://embed-swf.org/embed-swf.php

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

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.

Set HTML5 Canvas Height And Width And Having Content Inside Scale

Is it possible to set the width and height of a canvas element and have the existing content scale to fit these new dimensions?
Right now the user uploads an image and my page creates a canvas element containing this image and whose dimensions are the same size as the image. I want to limit the size that I'm working with, however, so here is an example of what I want to have happen:
The user uploads an image that is 1600 x 1200 pixels (Not saved to server)
The data goes right to an html5 canvas object
The canvas height and width are set to 800 x 600 and the image content scales appropriately and then is displayed.
Right now if I set the canvas width and height it just crops the image at those dimension, not resizing as I would like. Thanks!
There are many ways to call drawImage
One is:
ctx.drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh)
Where dx,dy,dw,dh are the destination x, y, width, and height.
If you make dw and dh always 800x600, the image will drawn will always automatically be scaled to 800x600.
Here's a tiny example that will always draw any size image to 800x600 http://jsfiddle.net/jAT8Y/
Another alternative is to use context.scale function. Your image quality will remain better if you use scale.

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.