简体   繁体   English

Fabric.js-将对象创建附加到鼠标单击

[英]Fabric.js - Attach object creation to mouse click

When you create a new fabric object, you can specify the location for it to appear on the canvas. 创建新的结构对象时,可以指定其在画布上显示的位置。 Is there a way to attach the generated object to the mouse and then place the object on click (or touch)? 有没有一种方法可以将生成的对象附加到鼠标,然后将其放置在单击(或触摸)上?

Eg way to generate a circle which appears on the canvas. 例如,生成在画布上显示的圆圈的方法。

var circle = new fabric.Circle({
  radius: 20, fill: 'green', left: 100, top: 100
});

It's fairly easy to update the position of an object to match that of the mouse's position and on mouse:up clone that object and place it on the canvas. 更新对象的位置以使其与鼠标的位置相匹配是很容易的,并且在mouse:up克隆该对象并将其放置在画布上。

 var canvas = new fabric.Canvas('c'); var mousecursor = new fabric.Circle({ left: 0, top: 0, radius: 5, fill: '#9f9', originX: 'right', originY: 'bottom', }) canvas.add(mousecursor); canvas.on('mouse:move', function(obj) { mousecursor.top = obj.ey - mousecursor.radius; mousecursor.left = obj.ex - mousecursor.radius; canvas.renderAll() }) canvas.on('mouse:out', function(obj) { // put circle off screen mousecursor.top = -100; mousecursor.left = -100; canvas.renderAll() }) canvas.on('mouse:up', function(obj) { canvas.add(mousecursor.clone()) canvas.renderAll() }) 
 canvas { border: 1px solid #ccc; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.6.3/fabric.js"></script> <canvas id="c" width="600" height="600"></canvas> 

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

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