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:
var rect = new Path.Rectangle(new Point(0,0), new Size(50,50);
misses a final parenthesisrect.fillColor = "yellow;
misses a final quoteHere 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.