5
I have a page that allows you to insert an image inside the canvas element and works perfectly.
Now, what I need is a way to limit the positioning of the image within the canvas, not allowing it to be left with white spaces, regardless of the positioning of the image.
The image selection option is enabled. Repositioning is done using drag and drop.
I need something like this from link, but without having to zoom in to move the image.
Canvas
<canvas id="c" height="300" width="200"></canvas>
Imagery
<img id="imagem" src="images/teste.jpg">
JS
var c = new fabric.Canvas('c');
var imageSrc = "images/teste.jpg";
fabric.Image.fromURL(imageSrc, function(img) {
img.scaleToWidth(c.getWidth());
img.scaleToHeight(c.getHeight());
img.set('selectable', true);
img.set('hasControls', false);
c.add(img);
});
I tried to run your code here and it wasn’t. Something’s missing?
– Stéfano
I’m using Fabric Js.
– renanvm
Do the images always have the same aspect ratio as the canvas? If not, how do you want to make the cuts if necessary?
– bfavaretto
No, images are usually larger than canvas. The idea is to allow the framing of the image within the canvas.
– renanvm