我的画布有两个对象。第一个可选 true,第二个可选 false。我希望通过第一个对象的操作来管理第二个对象。
如果第一个对象移动,那么第二个对象需要沿相同方向移动。如果第一个旋转,则第二个在第二个位置旋转相同的角度。
我尝试将事件(移动、旋转)添加到第一个可选对象,然后从第一个对象获取所需的值并将其设置为第二个对象,但旋转无法正常工作。我的代码如下:
imageOne.on('rotating', function (evt) { imageThree.angle = imageOne.getAngle(); });
Run Code Online (Sandbox Code Playgroud)
var canvas = new fabric.Canvas('c');
var evented = false;
var rect1 = new fabric.Rect({
left: 50,
top: 60,
fill: 'blue',
width: 150,
height: 150,
});
var rect2 = new fabric.Rect({
left: 210,
top: 60,
fill: 'magenta',
width: 150,
height: 150,
selectable: false
});
canvas.add(rect1,rect2);
function rect1MouseDown(option){
this.mousesDownLeft = this.left;
this.mousesDownTop = this.top;
this.rect2Left = rect2.left;
this.rect2Top = rect2.top;
}
function rect1Move(option){
rect2.left = this.rect2Left+ this.left - this.mousesDownLeft ;
rect2.top = this.rect2Top+ this.top- this.mousesDownTop;
rect2.setCoords();
}
function rect1Rotating(options){
rect2.setAngle(this.angle);
}
register();
function register(){
if(evented) return;
rect1.on('moving', rect1Move);
rect1.on('mousedown', rect1MouseDown);
rect1.on('rotating', rect1Rotating);
evented = true;
}
function unRegister(){
rect1.off('moving');
rect1.off('mousedown');
rect2.on('rotating');
evented = false;
}Run Code Online (Sandbox Code Playgroud)
canvas {
border: blue dotted 2px;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.19/fabric.min.js"></script>
<button onclick='register()'>Register Event</button>
<button onclick='unRegister()'>Unregister Event</button><br>
<canvas id="c" width="400" height="400"></canvas>Run Code Online (Sandbox Code Playgroud)
您可以使用obj.setAngle()版本 <1.7.19 和obj.rotate()版本 2 来设置对象的角度。
| 归档时间: |
|
| 查看次数: |
1674 次 |
| 最近记录: |