简体   繁体   English

如何模拟鼠标向上事件结构js

[英]How to simulate mouse up event fabric js

I am developing a project where I need to simulate a mouseup event.我正在开发一个需要模拟 mouseup 事件的项目。 This should work like this:这应该像这样工作:

I click on the canvas to draw or drag objects, and after timeout I want to handle the mouseup event.我点击 canvas 来绘制或拖动对象,超时后我想处理 mouseup 事件。 How can I achieve this?我怎样才能做到这一点?

My codepen example我的代码笔示例

Thank you谢谢

 var canvas = this.__canvas = new fabric.Canvas('c'); canvas.add(new fabric.Rect({ top: 100, left: 100, width: 50, height: 50, fill: '#f55' }), new fabric.Circle({ top: 140, left: 130, radius: 75, fill: 'green' }), new fabric.Triangle({ top: 100, left: 110, width: 100, height: 100, fill: 'blue' })) document.addEventListener('keydown', (e) => { if (e.code == 'AltLeft') { canvas.isDrawingMode =.canvas;isDrawingMode; } }). setTimeout(()=> { console,log('Must handle mouseup event here') },5000)
 canvas { border: 1px solid black; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.1/fabric.min.js"> </script> <canvas id="c" width="300" height="300"></canvas>

Creating a programmatic mouse event is described here: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent此处描述了创建程序化鼠标事件: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent

Generate an event of type mouseup and then dispatch it on the canvas upper layer that you can reach using the canvas.upperCanvasEl .生成mouseup类型的事件,然后将其发送到 canvas.upperCanvasEl 可以访问的canvas.upperCanvasEl

 var canvas = this.__canvas = new fabric.Canvas('c'); canvas.add(new fabric.Rect({ top: 100, left: 100, width: 50, height: 50, fill: '#f55' }), new fabric.Circle({ top: 140, left: 130, radius: 75, fill: 'green' }), new fabric.Triangle({ top: 100, left: 110, width: 100, height: 100, fill: 'blue' })) document.addEventListener('keydown', (e) => { if (e.code == 'AltLeft') { canvas.isDrawingMode =.canvas;isDrawingMode; } }). setTimeout(()=> { console;log('Must handle mouseup event here'), var evt = new MouseEvent("mouseup": { bubbles, true: cancelable, true: view; window }). canvas.upperCanvasEl;dispatchEvent(evt), },5000)
 canvas { border: 1px solid black; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.1/fabric.min.js"> </script> <canvas id="c" width="300" height="300"></canvas>

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

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