Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

javascript - scale image by scaling rectangle without group fabric.js

I am new in fabric.js and want to achieve one functionality I added image and one rectangle same size of image, now I want to scale image while scaling rectangle. I don't want to group objects , image should scale by scaling rectangle with previous left, top position of image

below is my code

var imgURL = 'http://i.imgur.com/8rmMZI3.jpg';

var canvas = new fabric.Canvas('canvas');
var pug;
var pugImg = new Image();
pugImg.onload = function (img) {    
    pug = new fabric.Image(pugImg, {
        angle: 0,
        width: 500,
        height: 500,
        left: 50,
        top: 70,
        scaleX: .25,
        scaleY: .25,
        selectable: false
    });
    var rect = new fabric.Rect({
      top: 65,
      left: 200,
      width: 150,
      height: 150,
      fill: 'red',
    });
    canvas.sendToBack(rect);
    canvas.bringToFront(pug);
    
    canvas.add(rect);
    canvas.add(pug);
    canvas.renderAll();
    
};
pugImg.src = imgURL; 
canvas.on('object:scaling', function(e) {
  var bounds = e.target.getBoundingRect();
  pug.set({
    width: bounds.width,
    height: bounds.height,
    top: bounds.top,
    left: bounds.left
  })
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/4.2.0/fabric.js"></script>
<canvas id="canvas" width=400 height=400></canvas>
question from:https://stackoverflow.com/questions/65950627/scale-image-by-scaling-rectangle-without-group-fabric-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I added image and one rectangle same size of image

In your example the rectangle is not the same size as the image

rect = {
  width: 150,
  height: 150,
  scaleX: 1,
  scaleY: 1,
}
pug = {
    width: 500,
    height: 500,
    scaleX: .25,
    scaleY: .25
});

To achieve the desired effect, you need to give theme the same (width, height, scaleX, scaleY), then just copy the rect (scaleX, scaleY) to the pug. otherwise if they're not the same size you'll need to compute the scale ratio between the two.

var imgURL = 'http://i.imgur.com/8rmMZI3.jpg';

var canvas = new fabric.Canvas('canvas');
var pug, rect;
var pugImg = new Image();
pugImg.onload = function (img) {    
    pug = new fabric.Image(pugImg, {
        angle: 0,
        width: 500,
        height: 500,
        left: 50,
        top: 70,
        scaleX: .25,
        scaleY: .25,
        selectable: false
    });
    rect = new fabric.Rect({
      top: 65,
      left: 200,
      width: 500,
      height: 500,
      scaleX: .25,
      scaleY: .25,
      fill: 'red',
    });
    canvas.sendToBack(rect);
    canvas.bringToFront(pug);
    
    canvas.add(rect);
    canvas.add(pug);
    canvas.renderAll();
    
};
pugImg.src = imgURL; 
canvas.on('object:scaling', function(e) {
  pug.set({
    scaleX: rect.scaleX,
    scaleY: rect.scaleY,
  })
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/4.2.0/fabric.js"></script>
<canvas id="canvas" width=400 height=400></canvas>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...