0

苏……几乎是个菜鸟,可怜我任何明显的错误。

我正在尝试使用 Raphaël 创建一些可点击(或不可点击)的形状。

但是,由于我希望在实际渲染方式上保持灵活,我不希望它是包含 raphael 形状的对象,而不仅仅是一个 raphael 形状的数组。

基本上试图将绘图与对象属性分开。

我在下面的非工作尝试:

var Box = function (where, type, id) {
    // This is the object containing rendering info

    this.id = id

    this.posx = coords.topLeft.x;
    this.posy = coords.topLeft.y;

    this.width = type.w; // length 
    this.height = type.h; // height 

    this.color = baseColors.black;
    this.line = baseColors.white;

    this.paper = where;

    this.raphShp = this.drawMe()
    this.clickMe()  
}

Box.prototype.drawMe = function () {
    // Function to be called for drawing the shape 
    var box = this.paper.rect(this.posx, this.posy, this.width, this.height);
    box.attr({fill: this.color,stroke: this.line,'stroke-width': '1','stroke-opacity': '1'}).data('name', 'box' + this.id);

    // trying to pass the raphael shape back to the object
    return this.paper.getById(box.id)
}

Box.prototype.clickMe = function () {
    // trying to add clickable property to the shape
        this.raphShp.click(function(evt) {
            this.rotate(45);
            console.log('rotate')
        }); 
}

在 drawMe 中添加 clickable 属性当然可以。但我希望能够单独完成。

我错过了什么令人痛苦的显而易见的事情?

4

1 回答 1

0

这段代码对我有用:http: //jsfiddle.net/89dL5/2/

不要忘记实例化您的对象:

var b = new Box(Raphael(100, 100, 500, 500), [etc], [etc]);

您可能在代码的其他地方做错了什么。

于 2013-04-06T16:06:43.733 回答