Easel.JS > Bitmap larger than canvas... but shows up smaller - javascript

In Easel.js I'm putting a bitmap on the screen that's 1920 pixels wide.
The Canvas itself is smaller (1500 pixels).
However, the Canvas shows the image on the screen and it is not large enough to even reach the edge.
In the code, I'm not scaling the image, the stage nor the container.
How can I get it to show up in its actual pixel size?
Is Easel.js taking screen-density into account? I didn't see a difference between a "normal" screen and a retina display...
Thanks!

Whoops, I'm an idiot. The pivot-point was set in the middle of the image:
bmp.regX = image.width / 2;
bmp.regY = image.height / 2;
Solved it. Sorry to bother you all!

Related

How to scale image to KonvaJS canvas without stretching?

I'm trying to show images with different aspect ratios on 1080x1920 canvas. So when I try to show 9:16 images, it scales perfectly, but if aspect ratio is 4:3 or 3:4, scaled image stretches.
What I want to do is, if aspect ratio is 3:4, scaling and showing image as 9:16 on center and cutting edges. (So if original width is 1440, showing only 1080 and leaving 180px at each side off the screen. And if original width is 3024, scaling it down to 1440 and then cut)
And if image is not portrait but landscape, showing it on center and filling remaining space with some color.
How can I achive this? Does KonvaJS provides easy way to do this?
No konvaJS does not provide a way around that, you need to use your own logic for positioning the image at the center for different resolutions.
I have used logic for finding the image resolution which fits the provided dimensions in my personal project. Check if it helps.
function getScaledImageCoordinates(
containerWidth,
containerHeigh,
widt,
height,
) {
var widthRatio = (containerWidth) / width,
heightRatio = (containerHeight) / height
var bestRatio = Math.min(widthRatio, heightRatio)
var newWidth = width * bestRatio,
newHeight = height * bestRatio
return {newWidth, newHeight}
}
You can find the image resolution that fits your container (say 1080x1920) and then center position the image on Konva Stage

Resizing images on canvas

I have the following code:
https://jsfiddle.net/8o3sn9mh/17/
<canvas id='c' style='position:absolute; left:0px; top:0px;'>
</canvas>
<script>
var
canvas = document.getElementById('c'),
ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var flower = new Image();
flower.src = "http://plainicon.com/dboard/userprod/2803_dd580/prod_thumb/plainicon.com-46129-128px-af2.png"
flower.onload = function(){
ctx.drawImage(flower,0,0,128,128, 0, 0, 30, 30);
ctx.drawImage(flower,0,0,128,128, 0, 0, 218, 218);
</script>
Short story: as you can see the flower doesn't resize well, it's quality is lost.
Long story:
I am making a game with shapes. basically, I use image with ratios such as 128-128 or 80-80 since its designed for phones, and I make sure when I resize the images to keep that 1:1 ratio. With calculations based on the user's window, I decide how much to resize the images proportionally since the canvas is on full screen. It works decent on some screen but on some it doesn't - the images can downscale too much and look poor or upscale too much and look unclear. it is most noticeable on triangle images(I know I can draw pure shapes with canvas but I need to draw faces on them so it is impossible). Any good method to do nicely?
That is the nature of pixel based images.
When downscaling try to keep the image sizes whole ratios, it will improve the quality.
Eg you have 128, then you reduce it to 30, that means pixels are reduced by 128/30 = 4.2666. Pixels are discrete units of information and can not easily be cut into smaller units without loss of quality.
If you changed the size to 32 pixels 128/32 = 4 you get better quality because you are not cutting up any pixels.
For upscaling there is little you can do, Its either blurry or pixelated. Your source images should always be at a higher resolution than you need. Rule of thumb is "only downscale".
If the quality of the images downscaled with whole ratios is not to your liking then the best option is do the scaling in production on photoshop (or the like), then let the device select which images to download. This will give you the best possible quality for all devices and save you and the users some bandwidth (for smaller res devices)

How to center an image on canvas with CreateJS

I'm learning CreateJS, but have run into a little struggle.
Usually I'd center an image by dividing a canvas in half and subtracting half the width/height to those values, but unfortunately I don't know how to access a scaled down image's size using CreateJS.
Even using obj.image.width didn't get me anywhere, as it returns 0.
Is there any way of doing this, or some function I'm unaware of?
Massive thanks!
You can multiply the image size by its scale (scaleX and/or scaleY) to get the "transformed" size.
// Note that the image must be loaded before you can get the width
bmp.x = (stage.canvas.width - bmp.image.width * bmp.scaleX) / 2;
Here is a quick fiddle:
http://jsfiddle.net/lannymcnie/v6vuwntx/

Scaling a canvas to full screen really decreases the resolution

I'm trying to make a full screen game to help me learn JS. I made a canvas item and streched it to the size of the screen using CSS. Then I draw a circle. The issue I have is that the circle looks terrible!
_draw.arc(20, 20, 10, 0, 2 * Math.PI);
Here is a jsfiddle example. http://jsfiddle.net/H5dHD/152/
I've tried using different scale factors (so _draw.scale) but it dosent seem to matter...
What am I doing wrong?
P.S. I know the coordinates are off. I didn't include that code for the example.
The problem is that you resized the canvas using the CSS-style, and do not change the actual width and height. When you use CSS styling to change the size, the canvas will be stretched, but the internal drawing resolution stays the same. The result is that the canvas blurs.
To change the internal resolution, change the width and height attributes of the canvas HTML element itself.
document.getElementById('iDraw').height = screen.availHeight;
document.getElementById('iDraw').width = screen.availWidth;
Here is your updated fiddle:
http://jsfiddle.net/H5dHD/154/

Everything inside my canvas got bigger after canvas resize

When I first started this project that I'm working on, my canvas size with 1400px wide and 480px tall. I realized that I am going to need to make the canvas the same size as the window itself later, so I did that and everything inside of the canvas zoomed in or something. I set a drawImage(); to be 300 px wide and 180 px tall, and it is a LOT bigger than that, the image is actually the same width as the canvas now. Any suggestions? Here's the link to the project:
http://brycemckenney.com/animation-app
Thank you guys!
You have set the dimensions through css, instead of the physical dimensions of the (image) canvas.
The relevant piece (for others to read in the future) of your code is:
var canvas = document.getElementById("canvas1");
var ctx = canvas.getContext("2d");
var windowHeight = $(window).height();
var windowWidth = $(window).width();
$(canvas).css({
height: windowHeight - 8,
width: windowWidth - 8
});
Think of it like this: suppose you have a normal jpg-image.
That jpg has it's own 'physical' dimensions (aka width and height).
In both HTML and CSS you can set the dimensions (in px, percent, etc) that you'd like the browser to render (scale) the picture (hey, the picture already has a immutable size right?).
Now for canvas:
In order for canvas to have a physical width/height, you have to set the .width and .height of the canvas-element itself, either in HTML or per javascript (a side-effect is that setting the physical dimensions is that the canvas will clear itself, as per spec).
Then to scale the image (like you did with the above jpg example) you use css (again in px/percent/etc).
I think this is a clever solution by the way to add that new canvas-element to the HTML-Spec!
So, rounding up:
A canvas with a width and a height of 300 px rendered as 100% of a container (like document.body) that measures 900x900px will be scaled-up 3 times!
The reverse (scaling down) will let you draw even more crisp lines by the way!
Hope this helps your understanding!

Categories

Resources