class LassoSelectionTool extends SelectionTool { currentPixels = []; currSelection = {}; boundingBox = {minX: 99999, maxX: -1, minY: 99999, maxY: -1}; constructor (name, options, switchFunc, moveTool) { super(name, options, switchFunc, moveTool); Events.on('click', this.mainButton, switchFunc, this); } onStart(mousePos) { super.onStart(mousePos); let mouseX = Math.floor(mousePos[0] / currFile.zoom); let mouseY = Math.floor(mousePos[1] / currFile.zoom); // 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); let mouseX = Math.floor(mousePos[0] / currFile.zoom); let mouseY = Math.floor(mousePos[1] / currFile.zoom); if (this.currentPixels[this.currentPixels.length - 1] != mousePos) this.currentPixels.push([mouseX, mouseY]); 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); 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; } onSelect() { super.onSelect(); } onDeselect() { super.onDeselect(); } 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(); 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"; currFile.VFXLayer.context.fillStyle = 'rgba(0,0,0,0.3)'; currFile.VFXLayer.context.fill(); currFile.VFXLayer.context.closePath(); } cursorInSelectedArea(mousePos) { } getSelection() { 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