|
class DrawHandler { |
|
|
|
constructor(){ |
|
|
|
|
|
this.drawGraphic = createGraphics(width, height); |
|
|
|
|
|
this.drawings = []; |
|
|
|
|
|
this.currentPath = []; |
|
|
|
|
|
this.isDrawing = false; |
|
|
|
|
|
this.isErasing = false; |
|
|
|
|
|
this.eraserRadius = 10; |
|
|
|
this.pencil_btn = createButton('<i class="fa-solid fa-pencil"></i>'); |
|
this.pencil_btn.mousePressed(this.pickPencil.bind(this)); |
|
this.pencil_btn.parent('left-panel'); |
|
this.pencil_active = true; |
|
this.pencil_btn.addClass('tool-active'); |
|
|
|
this.eraser_btn = createButton('<i class="fa-solid fa-eraser"></i>'); |
|
this.eraser_btn.mousePressed(this.pickEraser.bind(this)); |
|
this.eraser_btn.parent('left-panel'); |
|
this.eraser_active = false; |
|
} |
|
|
|
|
|
|
|
|
|
pickPencil(){ |
|
this.isErasing = false; |
|
if(this.pencil_active == false){ |
|
this.pencil_active = true; |
|
this.pencil_btn.addClass('tool-active'); |
|
this.eraser_active = false; |
|
this.eraser_btn.removeClass('tool-active'); |
|
} |
|
} |
|
|
|
pickEraser(){ |
|
this.isErasing = true; |
|
if(this.eraser_active == false){ |
|
this.eraser_active = true; |
|
this.eraser_btn.addClass('tool-active'); |
|
this.pencil_active = false; |
|
this.pencil_btn.removeClass('tool-active'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
mouseDragged(target_graphics){ |
|
|
|
if(Draws.isDrawing && Draws.isErasing){ |
|
|
|
this.trueErase(this.eraserRadius, target_graphics); |
|
|
|
} |
|
|
|
redraw(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
keydown_check(){ |
|
|
|
|
|
|
|
if (keyIsDown(69)){ |
|
this.pickEraser(); |
|
this.isErasing = true; |
|
|
|
} else { |
|
this.pickPencil(); |
|
this.isErasing = false; |
|
|
|
} |
|
} |
|
|
|
|
|
|
|
|
|
startPath() { |
|
|
|
Cursor.calculateAngle(); |
|
|
|
for (let i = 0; i < brushesPoints.length; i++) { |
|
|
|
brushesPoints[i] |
|
.calcPointCoordinates(mouseX, |
|
mouseY, |
|
Cursor.targeAngle, |
|
Cursor.diameter |
|
); |
|
|
|
} |
|
|
|
|
|
for (let i = 0; i < brushesPoints.length; i++) { |
|
brushesPoints[i].resetPointOrigin(); |
|
} |
|
|
|
this.isDrawing = true; |
|
this.currentPath = []; |
|
|
|
|
|
|
|
this.drawings.push(this.currentPath); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
endPath(source_graphics, target_graphics) { |
|
this.isDrawing = false; |
|
|
|
|
|
target_graphics.image(source_graphics, 0, 0); |
|
|
|
|
|
|
|
source_graphics.clear(); |
|
|
|
redraw(); |
|
} |
|
|
|
trueErase(r, target){ |
|
|
|
target.loadPixels(); |
|
|
|
for (let x = mouseX - r; x < mouseX + r; x++) { |
|
for (let y = mouseY - r; y < mouseY + r; y++) { |
|
if ((dist(x,y, mouseX, mouseY) < r) && x > 0 && x <= width) { |
|
|
|
target.set(x,y,color(0,0)); |
|
|
|
} |
|
} |
|
} |
|
|
|
target.updatePixels(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
get_new_current_path(){ |
|
if (this.isDrawing == true && this.isErasing == false) { |
|
|
|
this.drawGraphic.clear(); |
|
|
|
let point = { |
|
x1: [], |
|
y1: [], |
|
x2: [], |
|
y2: [], |
|
x3: [], |
|
y3: [], |
|
x4: [], |
|
y4: [] |
|
} |
|
this.currentPath.push(point); |
|
|
|
for (let i = 0; i < brushesPoints.length; i++) { |
|
brushesPoints[i].shiftPointVertex() |
|
brushesPoints[i].pushPoints(point); |
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
drawLines(index, color, size, graphics){ |
|
|
|
|
|
if(this.isDrawing == true && this.isErasing == false){ |
|
|
|
for (let i = 0; i < this.drawings.length; i++) { |
|
|
|
let path = this.drawings[i]; |
|
|
|
if (path.length != 0) { |
|
noFill(); |
|
|
|
for (let j = 0; j < path.length; j++) { |
|
|
|
|
|
push(); |
|
|
|
graphics.beginShape(); |
|
graphics.strokeWeight(size); |
|
graphics.noFill(); |
|
graphics.stroke(color); |
|
|
|
graphics.curveVertex(path[j].x1[index], path[j].y1[index]); |
|
graphics.curveVertex(path[j].x2[index], path[j].y2[index]); |
|
graphics.curveVertex(path[j].x3[index], path[j].y3[index]); |
|
graphics.curveVertex(path[j].x4[index], path[j].y4[index]); |
|
|
|
graphics.endShape(); |
|
|
|
pop(); |
|
|
|
|
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
} |