2021-11-01 15:02:18 +03:00
|
|
|
class LineTool extends ResizableTool {
|
2021-10-31 20:03:21 +03:00
|
|
|
constructor(name, options, switchFunction) {
|
|
|
|
super(name, options);
|
|
|
|
|
|
|
|
Events.on('click', this.mainButton, switchFunction, this);
|
|
|
|
Events.on('click', this.biggerButton, this.increaseSize.bind(this));
|
|
|
|
Events.on('click', this.smallerButton, this.decreaseSize.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
onStart(mousePos) {
|
|
|
|
super.onStart(mousePos);
|
|
|
|
|
|
|
|
// Putting the tmp layer on top of everything
|
2021-12-06 19:37:43 +03:00
|
|
|
currFile.TMPLayer.canvas.style.zIndex = parseInt(currFile.currentLayer.canvas.style.zIndex, 10) + 1;
|
2021-10-31 20:03:21 +03:00
|
|
|
|
|
|
|
this.startMousePos[0] = Math.floor(mousePos[0]) + 0.5;
|
|
|
|
this.startMousePos[1] = Math.floor(mousePos[1]) + 0.5;
|
2021-11-09 14:19:57 +03:00
|
|
|
|
|
|
|
new HistoryState().EditCanvas();
|
2021-10-31 20:03:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
onDrag(mousePos, cursorTarget) {
|
|
|
|
super.onDrag(mousePos);
|
|
|
|
|
|
|
|
// Drawing the line at the right position
|
|
|
|
this.drawLine(mousePos);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Finishes drawing the rect, decides the end coordinates and moves the preview rectangle to the
|
|
|
|
* current layer
|
|
|
|
*
|
|
|
|
* @param {*} mousePos The position of the mouse when the user stopped dragging
|
|
|
|
*/
|
|
|
|
onEnd(mousePos) {
|
|
|
|
super.onEnd(mousePos);
|
|
|
|
|
2021-12-06 19:37:43 +03:00
|
|
|
const tmpContext = currFile.TMPLayer.context;
|
|
|
|
const tmpCanvas = currFile.TMPLayer.canvas;
|
2021-10-31 20:03:21 +03:00
|
|
|
// Setting the correct linewidth and colour
|
2021-12-06 19:37:43 +03:00
|
|
|
currFile.currentLayer.context.lineWidth = this.currSize;
|
2021-10-31 20:03:21 +03:00
|
|
|
|
|
|
|
// Drawing the line
|
2021-12-06 19:37:43 +03:00
|
|
|
currFile.currentLayer.context.drawImage(tmpCanvas, 0, 0);
|
2021-10-31 20:03:21 +03:00
|
|
|
|
|
|
|
// Update the layer preview
|
2021-12-06 19:37:43 +03:00
|
|
|
currFile.currentLayer.updateLayerPreview();
|
2021-10-31 20:03:21 +03:00
|
|
|
// Clearing the tmp canvas
|
2021-12-06 19:37:43 +03:00
|
|
|
tmpContext.clearRect(0, 0, currFile.TMPLayer.canvas.width, currFile.TMPLayer.canvas.height);
|
2021-10-31 20:03:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
onSelect() {
|
|
|
|
super.onSelect();
|
|
|
|
}
|
|
|
|
|
|
|
|
onDeselect() {
|
|
|
|
super.onDeselect();
|
|
|
|
}
|
|
|
|
|
|
|
|
drawLine(mousePos) {
|
2021-12-06 19:37:43 +03:00
|
|
|
let x0 = Math.floor(this.startMousePos[0]/currFile.zoom);
|
|
|
|
let y0 = Math.floor(this.startMousePos[1]/currFile.zoom);
|
|
|
|
let x1 = Math.floor(mousePos[0]/currFile.zoom);
|
|
|
|
let y1 = Math.floor(mousePos[1]/currFile.zoom);
|
2021-10-31 20:03:21 +03:00
|
|
|
|
|
|
|
let dx = Math.abs(x1-x0);
|
|
|
|
let dy = Math.abs(y1-y0);
|
|
|
|
let sx = (x0 < x1 ? 1 : -1);
|
|
|
|
let sy = (y0 < y1 ? 1 : -1);
|
|
|
|
let err = dx-dy;
|
|
|
|
|
|
|
|
const canvas = document.getElementById('tmp-canvas');
|
|
|
|
const context = canvas.getContext('2d');
|
|
|
|
|
|
|
|
context.clearRect(0, 0, canvas.width, canvas.height);
|
2021-12-06 19:37:43 +03:00
|
|
|
canvas.style.zIndex = parseInt(currFile.currentLayer.canvas.style.zIndex, 10) + 1;
|
2021-10-31 20:03:21 +03:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
context.fillRect(x0-Math.floor(this.currSize/2), y0-Math.floor(this.currSize/2), this.currSize, this.currSize);
|
|
|
|
|
|
|
|
//if we've reached the end goal, exit the loop
|
|
|
|
if ((x0==x1) && (y0==y1)) break;
|
|
|
|
var e2 = 2*err;
|
|
|
|
|
|
|
|
if (e2 >-dy) {
|
|
|
|
err -=dy;
|
|
|
|
x0+=sx;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e2 < dx) {
|
|
|
|
err +=dx;
|
|
|
|
y0+=sy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|