pixel-editor/js/tools/LassoSelectionTool.js

187 lines
6.8 KiB
JavaScript
Raw Normal View History

class LassoSelectionTool extends SelectionTool {
2021-12-30 01:16:16 +03:00
currentPixels = [];
currSelection = {};
boundingBox = {minX: 99999, maxX: -1, minY: 99999, maxY: -1};
2021-12-29 00:51:18 +03:00
constructor (name, options, switchFunc, moveTool) {
super(name, options, switchFunc, moveTool);
2021-12-29 00:51:18 +03:00
Events.on('click', this.mainButton, switchFunc, this);
}
onStart(mousePos) {
super.onStart(mousePos);
2021-12-29 00:51:18 +03:00
let mouseX = Math.floor(mousePos[0] / currFile.zoom);
let mouseY = Math.floor(mousePos[1] / currFile.zoom);
2021-12-29 00:51:18 +03:00
// Putting the vfx layer on top of everything
currFile.VFXLayer.canvas.style.zIndex = MAX_Z_INDEX;
// clearSelection();
this.currentPixels = [];
this.drawSelection();
this.currentPixels.push([mouseX, mouseY]);
this.boundingBox = {minX: 99999, maxX: -1, minY: 99999, maxY: -1};
this.checkBoundingBox(mouseX, mouseY);
}
onDrag(mousePos) {
super.onDrag(mousePos);
2021-12-29 00:51:18 +03:00
let mouseX = Math.floor(mousePos[0] / currFile.zoom);
let mouseY = Math.floor(mousePos[1] / currFile.zoom);
2021-12-30 01:16:16 +03:00
2021-12-29 00:51:18 +03:00
if (this.currentPixels[this.currentPixels.length - 1] != mousePos)
this.currentPixels.push([mouseX, mouseY]);
2021-12-29 00:51:18 +03:00
this.drawSelection();
this.checkBoundingBox(mouseX, mouseY);
}
onEnd(mousePos) {
super.onEnd(mousePos);
new HistoryState().EditCanvas();
let mouseX = Math.floor(mousePos[0] / currFile.zoom);
let mouseY = Math.floor(mousePos[1] / currFile.zoom);
this.currentPixels.push(this.currentPixels[0]);
this.checkBoundingBox(mouseX, mouseY);
this.getSelection();
// Switch to the move tool so that the user can move the selection
this.switchFunc(this.moveTool);
2021-12-30 01:16:16 +03:00
this.moveTool.setSelectionData(null, this);
}
checkBoundingBox(mouseX, mouseY) {
if (mouseX > this.boundingBox.maxX)
this.boundingBox.maxX = mouseX;
else if (mouseX < this.boundingBox.minX)
this.boundingBox.minX = mouseX;
if (mouseY < this.boundingBox.minY)
this.boundingBox.minY = mouseY;
else if (mouseY > this.boundingBox.maxY)
this.boundingBox.maxY = mouseY;
}
2021-12-29 00:51:18 +03:00
onSelect() {
super.onSelect();
}
2021-12-29 00:51:18 +03:00
onDeselect() {
super.onDeselect();
}
2021-12-29 00:51:18 +03:00
drawSelection() {
if (this.currentPixels.length <= 1)
return;
let point = [];
currFile.VFXLayer.context.clearRect(0, 0, currFile.canvasSize[0], currFile.canvasSize[1]);
currFile.VFXLayer.context.strokeStyle = 'rgba(0,0,0,1)';
currFile.VFXLayer.context.fillStyle = 'rgba(0,0,0,1)';
currFile.VFXLayer.context.lineWidth = 1;
currFile.VFXLayer.context.beginPath();
2021-12-29 00:51:18 +03:00
for (var index = 0; index < this.currentPixels.length; index ++) {
point = this.currentPixels[index];
if (index == 0)
currFile.VFXLayer.context.moveTo(point[0], point[1]);
else {
currFile.VFXLayer.context.lineTo(point[0], point[1]);
}
}
currFile.VFXLayer.context.filter = "url(#remove-alpha)"
currFile.VFXLayer.context.lineTo(this.currentPixels[0][0], this.currentPixels[0][1]);
currFile.VFXLayer.context.stroke();
currFile.VFXLayer.context.filter = "none";
2021-12-29 00:51:18 +03:00
currFile.VFXLayer.context.fillStyle = 'rgba(0,0,0,0.3)';
currFile.VFXLayer.context.fill();
currFile.VFXLayer.context.closePath();
}
2021-12-30 01:16:16 +03:00
cursorInSelectedArea(mousePos) {
}
getSelection() {
2021-12-30 01:16:16 +03:00
let selected = [];
if (this.currentPixels.length <= 1){
return;
}
currFile.VFXLayer.context.fillStyle = "blue";
currFile.VFXLayer.context.fillRect(this.boundingBox.minX, this.boundingBox.minY, 1, 1);
currFile.VFXLayer.context.fillRect(this.boundingBox.minX, this.boundingBox.maxY, 1, 1);
currFile.VFXLayer.context.fillRect(this.boundingBox.maxX, this.boundingBox.maxY, 1, 1);
currFile.VFXLayer.context.fillRect(this.boundingBox.maxX, this.boundingBox.minY, 1, 1);
let fillPoint = [];
let found = false;
// Find a point inside the selection
while (!found) {
let nIntersections = 0;
let prevPoint = -1;
fillPoint = [Math.round(Math.random() * (this.boundingBox.maxX - this.boundingBox.minX)),
Math.round(Math.random() * (this.boundingBox.maxY - this.boundingBox.minY))];
currFile.VFXLayer.context.fillStyle = "green";
currFile.VFXLayer.context.fillRect(fillPoint[0], fillPoint[1], 1, 1);
// Count the number of intersections with the shape
for (let i=0; i<this.currentPixels.length; i++) {
if (fillPoint[0] == this.currentPixels[i][0] && this.currentPixels[i][1] != prevPoint)
nIntersections++;
prevPoint = this.currentPixels[i][1];
}
if (nIntersections & 1)
found = true;
}
console.log("Point: ");
console.log(fillPoint);
/*for (let x=this.boundingBox.minX+1; x<this.boundingBox.maxX; x++) {
for (let y=this.boundingBox.minY+1; y<this.boundingBox.maxY; y++) {
2021-12-30 01:16:16 +03:00
let toCheck = [x, y];
let intersectionCount = 0;
if (currFile.VFXLayer.context.getImageData(toCheck[0], toCheck[1], 1, 1).data[3] != 255) {
// Start from the pixel to check
let currPos = [toCheck[0], toCheck[1]];
// Finish when you get the pixel to check
let endPos = [toCheck[0], this.boundingBox.maxX];
let prevColor = 0;
// Go down: if you meet a pixel of the border, increase the number of intersections
while (currPos[1] <= endPos[1]) {
let pixel = currFile.VFXLayer.context.getImageData(currPos[0],
currPos[1], 1, 1).data;
if (pixel[3] == 255 && prevColor != 255) {
// Check if there's a closing pixel below
intersectionCount++;
2021-12-30 01:16:16 +03:00
}
currPos[1]++;
prevColor = pixel[3];
2021-12-30 01:16:16 +03:00
}
// If the number of intersections is even (0 or 2), then the pixel is outside the
// seleted region
2021-12-30 01:16:16 +03:00
if (intersectionCount & 1)
selected.push(toCheck);
2021-12-30 01:16:16 +03:00
}
}
}*/
2021-12-30 01:16:16 +03:00
for (let i=0; i<selected.length; i++) {
currFile.VFXLayer.context.fillStyle = "red";
currFile.VFXLayer.context.fillRect(selected[i][0], selected[i][1], 1, 1);
}
}
}