2020-12-31 18:47:56 +03:00
|
|
|
//draws a line between two points on canvas
|
2020-03-07 18:49:01 +03:00
|
|
|
function line(x0,y0,x1,y1, brushSize) {
|
2019-03-27 02:20:54 +03:00
|
|
|
var dx = Math.abs(x1-x0);
|
|
|
|
var dy = Math.abs(y1-y0);
|
|
|
|
var sx = (x0 < x1 ? 1 : -1);
|
|
|
|
var sy = (y0 < y1 ? 1 : -1);
|
|
|
|
var err = dx-dy;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
//set pixel
|
2019-04-01 22:37:53 +03:00
|
|
|
// If the current tool is the brush
|
2021-04-28 23:35:26 +03:00
|
|
|
if (currentTool.name == 'pencil' || currentTool.name == 'rectangle' || currentTool.name == 'ellipse') {
|
2019-04-01 22:37:53 +03:00
|
|
|
// I fill the rect
|
2019-03-31 17:32:49 +03:00
|
|
|
currentLayer.context.fillRect(x0-Math.floor(brushSize/2), y0-Math.floor(brushSize/2), brushSize, brushSize);
|
2020-04-15 03:01:31 +03:00
|
|
|
} else if (currentTool.name == 'eraser') {
|
2019-04-01 22:37:53 +03:00
|
|
|
// In case I'm using the eraser I must clear the rect
|
2020-04-15 03:01:31 +03:00
|
|
|
currentLayer.context.clearRect(x0-Math.floor(tool.eraser.brushSize/2), y0-Math.floor(tool.eraser.brushSize/2), tool.eraser.brushSize, tool.eraser.brushSize);
|
2019-03-31 14:28:46 +03:00
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2020-03-15 18:32:48 +03:00
|
|
|
//if we've reached the end goal, exit the loop
|
|
|
|
if ((x0==x1) && (y0==y1)) break;
|
|
|
|
var e2 = 2*err;
|
|
|
|
|
2020-07-21 15:42:25 +03:00
|
|
|
if (e2 >-dy) {
|
|
|
|
err -=dy;
|
|
|
|
x0+=sx;
|
|
|
|
}
|
2020-03-15 18:32:48 +03:00
|
|
|
|
2020-07-21 15:42:25 +03:00
|
|
|
if (e2 < dx) {
|
|
|
|
err +=dx;
|
|
|
|
y0+=sy;
|
2020-03-15 18:32:48 +03:00
|
|
|
}
|
2019-03-27 02:20:54 +03:00
|
|
|
}
|
2020-07-21 15:42:25 +03:00
|
|
|
}
|