mirror of
https://github.com/lospec/pixel-editor.git
synced 2023-08-10 21:12:51 +03:00
Optimized lasso tool selection
This commit is contained in:
parent
e139a6cc41
commit
4179393e91
@ -86,4 +86,13 @@ class Util {
|
|||||||
static toggle(elementId) {
|
static toggle(elementId) {
|
||||||
Util.getElement(elementId).classList.toggle('selected');
|
Util.getElement(elementId).classList.toggle('selected');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static getPixelColor(data, x, y, dataWidth) {
|
||||||
|
let pos = (y * dataWidth + x) * 4;
|
||||||
|
return [data[pos], data[pos+1], data[pos+2], data[pos + 3]];
|
||||||
|
}
|
||||||
|
|
||||||
|
static isPixelTransparent(data, x, y, dataWidth) {
|
||||||
|
return this.getPixelColor(data, x, y, dataWidth)[3] == 255;
|
||||||
|
}
|
||||||
}
|
}
|
@ -103,7 +103,7 @@ class LassoSelectionTool extends SelectionTool {
|
|||||||
pixel[1] == this.boundingBox.minY || pixel[1] == this.boundingBox.maxY;
|
pixel[1] == this.boundingBox.minY || pixel[1] == this.boundingBox.maxY;
|
||||||
}
|
}
|
||||||
|
|
||||||
visit(pixel, visited) {
|
visit(pixel, visited, data) {
|
||||||
let toVisit = [pixel];
|
let toVisit = [pixel];
|
||||||
let selected = [];
|
let selected = [];
|
||||||
let currVisited = {};
|
let currVisited = {};
|
||||||
@ -115,7 +115,7 @@ class LassoSelectionTool extends SelectionTool {
|
|||||||
visited[pixel] = true;
|
visited[pixel] = true;
|
||||||
currVisited[pixel] = true;
|
currVisited[pixel] = true;
|
||||||
|
|
||||||
let col = currFile.VFXLayer.context.getImageData(pixel[0], pixel[1], 1, 1).data;
|
let col = Util.getPixelColor(data, pixel[0], pixel[1], currFile.canvasSize[0]);
|
||||||
if (col[3] == 255)
|
if (col[3] == 255)
|
||||||
continue;
|
continue;
|
||||||
if (this.isBorderOfBox(pixel))
|
if (this.isBorderOfBox(pixel))
|
||||||
@ -159,23 +159,15 @@ class LassoSelectionTool extends SelectionTool {
|
|||||||
getSelection() {
|
getSelection() {
|
||||||
let selected = [];
|
let selected = [];
|
||||||
let visited = {};
|
let visited = {};
|
||||||
|
let data = currFile.VFXLayer.context.getImageData(0, 0, currFile.canvasSize[0], currFile.canvasSize[1]).data;
|
||||||
if (this.currentPixels.length <= 1){
|
if (this.currentPixels.length <= 1){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** I'm once again asking you to make a BFS
|
|
||||||
* - This time since I'm dumb check all pixels in the bounding box
|
|
||||||
* - Start a BFS: stop when you reach the border of the bounding box:
|
|
||||||
* - In that case all the pixels you visited aren't part of the selection
|
|
||||||
* - Also stop when you touch black:
|
|
||||||
* - If you haven't found the border of the bounding box, then all the pixels you visited
|
|
||||||
* are inside the selection
|
|
||||||
*/
|
|
||||||
|
|
||||||
for (let x=this.boundingBox.minX; x<=this.boundingBox.maxX; x++) {
|
for (let x=this.boundingBox.minX; x<=this.boundingBox.maxX; x++) {
|
||||||
for (let y=this.boundingBox.minY; y<=this.boundingBox.maxY; y++) {
|
for (let y=this.boundingBox.minY; y<=this.boundingBox.maxY; y++) {
|
||||||
if (visited[x, y] == undefined) {
|
if (visited[[x, y]] == undefined) {
|
||||||
let insidePixels = this.visit([x,y], visited);
|
let insidePixels = this.visit([x,y], visited, data);
|
||||||
|
|
||||||
for (let i=0; i<insidePixels.length; i++) {
|
for (let i=0; i<insidePixels.length; i++) {
|
||||||
selected.push(insidePixels[i]);
|
selected.push(insidePixels[i]);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user