在 Fabric.js 中渲染多个图形以获取圆柱体

发布于 2025-01-14 17:40:54 字数 2452 浏览 2 评论 0原文

亲爱的同学们!

我需要在 Fabric.js 中获取圆柱体。应该是一种形状。我尝试以这种方式解决问题。首先,我画两个椭圆和两条直线。您可以在文本下方看到这项工作的代码:

var canvas = new fabric.Canvas('c');
let width_changed = 4;


var eli_1 = new fabric.Ellipse({
    top: 150,
    left: 400,
   /* Try same values rx, ry => circle */
    rx: 75,
    ry: 50,
    fill: '',
    stroke: 'blue',
    strokeWidth: width_changed
});

var eli_2 = new fabric.Ellipse({
    top: 350,
    left: 400,
   /* Try same values rx, ry => circle */
    rx: 75,
    ry: 50,
    fill: '',
    stroke: 'blue',
    strokeWidth: width_changed
});



var line_2 = new fabric.Line([ 402, 200, 402, 400 ],{
      strokeWidth: width_changed,
      fill: 'blue',
      stroke: 'blue',
      originX: 'center',
      originY: 'center'
    });

var line_3 = new fabric.Line([ 552, 200, 552, 400 ],{
      strokeWidth: width_changed,
      fill: 'blue',
      stroke: 'blue',
      originX: 'center',
      originY: 'center'
    });

var shape_1 = new Array(eli_1,eli_2,line_2,line_3);
/*
var polyg_1 = new fabric.Polygon(shape_1[1], {
    top: 280,
    left: 290,
    fill: '',
    stroke: 'blue',
    strokeWidth: 2
});
*/

canvas.add(eli_1,eli_2,line_2,line_3);
//canvas.add(polyg_1);
canvas.renderAll();
<canvas id="c" width="800" height="630" style="border:1px dotted red"></canvas>

<script src="https://cdn.socket.io/4.4.0/socket.io.min.js" integrity="sha384-1fOn6VtTq3PWwfsOrk45LnYcGosJwzMHv+Xh/Jx5303FVOXzEnw0EpLv30mtjmlj" crossorigin="anonymous"></script>

<script src="https://unpkg.com/[email protected]/dist/fabric.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
as a result I want to get something like on figure.It should be one shape

输入图片这里的描述

当我尝试在一个形状中添加 4 个数字时:

var shape_1 = new Array(eli_1,eli_2,line_2,line_3);
/*
var polyg_1 = new fabric.Polygon(shape_1[1], {
    top: 280,
    left: 290,
    fill: '',
    stroke: 'blue',
    strokeWidth: 2
});

出现错误。我应该如何解决我的问题?

dear colleages!

I need to get cylinder in fabric.js. It should be one shape. I try to solve th problem in that way. First of all I draw two ellipses and two lines. The code of this work you can see below the text:

var canvas = new fabric.Canvas('c');
let width_changed = 4;


var eli_1 = new fabric.Ellipse({
    top: 150,
    left: 400,
   /* Try same values rx, ry => circle */
    rx: 75,
    ry: 50,
    fill: '',
    stroke: 'blue',
    strokeWidth: width_changed
});

var eli_2 = new fabric.Ellipse({
    top: 350,
    left: 400,
   /* Try same values rx, ry => circle */
    rx: 75,
    ry: 50,
    fill: '',
    stroke: 'blue',
    strokeWidth: width_changed
});



var line_2 = new fabric.Line([ 402, 200, 402, 400 ],{
      strokeWidth: width_changed,
      fill: 'blue',
      stroke: 'blue',
      originX: 'center',
      originY: 'center'
    });

var line_3 = new fabric.Line([ 552, 200, 552, 400 ],{
      strokeWidth: width_changed,
      fill: 'blue',
      stroke: 'blue',
      originX: 'center',
      originY: 'center'
    });

var shape_1 = new Array(eli_1,eli_2,line_2,line_3);
/*
var polyg_1 = new fabric.Polygon(shape_1[1], {
    top: 280,
    left: 290,
    fill: '',
    stroke: 'blue',
    strokeWidth: 2
});
*/

