JCanvas: How do I get the size of an Image?

174 Views Asked by At

I have drawn an image like this:

$("canvas").drawImage({
        x: 0,
        y: 0,
        source: src,
        draggable: true,
        layer: true,
        name: "image",
        fromCenter: false,
    }
});

The image shows as it should. It is larger than the screen and I want to draw a much smaller rect (by the factor of .05) the sides of which are proportionate to the original image. How do I retrieve width and height of the above image?

I tried:

var imageWidth = $("canvas").getLayer("image").width;
var imageHeight = $("canvas").getLayer("image").height;

But alert($("canvas").getLayer("image").width); returns null. I also tried:

var imageWidth = $("canvas").getCanvasImage.width;
var imageHeight = $("canvas").getCanvasImage.height;

But alert($("canvas").getCanvasImage.width); returns undefined.

1

There are 1 best solutions below

1
Ranjeet On

You can use below statement to get height and width of canvas image

var imageWidth = $("canvas").width();
var imageHeight = $("canvas").height();