File size: 6,279 Bytes
c2a5d87 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
class DrawHandler {
constructor(){
// create a layer that will handle drawing phase
this.drawGraphic = createGraphics(width, height);
// create an array to store all paths
this.drawings = [];
// create an array to store path's points being produced
this.currentPath = [];
// bool to check if user is drawing, pencil touching and dragged on the canvas
this.isDrawing = false;
// bool to check if user is using eraser instead
this.isErasing = false;
// set the eraser radius
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');
}
}
// -----------------------------------------
// -----------------------------------------
// this function calls the trueErase() method applied on the targeted layer
mouseDragged(target_graphics){
if(Draws.isDrawing && Draws.isErasing){
this.trueErase(this.eraserRadius, target_graphics);
}
redraw();
}
// -----------------------------------------
// -----------------------------------------
// this function checks if a key is down,
// if so do corresponding task
keydown_check(){
// checks if the "E" key is down,
// if so set isErasing bool to true while "E" key is down
if (keyIsDown(69)){ // KEY E
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 = [];
//console.log("——");
//console.log("You started a new path!");
this.drawings.push(this.currentPath);
//console.log("A new array of points is pushed in 'drawings'");
}
// -----------------------------------------
// -----------------------------------------
endPath(source_graphics, target_graphics) {
this.isDrawing = false;
// on affiche le nouveau drawings sur la target
target_graphics.image(source_graphics, 0, 0);
// on vide le drawings array
//this.drawings = [];
// on clear le drawGraphic
source_graphics.clear();
//on redraw
redraw();
}
trueErase(r, target){
// target is the graphics you want to erase on | e.g: className.frameGraphics
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){
//Shows the current drawing if there any data in drawing array
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); // A
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();
}
}
}
}
}
} |