Skip to content

Instantly share code, notes, and snippets.

@julescarbon
Created November 12, 2014 18:06
Show Gist options
  • Save julescarbon/9cb048fcf0134458a5ca to your computer and use it in GitHub Desktop.
Save julescarbon/9cb048fcf0134458a5ca to your computer and use it in GitHub Desktop.
Lasso Fill
Gfx.lassoFill = function(o, f, e, g) {
var m = function(D, H, w) {
var p = H[0];
var B = H[1];
var F = D * 4 + w;
var q = p[F + 0];
var v = p[F + 1];
var A = p[F + 2];
var G = ((q == f.r) && (v == f.g) && (A == f.b));
var q = B[F + 0];
var v = B[F + 1];
var A = B[F + 2];
var C = B[F + 3];
var u = ((q === 0) && (v === 0) && (A === 255) && (C === 255));
return ((u) && (G))
};
var h = function(q, v, A) {
var w = v[0];
var u = v[1];
var p = q * 4 + A;
u[p + 0] = 0;
u[p + 1] = 0;
u[p + 2] = 255;
u[p + 3] = 0
};
var n = function(G, H, F, w) {
var C = null;
var u;
var D = F.nextp;
while ((D !== null) && (D.nextp !== null)) {
C = D.nextp;
var q = Math.round(D.xIntersect);
var p = Math.round(C.xIntersect);
var B = H + w.y;
var A = d.getScanline(B);
var v = d.scanlineDataArr;
Gfx.floodFill(new Vector2(q, B), w, d, m, h, {
computeBbox: false
});
Gfx.floodFill(new Vector2(p, B), w, d, m, h, {
computeBbox: false
});
D = C.nextp
}
};
g.fillStyle = "rgba(0,0,255,0.0)";
g.globalCompositeOperation = "copy";
g.fillRect(0, 0, g.canvas.width, g.canvas.height);
g.fillStyle = "rgba(0,0,255,1.0)";
g.globalCompositeOperation = "source-over";
var k = Gfx.scanFill(g, o);
var d = Gfx.scanlineCacheFull;
d.init([e, g], k);
Gfx.scanFill(g, o, k, n);
d.flush();
return new Rect()
};
Gfx.scanFill = function(d, m, k, h) {
var g = [];
if (typeof k === "undefined") {
k = Rect.createEnclosingRect(m);
k.w += 1;
k.h += 1
} else {
var e = Rect.createEnclosingRect(m);
e.w += 1;
e.h += 1
}
if (typeof h === "undefined") {
h = Gfx.fillScan
}
for (var f = 0; f <= k.h; f++) {
g.push({
nextp: null
})
}
Gfx.makeSET(m, g, k);
AET = {
nextp: null
};
for (var f = 0; f <= k.h; f++) {
Gfx.buildAET(f, AET, g, k);
if (AET.nextp !== null) {
h(d, f, AET, k);
Gfx.updateAET(f, AET, k);
Gfx.resortAET(AET)
}
}
return k
};
Gfx.insertEdge = function(d, h) {
var f = d;
var g = d.nextp;
while (g !== null) {
if (h.xIntersect < g.xIntersect) {
g = null
} else {
f = g;
g = g.nextp
}
}
h.nextp = f.nextp;
f.nextp = h
};
Gfx.buildAET = function(d, e, k, h) {
var f = null;
var g = k[d].nextp;
while (g !== null) {
f = g.nextp;
Gfx.insertEdge(e, g);
g = f
}
};
Gfx.deleteAfter = function(e) {
var d = e.nextp;
e.nextp = d.nextp;
delete d
};
Gfx.updateAET = function(d, e, h) {
var f = e;
var g = e.nextp;
while (g !== null) {
if ((d + h.y) >= g.yUpper) {
g = g.nextp;
Gfx.deleteAfter(f)
} else {
g.xIntersect += g.dx;
f = g;
g = g.nextp
}
}
};
Gfx.resortAET = function(d) {
var e = null;
var f = d.nextp;
d.nextp = null;
while (f !== null) {
e = f.nextp;
Gfx.insertEdge(d, f);
f = e
}
};
Gfx.fillScan = function(d, e, g, k) {
var h = null;
var f;
var m = g.nextp;
while ((m !== null) && (m.nextp !== null)) {
h = m.nextp;
d.fillRect(Math.round(m.xIntersect), e + k.y, Math.round(h.xIntersect) - Math.round(m.xIntersect) + 1, 1);
m = h.nextp
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment