繁体   English   中英

在 Paper.js 中使用链表存储矩形

[英]Using linked list to store rectangles in Paper.js

我想使用链表在 Paper.js 中存储矩形。 但是,当我尝试在 canvas 中定义自己的链表 class 时,似乎出现了问题。 当我运行下面的代码时,我得到一个空白的 canvas,而我希望 canvas 的左上角有一个黄色矩形。 如果我注释掉 class 定义和最后两行,我得到黄色矩形:

class LinkedList{
constructor(){
    this.head = null;
    this.tail = null;
    this.length = 0;
}
unshift(val){
    var newSegment = new Section(val);
    if(!this.head){
        this.head = newSegment;
        this.tail = this.head;
    } else{
        newSegment.next = this.head;
        this.head = newSegment;
    }
    this.length++;
    return this;
}
}

class Section{
constructor(val){
    this.val = val;
    this.next = null;
}
}

var rect = new Path.Rectangle(new Point(0,0), new Size(50,50);
rect.fillColor = "yellow;
rect.strokeColor = "black";
var list = new LinkedList();
list.unshift(rect);

任何解决此问题的帮助表示赞赏!

您的代码中有错别字:

  • 这一行var rect = new Path.Rectangle(new Point(0,0), new Size(50,50);最后一个括号
  • 这一行rect.fillColor = "yellow;错过了最后的报价

这是更正后的代码:

class LinkedList {
    constructor() {
        this.head = null;
        this.tail = null;
        this.length = 0;
    }

    unshift(val) {
        var newSegment = new Section(val);
        if (!this.head) {
            this.head = newSegment;
            this.tail = this.head;
        } else {
            newSegment.next = this.head;
            this.head = newSegment;
        }
        this.length++;
        return this;
    }
}

class Section {
    constructor(val) {
        this.val = val;
        this.next = null;
    }
}

var rect = new Path.Rectangle(new Point(0, 0), new Size(50, 50));
rect.fillColor = 'yellow';
rect.strokeColor = 'black';
var list = new LinkedList();
list.unshift(rect);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM