// Author: Jidong Xiao
// first we need to create a stage
var stage = new Konva.Stage({
container: 'container', // id of container
width: 2000,
height: 1000
});
// then create layer
var layer = new Konva.Layer();
// add the layer to the stage
stage.add(layer);
// the rectangle which contains the vector.
var rect2 = new Konva.Rect({
x: 70,
y: 360,
id:"code_rec1",
stroke: '#555',
strokeWidth: 5,
fill: '#ddd',
width: 650,
height: 350,
shadowColor: 'black',
shadowBlur: 10,
shadowOffsetX: 10,
shadowOffsetY: 10,
shadowOpacity: 0.2,
cornerRadius: 10,
});
layer.add(rect2);
// the rectangle which contains the list.
var rect3 = new Konva.Rect({
x: 850,
y: 360,
id:"code_rec1",
stroke: '#555',
strokeWidth: 5,
fill: '#ddd',
width: 900,
height: 300,
shadowColor: 'black',
shadowBlur: 10,
shadowOffsetX: 10,
shadowOffsetY: 10,
shadowOpacity: 0.2,
cornerRadius: 10,
});
layer.add(rect3);
// Create a function to draw vector nodes
function drawVecNode(x, y, label, id){
// the rectangle represents list nodes.
let rect1 = new Konva.Rect({
x: x,
y: y,
id:"node_rec_" + id,
stroke: '#555',
strokeWidth: 5,
fill: '#ddd',
width: 100,
height: 50,
shadowColor: 'black',
shadowBlur: 10,
shadowOffsetX: 10,
shadowOffsetY: 10,
shadowOpacity: 0.2,
cornerRadius: 10,
});
layer.add(rect1);
var text = new Konva.Text({
x: x + 45,
y: y + 20,
text: label,
id: "node_text_" + id,
fontSize: 20,
fontFamily: 'Calibri',
fill: 'black'
});
layer.add(text);
var text1 = new Konva.Text({
x: x + 45,
y: y + 60,
text: id - 1,
id: "node_index_" + id,
fontSize: 20,
fontFamily: 'Calibri',
fill: 'black'
});
layer.add(text1);
}
drawVecNode(100, 460, '7', 1);
drawVecNode(200, 460, '5', 2);
drawVecNode(300, 460, '8', 3);
drawVecNode(400, 460, '1', 4);
drawVecNode(500, 460, '9', 5);
// Create a function to draw list nodes
function drawListNode(x, y, label, id){
// the rectangle represents list nodes.
let rect1 = new Konva.Rect({
x: x,
y: y,
id:"list_node_rec_" + id,
stroke: '#555',
strokeWidth: 5,
fill: '#ddd',
width: 100,
height: 50,
});
layer.add(rect1);
let rect2 = new Konva.Rect({
x: x+100,
y: y,
id:"list_node_pointer_rec_" + id,
stroke: '#555',
strokeWidth: 5,
fill: '#ddd',
width: 20,
height: 50,
});
layer.add(rect2);
var text = new Konva.Text({
x: x + 45,
y: y + 20,
text: label,
id: "list_node_text_" + id,
fontSize: 20,
fontFamily: 'Calibri',
fill: 'black'
});
layer.add(text);
var arrow = new Konva.Arrow({
points: [x+110, y+25, x+150, y+25],
tension: 0.5,
pointerLength: 10,
pointerWidth: 10,
id: "list_arrow_" + id,
fill: 'green',
stroke: 'green',
strokeWidth: 5,
});
layer.add(arrow);
}
drawListNode(900, 460, '7', 1);
drawListNode(1050, 460, '5', 2);
drawListNode(1200, 460, '8', 3);
drawListNode(1350, 460, '1', 4);
drawListNode(1500, 460, '9', 4);
var text_null = new Konva.Text({
x: 1655,
y: 475,
text: "NULL",
id: "node_text_null",
fontSize: 20,
fontFamily: 'Calibri',
fill: 'black'
});
layer.add(text_null);
var text_itr = new Konva.Text({
x: 235,
y: 640,
text: "itr",
id: 'vec_itr_1',
fontSize: 26,
fontFamily: 'Calibri',
fill: '#17202a'
});
var itr_arrow = new Konva.Arrow({
points: [250, 625, 250, 555],
tension: 0.5,
pointerLength: 10,
pointerWidth: 10,
id: 'itr_arrow_1',
fill: 'green',
stroke: 'green',
strokeWidth: 5,
});
var pts = itr_arrow.points();
var text_itr2 = new Konva.Text({
x: 235,
y: 640,
text: "itr2",
id: 'vec_itr_2',
fontSize: 26,
fontFamily: 'Calibri',
fill: '#17202a'
});
var itr2_arrow = new Konva.Arrow({
points: [250, 625, 250, 555],
tension: 0.5,
pointerLength: 10,
pointerWidth: 10,
id: 'itr_arrow_2',
fill: 'yellow',
stroke: 'yellow',
strokeWidth: 5,
});
var pts2 = itr2_arrow.points();
var text_p = new Konva.Text({
x: 1100,
y: 610,
text: "p",
id: 'list_itr_p',
fontSize: 26,
fontFamily: 'Calibri',
fill: '#17202a'
});
var arrow_p = new Konva.Arrow({
points: [1100, 605, 1100, 535],
tension: 0.5,
pointerLength: 10,
pointerWidth: 10,
id: 'arrow_p',
fill: 'yellow',
stroke: 'yellow',
strokeWidth: 5,
});
// the code rectangle.
var rect1 = new Konva.Rect({
x: 70,
y: 60,
id:"code_rec1",
stroke: '#555',
strokeWidth: 5,
fill: '#ddd',
width: 650,
height: 250,
shadowColor: 'black',
shadowBlur: 10,
shadowOffsetX: 10,
shadowOffsetY: 10,
shadowOpacity: 0.2,
cornerRadius: 10,
});
layer.add(rect1);
// the code rectangle.
var rect_list = new Konva.Rect({
x: 850,
y: 60,
id:"code_rec_list",
stroke: '#555',
strokeWidth: 5,
fill: '#ddd',
width: 650,
height: 250,
shadowColor: 'black',
shadowBlur: 10,
shadowOffsetX: 10,
shadowOffsetY: 10,
shadowOpacity: 0.2,
cornerRadius: 10,
});
layer.add(rect_list);
function makeCode(x,y,str,id) {
return new Konva.Text({
x: x,
y: y,
text: str,
id: 'line'+id,
fontSize: 18,
fontFamily: 'Calibri',
fill: '#000000',
width: 650,
padding: 20,
});
}
var code=[
"0. void erase_from_vector(vector::iterator itr, vector& v) {",
"1. \t\t\t vector::iterator itr2 = itr;",
"2. \t\t\t itr2++;",
"3. \t\t\t while (itr2 != v.end()) {",
"4. \t\t\t \t\t\t *itr = *itr2;",
"5. \t\t\t \t\t\t itr++;",
"6. \t\t\t \t\t\t itr2++;",
"7. \t\t\t }",
"8. \t\t\t v.pop_back();",
"9. }",
];
function makeBold(id){
for(let i=1; i<=code.length; i++){
if(i!=id){
stage.find('#line'+i).fontStyle('normal');
}else{
stage.find('#line'+id).fontStyle('bold');
}
}
}
// write the code fragment into the code box.
for (let i=0;i s;",
"1. std::list::iterator p; // code initializing p is omitted",
"2. p = s.erase(p);",
];
function makeBold_list(id){
for(let i=1; i<=code_list.length; i++){
if(i!=id){
// regarding the parentheses, we must add the two numbers first.
stage.find('#line'+(i+100)).fontStyle('normal');
}else{
stage.find('#line'+(i+100)).fontStyle('bold');
}
}
}
// write the code fragment into the code box.
for (let i=0;i