如何在draw2d.js中创建复合图形

时间:2016-02-14 11:17:50

标签: javascript draw2d-js

我已经查看了与库一起使用的示例集合,但不幸的是,所有用于复合图形的示例都基于解析json文件。

reader.unmarshal(canvas, jsonDocument);

但是,我想看到的是如何从基本数据创建复合数字,如:

var figure_1 = new draw2d.shape.basic.Rectangle(....);
var figure_1 = new draw2d.shape.basic.Diamond(....);
... then do something, so that figure_1 and figure_2 are now part of
... a composite figure

PS。我认为我需要的是StrongComposite,但我不知道如何使用它。希望有人可以帮忙!

修改

这就是我的尝试:

var baseRectangle =  new draw2d.shape.composite.StrongComposite({width: 200, height: 200, x: conf.x, y: conf.y});
var top = new draw2d.shape.basic.Oval({width: 120, height: 40, x: conf.x, y: conf.y});
var bottom = new draw2d.shape.basic.Oval({width: 120, height: 40, x: conf.x, y: conf.y + 60});
baseRectangle.assignFigure(top);
baseRectangle.assignFigure(bottom);
canvas.add(baseRectangle);

但它不起作用。我只看到一个灰色的盒子。我也试过这个:

var baseRectangle =  new draw2d.shape.composite.StrongComposite({width: 200, height: 200, x: conf.x, y: conf.y});
var top = new draw2d.shape.basic.Oval({width: 120, height: 40, x: conf.x, y: conf.y});
var bottom = new draw2d.shape.basic.Oval({width: 120, height: 40, x: conf.x, y: conf.y + 60});
baseRectangle.assignFigure(top);
baseRectangle.assignFigure(bottom);
canvas.add(top);
canvas.add(bottom);

但结果,我得到了绝对独立的椭圆形。

1 个答案:

答案 0 :(得分:0)

您还必须将baseRectangle添加到画布。

var baseRectangle = new draw2d.shape.composite.StrongComposite({
    width: 200,
    height: 200,
    x: conf.x,
    y: conf.y
});
canvas.add(baseRectangle);

var top = new draw2d.shape.basic.Oval({
    width: 120,
    height: 40,
    x: conf.x,
    y: conf.y
});
var bottom = new draw2d.shape.basic.Oval({
    width: 120,
    height: 40,
    x: conf.x,
    y: conf.y + 60
});

baseRectangle.assignFigure(top);
baseRectangle.assignFigure(bottom);

canvas.add(top);
canvas.add(bottom);