在PaperJS中,似乎只能将常规类型添加到组中。每当我尝试向该组添加自定义对象时,我都会收到错误item._remove is not a function
例如,无论何时创建自己的对象,
function textBox(point, width) {
this.box = new Path.Rectangle({
point: point,
size: new Size(width,40),
strokeColor: new Color(0,0,0,0.1),
fillColor: 'white',
strokeWidth: 1
});
this.box.onMouseUp = function(event) {
cursor = this.point + new Point(5,2);
}
}
,然后尝试将其添加到组中:
var testGroup = new Group();
testGroup.appendTop(new textBox(new Point(0,0), 400));
显示错误。因此,我的问题是:如何将自定义对象添加到组?当然,我不能期望手动创建每个单独的对象,或者以其他方式在不使用组动态的情况下在单个级别上全部操作它们。似乎我必须像PaperJS中的其他所有类型一样,使我的对象扩展Item,但是到目前为止,我未能使它接受我的构造函数。我想知道要接受它需要什么。
答案 0 :(得分:2)
实际上,除了将它们与库一起编译外,目前没有内置机制来扩展Paper.js
类。
因此,对于像您似乎遇到的那种简单情况,我将使用一个工厂函数实例化我的自定义对象,然后像与其他任何Paper.js
对象一样与它进行交互。
例如,如果您的自定义对象是一个带有文本的框,则可以实例化一个带有矩形和文本的组,然后与该组进行交互。
这里是sketch演示解决方案。
function createMyTextBox(point, content) {
// Create a text item
var text = new PointText({
point: point,
content: content,
fontSize: 36,
fillColor: 'red',
justification: 'center'
});
// Create a rectangle around the text
var rectangle = new Path.Rectangle({
rectangle: text.bounds.scale(1.2),
strokeColor: 'black'
});
// Create a Group that will wrap our items.
var group = new Group([text, rectangle]);
// Return the group
return group;
}
// Create 2 text boxes
var textBox1 = createMyTextBox(view.center, 'text 1');
var textBox2 = createMyTextBox(view.center + [0, 100], 'text 2');
// You can use text boxes like regular items.
textBox2.translate(100, 0);
textBox2.scale(0.8);