Fabricjs: Show horizontal and vertical position line on drag

痴心易碎 提交于 2019-12-23 02:58:29

问题


I am using fabricjs 1.5 and I am stuck on on thing. I want to show the dotted lines horizontally and vertically whenever I start to drag my object. Purpose of these lines is to give some idea to the user that where is the object with respect to the canvas and other objects. I have searched on many websites and could not find any solution or even any reference related to this problem and hence I could not start anything.

It would be great help if someone can put me in right direction. Thanks.


回答1:


centering_guidelines, you can use this library to draw center lines. aligning_guidelines this for drawing lines between objects.

DEMO

var canvas = new fabric.Canvas('canvas');
initCenteringGuidelines(canvas);
initAligningGuidelines(canvas);
var rect = new fabric.Rect({
  width: 100,
  height: 100,
  fill: 'green',
  originX:'center',
  originY:'center',
  left:canvas.width/2,
  top:canvas.height/2,
});
var rect2 = new fabric.Rect({
  width: 100,
  height: 100,
  fill: 'green',
  originX:'center',
  originY:'center',
  left:canvas.width/2,
  top:canvas.height/2,
});
canvas.add(rect,rect2);
canvas {
  border: 1px solid #999;
  margin-top: 10px;
}
<script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.js"></script>
<script src="https://rawgit.com/fabricjs/fabric.js/master/lib/centering_guidelines.js"></script>
<script src="https://rawgit.com/fabricjs/fabric.js/master/lib/aligning_guidelines.js"></script>
<canvas id="canvas" width="400" height="400"></canvas>



回答2:


I found a library which points us to the right direction. You can tweak the code to get exactly what you looking for here. It helped me and fixed almost what I was looking for.

I am posting the answer if someone else ends up on my question.

https://github.com/fabricjs/fabric.js/blob/master/lib/aligning_guidelines.js

Hope it helps someone.



来源:https://stackoverflow.com/questions/51184360/fabricjs-show-horizontal-and-vertical-position-line-on-drag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!