Emulate free drawing with fabricjs(使用 fabricjs 模拟自由绘图)
问题描述
我正在开发具有白板的应用程序.白板应该像 idroo.com 一样工作.一个用户正在绘制一些其他用户应该能够在他的浏览器上实时看到的东西.我使用fabricjs 作为画布包装器,它有我需要的一切.但我无法在画布上模拟自由绘图.我将鼠标位置和画笔选项发送到远程客户端,并尝试通过触发鼠标移动事件来呈现它们.但它不起作用.如果有人有类似的问题,你能帮忙吗?
I am developing application which has whiteboard. Whiteboard should work like idroo.com. One user is drawing something another user should be able to see it on his browser in real time. I use fabricjs as canvas wrapper and it has everithing I need. But I can't emulate free drawing on canvas. I send mouse position and brush options to remote client and try to render them by firing mouse move events. But it don't work. If some body has simialiar problem can you please help?
canvasContainer.on('mousemove', function (e) {
var left = canvasContainer.offset().left;
var top = canvasContainer.offset().top;
var x = e.pageX - left;
var y = e.pageY - top;
//Send data to remote browser by socket.io or signalr
//I need to draw on remote browser by these x and y coordinates.
updateCursor(_connections, x, y);
});
推荐答案
你可以这样做:
var brush = new fabric.PencilBrush(canvas);
var points = [[10,10], [20,20], [25,70],[100,300]];
brush.onMouseDown({x:points[0][0], y:points[0][1]});
for(var i=1;i<points.length;i++) {
brush.onMouseMove({x:points[i][0], y:points[i][1]});
}
在此处查看插件:https://plnkr.co/edit/V1c1xkB29tgB2ha99CRQ?p=preview
您可以简化计算 x 和坐标的代码,并像这样:
You can simplify your code for calculating x and coordinates and do it like this:
var point = canvas.getPointer(e);
这篇关于使用 fabricjs 模拟自由绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 fabricjs 模拟自由绘图


- addEventListener 在 IE 11 中不起作用 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 400或500级别的HTTP响应 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01