pixel-editor/js/tools/LassoSelectionTool.js

77 lines
2.5 KiB
JavaScript
Raw Normal View History

class LassoSelectionTool extends SelectionTool {
2021-12-30 01:16:16 +03:00
currentPixels = [];
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
// Putting the vfx layer on top of everything
currFile.VFXLayer.canvas.style.zIndex = MAX_Z_INDEX;
// clearSelection();
2021-12-29 00:51:18 +03:00
this.currentPixels = [];
this.drawSelection();
this.currentPixels.push([mousePos[0] / currFile.zoom, mousePos[1] / currFile.zoom]);
}
onDrag(mousePos) {
super.onDrag(mousePos);
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([mousePos[0] / currFile.zoom, mousePos[1] / currFile.zoom]);
2021-12-29 00:51:18 +03:00
this.drawSelection();
}
onEnd(mousePos) {
super.onEnd(mousePos);
new HistoryState().EditCanvas();
this.currentPixels.push([this.startMousePos[0] / currFile.zoom, this.startMousePos[1] / currFile.zoom]);
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);
}
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 = [];
let prevPoint = [];
2021-12-29 00:51:18 +03:00
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 {
prevPoint = this.currentPixels[index- 1];
currFile.VFXLayer.drawLine(Math.floor(prevPoint[0]), Math.floor(prevPoint[1]),
Math.floor(point[0]), Math.floor(point[1]), 1);
2021-12-29 00:51:18 +03:00
}
}
currFile.VFXLayer.drawLine(Math.floor(point[0]), Math.floor(point[1]),
Math.floor(this.currentPixels[0][0]), Math.floor(this.currentPixels[0][1]), 1);
}
}