Scalable Vector Graphics
<svg width="600" height="400" xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" fill="white"
stroke="black" stroke-width="5"/>
<rect x="100" y="100" width="100" height="100"
fill="blue" stroke="none"/>
<circle cx="400" cy="150" r="100" stroke="black"
stroke-width="5" fill="pink"/>
<polygon points="150 300 350 300 450 350 250 375"
stroke="black" stroke-width="5" fill="green"/>
<line x1="50" y1="50" x2="100" y2="350"
stroke="black" stroke-width="5"/>
</svg>
create elements
let circle = document.createElementNS(ns, 'circle');
set attributes
circle.setAttributeNS(null, "fill", "pink");
add elements
svg.appendChild(circle)
Goal: call a method (or methods) when user interacts with elements on the page
Examples:
click
on an elementmouseover
an elementmousemove
These are all Events in JS!
Call a method when an element is clicked:
// get the element you want to add the listener to
const box = document.querySelector("#dot-box");
// add the listener
box.addEventListener("click", drawDot);
"click"
is the name of the event we are listeningdrawDot
is the method that will get called when event occurs
Event
objectEvent
If e
is a (mouse) Event
, such as click
:
e.clientX
= x-coordinate of where the event occurede.clientY
= y-coordinate of where the event occurede.target
= element that “heard” the eventDraw dots on your SVG!
Draw other stuff as well!
Collection of
Example dot
class
cx
x position of centercy
y position of centerupdateLocation(cx, cy)
moves dot to a new locationIn JS, object types can be defined by defining a constructor
this
defines attributes and methodsBy convention, constructor names are Capitalized:
function Dot(cx, cy) {
this.cx = cx;
this.cy = cy;
this.circle = document.createElementNS(ns, 'circle');
this.circle.setAttributeNS(null, 'cx', this.cx);
this.circle.setAttributeNS(null, 'cy', this.cy);
this.circle.setAttributeNS(null, 'class', 'dot');
svg.appendChild(this.circle);
}
let someDot = new Dot(100,100);
let anotherDot = new Dot(200,200);
// refer to Dot fields
let x = someDot.cx; // x stores value 100
dots = []; // an array of dots
function makeDots() {
for(let i = 0; i < 10; i++) {
let x = Math.floor(600 * Math.random());
let y = Math.floor(400 * Math.random());
dots.push(new Dot(x, y));
}
}
You can include method definitions in the constructor as well!
function Dot(cx, cy) {
...
this.updateLocation = function (cx, cy) {
this.cx = cx;
this.cy = cy;
this.circle.setAttributeNS(null, 'cx', this.cx);
this.circle.setAttributeNS(null, 'cy', this.cy);
};
}
dot
s arounddots = [];
//...create dots...
function moveDots() {
for(let i = 0; i < 10; i++) {
let x = Math.floor(600 * Math.random());
let y = Math.floor(400 * Math.random());
dots[i].updateLocation(x, y);
}
}
Mathematical abstraction of networks
If $(u, v) \in E$, we say $u$ and $v$ are neighbors
Adjacency list representation
Example
Question. Suppose we want to write a JavaScript program to represent and manipulate graphs. What types of objects might we want to represent?
Graph
have?Vertex
have?Graph
sMy Goals:
What additional information/functionality should our Graph
s (and related objects) have to support visualization and user interaction?
Graph
object that stores vertices, edges, visualizerVertex
stores id, adjacency list, location, graphEdge
stores endpoints, idGraphVisualizer
stores graph, svg element, text field
Visualizing Graph Algorithms!