I've started experimenting with drawing on a Canvas object. In the past my exposure to a Canvas has been centered (NPI) around image manipulation, so I'm moderately familiar with its coordinate system and transformation process. I'm working with a test canvas that has a screen dimension of 30 X 30. After getting a handle to the 2D context I issue one call:
ctx.fillRect(0,0,10,10);
produces a little black spec about the size of a pin head in the upper left corner. To get the rectangle to be of any size, say something approximate 1/4 of the canvas, requires an adjustment to:
ctx.fillRect(0,0,200,200);
So, how has this canvas's scale been skewed? Yes, I guess I could "de-scale" it back to something resembling normal, but I'd like to figure out what's causing this in the first place. I've disabled Jquery thinking it might be interfering somehow, but that did not help.
Any ideas?
UPDATE:
The canvas is added to the dom along these lines:
<canvas id='foo_" + self.groupIndex + "' class='hCanvasClassName'>"
whereby the CSS defines the width at 100% and the height is assigned by JS code.
You didn't show the code for this, but it sounds as you have applied the size for the canvas using CSS and not directly on the element which would explain why it scales down.
You need to do this:
<canvas width="30" height="30" id="myCanvas"></canvas>
If you did this:
<canvas style="width:30px;height;30px;" id="myCanvas"></canvas>
what will happen is that the canvas element uses a default size (300x150, see here: http://jsfiddle.net/AbdiasSoftware/CfMNf/) and then you scale that down to 30x30 by css which means everything now drawn to the canvas will be scaled accordingly.
Related
I want my canvas and background image to scale to any browser size which I have learnt is possible through this.
<canvas id="canvas" style="width:100%; height:100%;
background-size:100% 100%; left:0px; top: 0px;">
Setting the width and height using percentages causes whatever I draw on top to come out blurred.
The reason for this I read is that it comes out blurry because the width and height of canvas set using css is different.
It is absolutely imperative that I keep track of the co-ordinates since I have written a collision detection logic using the original size of the image.
Is it possible to dynamically change the canvas size to fill the screen, redraw it and ensure that the collision logic works perfectly without blurry drawing on top?
Lets take an example here. The image floor.jpg that I want as the background of the canvas is 1200X700. I want the canvas to be 100% of the page width and height. My animated player that moves on top of it requires collision detection with walls which I have coded keeping in mind the 1200x700 image.
I have gone through the various similar question on this site, but can't seem to figure it out.
I'm using this HTML
<img src="stone.png" height="100" width="100"></img>
to display a tiny image that originally contains 5x5 pixels to instead be 100x100. The image becomes extremely blurry when I do this. Is there any way I ca retain the intentional "pixely" design when resizing it?
Note: I don't want to repeat the image, I just want to display it in a larger way and keep each pixel distinct.
No, there isn't.
When you scale an image of 5x5 pixels to 100x100 there needs to be 20x interpolated pixels for each axis (generated synthetically using bi-cubic or bi-linear interpolation) between each points which will always result in a blurry image.
You cannot even fix a huge scale-up like that in dedicated software.
The only solution is to create your image at the intended resolution or close to it. It's also better to down-scale an image as you would remove information rather then generate it.
I have canvas element and I want to scale it down, but without changing it's js logic. Drawing space in js should always be 600x300px, even if it is displayed in HTML as 300x150px. I know, I can resize image with static resolution, but can I do the same with canvas?
Changing the size using CSS scales it
Live Demo
So basically you set its size for drawing objects, etc, via the width and height properties like so
var canvas = document.getElementById("canvas"),
ctx = canvas.getContext("2d");
canvas.width = 600;
canvas.height = 300;
and then change its displayed size using css
#canvas{
width: 300px;
height: 150px;
}
Loktar has one way, using CSS, but that might cause some things to look funny. For instance paths scaled using CSS and scaled using the canvas' own transform may look very different (with the CSS ones looking bad and the canvas ones looking smooth). This depends on the browser though and might be perfectly fine. On chrome at least, text scaled this way looks very bad.
Instead I'd recommend looking at what I wrote here about the concept of "model" coordinates: Working with canvas in different screen sizes
Write everything as if the drawing space is 600x300, but keep a canvas that is 300x150.
Before drawing anything use ctx.scale(0.5, 0.5); and everything will look great!
It's quite possible after all to write one canvas app and the have it scale to all sorts of screens, even if you're just targeting one screen size.
I've just started working with the html5 canvas element.
I'm using the latest firefox and chromium browsers. And so far, they're
responding alike.
What I'm trying to achieve is scaling of an image without having to
specify the canvas or image drawing sizes. I'd like the canvas to fill
the browser window, and for the image to fill the canvas without
specifying any sizes. And to readjust canvas and its image on the
fly when the user adjusts the browser's frame.
The mansion pic that I'm testing with is 4284x2844.
I've managed to achieve dynamic scaling, but there's a problem...
if I don't specify sizes the image becomes blurry.
This is my first stackoverflow question and I haven't conquered the
formatting. So, please take a look at the small amount of code over
at pastebin:
http://pastebin.com/88faqJUx
Thank you for your help.
I found the solution...
Adding two lines, with no other changes, did the trick, though at this point I'm not exactly sure
why it was originally failing, but thoroughly happy to move on...
<canvas id="taba_main_canvas">
Your browser does not support the canvas element.
</ canvas>
<script type="text/javascript">
var main_canvas=document.getElementById("taba_main_canvas");
var cxt=main_canvas.getContext("2d");
// adding these next two lines solved the blurriness issues
//Set the canvas width to the same as the browser
main_canvas.width = window.innerWidth;
main_canvas.height = window.innerHeight;
var img=new Image();
<!-- mansion pic 4284x2844 -->
img.src="images/mansion_3344.png";
img.onload = function()
{
<!-- use the graphics full size and scale the canvas in css -->
cxt.drawImage(img,0,0,main_canvas.width,main_canvas.height);
}
</script>
Just one tiny little problem, the vertical size of the image is apparently just a few lines taller
than the canvas and so I get a vertival scrollbar. Dragging the browser window taller, which normally
would eliminate the vertical scrollbar has no effect. I've tried manipulating the canvas or image height
in the code, but that didn't change anything.
Still, having the image look clean is a big win. I'm moving on for the moment and will revisit this
later.
The other way to do this is to latch on to the document onresize event and resize the canvas by using window.innerWidth and window.innerHeight or some such thing. I've used it that way myself, but that was for something which I didn't care about IE support - see W3C DOM Compatibility - CSS Object Model View at quirksmode for info about browser support. Note also that the scrollbar width is included in innerWidth and innerHeight; if your page may need scrolling, you may wish to do something like subtract 20 and pad the containing element with a suitable background colour.
I presume that you're not just trying to draw an image - if you were just doing that, <img> would be a much better match.
Edit: jQuery has $(document).width(); and $(document).height(); which seem to get the right figures. Another edit: actually they're wrong; they're the document width and height, not viewport width and height, so I think innerWidth and innerHeight may be all there is.
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.