I started re-writing Windows! The code below draws 10 canvas objects you can move around. I made them random sizes. When you click on one it has thicker outline and can be dragged around.
When I added one line to change the initial top position of the canvases the move stopped working. Its very odd, I only wanted to stack the windows in increasing 10px steps rather then them all starting at the same spot. Why could this minor change stop the rest working?
var n = 0,
canv = [],
ct = []
var body = document.getElementsByTagName("body")[0];
var targ, wind = 10,
i, offx, offy = 0
for (n = 0; n < wind; n++) {
canv[n] = document.createElement('canvas');
canv[n].id = "C" + n;
canv[n].width = rnd(30 * n);
canv[n].height = rnd(30 * n);
//canv[n].style.top=10*n <- This line stops the drag effect working
canv[n].style.zIndex = n;
canv[n].style.position = "absolute";
canv[n].style.border = "2px solid";
body.appendChild(canv[n]);
targ=canv[0] // added now to stop errors before first click
ct[n] = canv[n].getContext("2d");
ct[n].fillText(n, canv[n].width / 2, canv[n].height / 2)
canv[n].addEventListener('mousedown', function(e) {
targ = e.currentTarget
if (targ.style.border == "2px solid") {
for (i = 0; i < wind; i++) {
canv[i].style.border = "2px solid"
}
targ.style.border = "5px solid";
offy = e.y - targ.style.top
} else {
targ.style.border = "2px solid"
}
});
canv[n].addEventListener('mousemove', function(e) {
if (targ.style.border == "5px solid") {
move(e.x, e.y)
}
});
}
function move(x, y) {
targ.style.top = y - offy
}
function rnd(m) {
return Math.floor(Math.random() * m)
}