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.
Related
I am doing an animation on a 1320px * 1440px html5 canvas. But I don't want to see this huge canvas on the screen rather I want to see this on a 550px * 600px canvas so that the drawing quality looks better. how to do this?
You are decreasing the height and width by a factor of 2.4, so you simply need to decrease the canvas and the animation inside it by that factor as well.
Make the canvas width attribute = 550 and the height attribute = 600
Multiply the animation width and height by .4167 (or 1/2.4) to scale it appropriately. You could easily do that by using the context.scale(.4167, .4167); method before drawing anything.
More information about scale() here.
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)
Html canvases have max sizes depending on device/browser. Eg some mobiles have roughly a 1000x1000 canvas max. What about images stored in memory? Can these be up to the ram of the phone/pc? Can you 'drawImage' large images to these 1000x1000 canvases where it does auto-cropping? (I don't have a phone to test this even for a specific device.)
The max size of an image object is probably browser or memory dependent.
More to your question...
The image object you draw into the canvas can be larger than the canvas.
This causes the canvas to become a "viewport" into the image.
For example:
Your canvas is 1000x1000.
Your image is 1500x1500.
If you do this then the image pixels will be cropped at x>1000 and y>1000.
// canvas shows image pixels from x=0-999 and y=0-999
drawImage(yourImage,0,0)
If you do this then image pixels will be cropped at x<200 & x>1200 and y<200 and y>1200.
// canvas shows image pixels from x=200-1999 and y=200-1999
drawImage(yourImage,-200,-200)
A mental exercise to see how this works is to imagine a piece of paper with a rectangle "viewport" cut from it. Then move a larger-than-viewport image behind the paper. Only a part of the image will only be visible through the viewport. You can move the image and a different portion of the image will be revealed in the viewport.
I want to decrease an 480 X 480 bitmap image size to 30 X 30 pixel size but keeping the whole height and width intact. (I do not want to scale or use height/width property! )
So if i divide 480/16 = 30. So i need to take average pixel values of 30 pixel elements and put it into new image.
How to take the average in actionscript 3.0? I looked at getpixels() method, is their any simple way/methods to achieve this?
Let me put in more simple way - I am trying to reduce pixels in an bitmap image from 480 X 480 to 30 X 30, the height and width remain same and i expect some amount of distortion after converting image to 30 X 30.
I did scaling but it reduces width and height, if i again increase width and height it just regains normal pixels. Thanks!
Why don't you simply then make a copy of the whole image in code, but use the simple scaling to scale the copy, and only present that to the user. Also look at this from Stack Overflow
How to resize dynamically loaded image into flash (as3)
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.