I'm trying to directly center an image that's dynamically added to the canvas via KonvasJS.
Here's the fiddle:
http://jsfiddle.net/71Lw0bk8/7/
I already have the code figured out, but it's not using Konva, it's an attempt of doing this without the library, and it works perfectly.
function addImage(imgUrl) {
const img = new Image();
img.onload = function () {
var padding = 20;
while (img.width + padding > canvas.width || img.height + padding > canvas.height) {
if (img.width + padding > canvas.width) {
let newWidth = canvas.width - (padding * 2);
img.height = Math.round((img.height / img.width) * newWidth);
img.width = newWidth;
}
else if (img.height + padding > canvas.height) {
let newHeight = canvas.height - (padding * 2);
img.width = Math.round((img.width / img.height) * newHeight);
img.height = newHeight;
}
}
ctx.drawImage(img, canvas.width / 2 - img.width / 2, canvas.height / 2 - img.height / 2, img.width, img.height);
};
img.src = imgUrl;
}
I'm just not sure how to convert that to here:
var uploadedImage = new Konva.Image({
x: stage.getWidth() / 2,
y: stage.getHeight() / 2,
width: 200,
height: 200,
...
How do I do this?