canvas.add(eli_1,eli_2,line_2,line_3);
//canvas.add(polyg_1);
canvas.renderAll();
<canvas id="c" width="800" height="630" style="border:1px dotted red"></canvas>

<script src="https://cdn.socket.io/4.4.0/socket.io.min.js" integrity="sha384-1fOn6VtTq3PWwfsOrk45LnYcGosJwzMHv+Xh/Jx5303FVOXzEnw0EpLv30mtjmlj" crossorigin="anonymous"></script>

<script src="https://unpkg.com/[email protected]/dist/fabric.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

as a result I want to get something like on figure.It should be one shape

enter image description here

when I try to add 4 figures in one shape:

var shape_1 = new Array(eli_1,eli_2,line_2,line_3);
/*
var polyg_1 = new fabric.Polygon(shape_1[1], {
    top: 280,
    left: 290,
    fill: '',
    stroke: 'blue',
    strokeWidth: 2
});

I get an error. How should I solve my problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倦话 2025-01-21 17:40:54
var canvas = new fabric.Canvas('c');
let width_changed = 4;


var eli_1 = new fabric.Ellipse({
    top: 150,
    left: 400,
   /* Try same values rx, ry => circle */
    rx: 75,
    ry: 50,
    fill: '',
    stroke: 'blue',
    strokeWidth: width_changed
});

var eli_2 = new fabric.Ellipse({
    top: 350,
    left: 400,
   /* Try same values rx, ry => circle */
    rx: 75,
    ry: 50,
    fill: '',
    stroke: 'blue',
    strokeWidth: width_changed
});



var line_2 = new fabric.Line([ 402, 200, 402, 400 ],{
      strokeWidth: width_changed,
      fill: 'blue',
      stroke: 'blue',
      originX: 'center',
      originY: 'center'
    });

var line_3 = new fabric.Line([ 552, 200, 552, 400 ],{
      strokeWidth: width_changed,
      fill: 'blue',
      stroke: 'blue',
      originX: 'center',
      originY: 'center'
    });

var group2 = new fabric.Group([ eli_1,eli_2,line_2,line_3 ], { left: 200, top: 200 });
canvas.add(group2);
canvas.renderAll();
<canvas id="c" width="800" height="630" style="border:1px dotted red"></canvas>

<script src="https://cdn.socket.io/4.4.0/socket.io.min.js" integrity="sha384-1fOn6VtTq3PWwfsOrk45LnYcGosJwzMHv+Xh/Jx5303FVOXzEnw0EpLv30mtjmlj" crossorigin="anonymous"></script>

<script src="https://unpkg.com/[email protected]/dist/fabric.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

var canvas = new fabric.Canvas('c');
let width_changed = 4;


var eli_1 = new fabric.Ellipse({
    top: 150,
    left: 400,
   /* Try same values rx, ry => circle */
    rx: 75,
    ry: 50,
    fill: '',
    stroke: 'blue',
    strokeWidth: width_changed
});

var eli_2 = new fabric.Ellipse({
    top: 350,
    left: 400,
   /* Try same values rx, ry => circle */
    rx: 75,
    ry: 50,
    fill: '',
    stroke: 'blue',
    strokeWidth: width_changed
});



var line_2 = new fabric.Line([ 402, 200, 402, 400 ],{
      strokeWidth: width_changed,
      fill: 'blue',
      stroke: 'blue',
      originX: 'center',
      originY: 'center'
    });

var line_3 = new fabric.Line([ 552, 200, 552, 400 ],{
      strokeWidth: width_changed,
      fill: 'blue',
      stroke: 'blue',
      originX: 'center',
      originY: 'center'
    });

var group2 = new fabric.Group([ eli_1,eli_2,line_2,line_3 ], { left: 200, top: 200 });
canvas.add(group2);
canvas.renderAll();
<canvas id="c" width="800" height="630" style="border:1px dotted red"></canvas>

<script src="https://cdn.socket.io/4.4.0/socket.io.min.js" integrity="sha384-1fOn6VtTq3PWwfsOrk45LnYcGosJwzMHv+Xh/Jx5303FVOXzEnw0EpLv30mtjmlj" crossorigin="anonymous"></script>

<script src="https://unpkg.com/[email protected]/dist/fabric.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文