在 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,但到目前为止我还未能让它接受我的构造函数。我想知道它需要什么才能被接受。
最佳答案
事实上,除了将 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);
关于javascript - 在 paperJS 中将自定义对象添加到 Group 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56604250/