简体   繁体   中英

Using linked list to store rectangles in Paper.js

I want to use a linked list to store rectangles in Paper.js. However, there seems to be a problem when I try to define my own linked list class in the canvas. When I run the code below, I get a blank canvas, whereas I expect a single yellow rectangle in the top-left corner of my canvas. If I comment out the class definitions and the last 2 lines, I get the yellow rectangle:

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);

Any help to solve this problem is appreciated!

There are typos in your code:

  • this line var rect = new Path.Rectangle(new Point(0,0), new Size(50,50); misses a final parenthesis
  • this line rect.fillColor = "yellow; misses a final quote

Here is the corrected code:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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