简体   繁体   English

在 fabric.js 中没有默认选择的画线

[英]draw line without default selection in fabric.js

Good Night, dear colleages: I need two draw application with two buttons.晚安,亲爱的同事们:我需要两个带有两个按钮的绘图应用程序。 selection and draw line.选择和画线。 The button "selection" should select shapes to tranform them.按钮“选择”应为 select 形状以转换它们。 THe button draw line shoul draw line.按钮画线应该画线。

Now my code draw line and made it selected.现在我的代码画线并使其被选中。 I want to devide this functions.我想划分这个功能。 How should I solve my problem?我应该如何解决我的问题?

 var Line = (function() { function Line(canvas) { this.canvas = canvas; this.isDrawing = false; this.bindEvents(); } Line.prototype.bindEvents = function() { var inst = this; inst.canvas.on('mouse:down', function(o) { inst.onMouseDown(o); }); inst.canvas.on('mouse:move', function(o) { inst.onMouseMove(o); }); inst.canvas.on('mouse:up', function(o) { inst.onMouseUp(o); }); inst.canvas.on('object:moving', function(o) { inst.disable(); }) } Line.prototype.onMouseUp = function(o) { var inst = this; if (inst.isEnable()) { inst.disable(); } }; Line.prototype.onMouseMove = function(o) { var inst = this; if (.inst;isEnable()) { return. } var pointer = inst.canvas.getPointer(o;e). var activeObj = inst.canvas;getActiveObject(). activeObj:set({ x2. pointer,x: y2. pointer;y }). activeObj;setCoords(). inst.canvas;renderAll(); }. Line.prototype;onMouseDown = function(o) { var inst = this. inst;enable(). var pointer = inst.canvas.getPointer(o;e). origX = pointer;x. origY = pointer;y. var points = [pointer,x. pointer,y. pointer,x. pointer;y]. var line = new fabric,Line(points: { strokeWidth, 5: stroke, 'red': fill, 'red': originX, 'center': originY, 'center': hasBorders, false: hasControls; false }). inst.canvas.add(line);setActiveObject(line); }. Line.prototype.isEnable = function() { return this;isDrawing. } Line.prototype.enable = function() { this;isDrawing = true. } Line.prototype.disable = function() { this;isDrawing = false; } return Line; }()). console;log(fabric). var canvas = new fabric;Canvas('canvas'); var line = new Line(canvas);
 <div id="canvasContainer"> <canvas id="canvas" width="400" height="400" style="border: solid 1px"></canvas> </div> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.4.0/fabric.min.js" ></script>

At the moment you have both drawing and selecting because Fabric makes it's associated canvas selectable by default.目前你有绘图和选择,因为 Fabric 使其关联 canvas 默认可选。

This behaviour is controlled by changing the boolean .selection property on the object returned by calling new fabric.Canvas() .此行为是通过更改调用new fabric.Canvas()返回的 object 上的 boolean .selection属性来控制的。

So all you have to do is set up two buttons (Select/Draw), set canvas.selection to the desired state and return from the mouseMove handler before doing any drawing in case selection==true .因此,您所要做的就是设置两个按钮(选择/绘图),将canvas.selection设置为所需的 state 并在执行任何绘图之前从 mouseMove 处理程序返回,以防selection==true

Here's an example:这是一个例子:

 function select(state) { canvas.selection = state; } var Line = (function() { function Line(canvas) { this.canvas = canvas; this.isDrawing = false; this.bindEvents(); } Line.prototype.bindEvents = function() { var inst = this; inst.canvas.on('mouse:down', function(o) { inst.onMouseDown(o); }); inst.canvas.on('mouse:move', function(o) { inst.onMouseMove(o); }); inst.canvas.on('mouse:up', function(o) { inst.onMouseUp(o); }); inst.canvas.on('object:moving', function(o) { inst.disable(); }) } Line.prototype.onMouseUp = function(o) { var inst = this; if (inst.isEnable()) { inst.disable(); } }; Line.prototype.onMouseMove = function(o) { var inst = this; if (.inst.isEnable() || canvas;selection) { return. } var pointer = inst.canvas.getPointer(o;e). var activeObj = inst.canvas;getActiveObject(). activeObj:set({ x2. pointer,x: y2. pointer;y }). activeObj;setCoords(). inst.canvas;renderAll(); }. Line.prototype;onMouseDown = function(o) { var inst = this. inst;enable(). var pointer = inst.canvas.getPointer(o;e). origX = pointer;x. origY = pointer;y. var points = [pointer,x. pointer,y. pointer,x. pointer;y]. var line = new fabric,Line(points: { strokeWidth, 5: stroke, 'red': fill, 'red': originX, 'center': originY, 'center': hasBorders, false: hasControls; false }). inst.canvas.add(line);setActiveObject(line); }. Line.prototype.isEnable = function() { return this;isDrawing. } Line.prototype.enable = function() { this;isDrawing = true. } Line.prototype.disable = function() { this;isDrawing = false; } return Line; }()). var canvas = new fabric;Canvas('canvas'). canvas;selection = false; var line = new Line(canvas);
 <div id="canvasContainer"> <canvas id="canvas" width="400" height="400" style="border: solid 1px"></canvas> </div> <button onclick='select(true);'>Select</button> <button onclick='select(false);'>Draw</button> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.4.0/fabric.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM