简体   繁体   English

Fabric js - 在画布悬停时自由绘图而不是单击/鼠标按下

[英]Fabric js - Freedrawing on hover of canvas instead of click/mouse down

When hovering on the canvas (Free drawing with Fabric js), I'd like to mimic the mouse down/click event and have the user create their drawing/paths.当悬停在画布上时(使用 Fabric js 自由绘图),我想模拟鼠标按下/点击事件并让用户创建他们的绘图/路径。

Would this at all possible?这可能吗?

Here is more information about working with events with Fabric - https://github.com/fabricjs/fabric.js/wiki/working-with-events#demos-and-examples以下是有关使用 Fabric 处理事件的更多信息 - https://github.com/fabricjs/fabric.js/wiki/working-with-events#demos-and-examples

I'm using Vuejs but totally open to anything to get to a solution!我正在使用 Vuejs,但对任何解决方案都持开放态度!

 var app = new Vue({ el: '#app', data(){ return {} }, mounted(){ var canvas = new fabric.Canvas('canvas'); canvas.isDrawingMode = true canvas.setHeight(window.innerHeight) canvas.setWidth(window.innerWidth) canvas.freeDrawingBrush.color = 'blue' canvas.freeDrawingBrush.width = 40 }, methods: {} })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.6/fabric.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <!-- Draw as if I've clicked BUT when hovering over canvas --> <canvas id="canvas"></canvas> </div>

Since event handlers related to isDrawingMode are triggered by mouse up and mouse down, one possible solution is to customize those handlers to make sure that:由于与isDrawingMode相关的事件处理程序由鼠标向上和向下触发,因此一种可能的解决方案是自定义这些处理程序以确保:

  • Default mouse down event handler is now triggered only once at the time first mouse move is detected;默认鼠标按下事件处理程序现在仅在检测到第一次鼠标移动时触发一次; mouse down handler is removed.鼠标按下处理程序被删除。
  • Mouse up handler is removed to ensure brushing line will not be interrupted.移除鼠标向上处理程序以确保刷牙线不会中断。

Here is the original code of fabric.js to handle those events: http://fabricjs.com/docs/fabric.js.html (line 11431 to 11472)这是处理这些事件的 fabric.js 的原始代码: http ://fabricjs.com/docs/fabric.js.html(第 11431 行到 11472 行)

Here is my sample这是我的样本

 var enableDrawingMode = false; var app = new Vue({ el: '#app', mounted(){ var canvas = new fabric.Canvas('canvas'); canvas.isDrawingMode = true canvas.setHeight(window.innerHeight) canvas.setWidth(window.innerWidth) // set up canvas drawing logic based on original mouse down event canvas._isCurrentlyDrawing = true; canvas. _onMouseDownInDrawingMode = function(e) { if (this.getActiveObject()) { this.discardActiveObject(e).requestRenderAll(); } if (this.clipTo) { fabric.util.clipContext(this, this.contextTop); } this._handleEvent(e, 'down'); } canvas._onMouseMoveInDrawingMode = function(e) { var pointer = this.getPointer(e); if (!enableDrawingMode) { console.log('enable original mouse move event only one') enableDrawingMode = true; this.freeDrawingBrush.onMouseDown(pointer); } this.freeDrawingBrush.onMouseMove(pointer); this.setCursor(this.freeDrawingCursor); this._handleEvent(e, 'move'); } canvas._onMouseUpInDrawingMode = function(e) { if (this.clipTo) { this.contextTop.restore(); } this._handleEvent(e, 'up'); } canvas.freeDrawingBrush.color = 'blue' canvas.freeDrawingBrush.width = 40 } })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.6/fabric.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <!-- Draw as if I've clicked BUT when hovering over canvas --> <canvas id="canvas"></canvas> </div>

with override functions to achieve drawing on mouse move.具有覆盖功能以实现鼠标移动绘图。

 let canvas = new fabric.Canvas('canvas'); canvas.isDrawingMode = true; canvas.on('mouse:move', function (event) { this._onMouseDownInDrawingMode(event); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/451/fabric.min.js"></script> <canvas id="canvas" style="border: 1px solid #000"></canvas>

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

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