All files / src/app/models canvas.ts

68.34% Statements 190/278
70.96% Branches 22/31
74% Functions 37/50
69.59% Lines 190/273

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593                      5645x 5645x       276x                       130x                     190x 190x 190x 190x       95x 95x               694x 694x 694x                 592x 592x                   1527x 1527x 1527x         28x 28x 28x 28x 28x                     1507x       838x 838x 838x 838x 838x 838x 838x           381x 381x   381x 381x 381x     381x 361x 361x 361x     381x         292x         292x 292x   292x 292x 292x     292x 292x 292x 292x     292x         14x     14x 14x     14x   14x     14x     14x 14x 14x     14x     14x 14x     14x 14x 14x     14x 14x 14x 14x     14x           550x   550x 550x   550x 550x 550x 550x       550x 550x 550x       550x         550x 550x     550x 550x 550x     550x       42x   42x 42x 1506x     42x 42x 42x       399x   399x 399x 1363x     399x 399x 399x 399x 116x 116x   399x                                             15x 5x 5x     15x 5x 5x     15x 15x 15x 15x                 10x       5x 5x 5x 5x 5x 261x 261x 261x 261x   5x                 5x 5x 5x       2856x       2856x       2846x       2846x       5x 5x 5x       5x         10x 10x   8x 8x 8x 8x 8x       2x 2x 2x 2x 2x   10x 10x         5x 5x 5x 5x                                                             2435x 2435x 2435x       4x 4x 4x                             5x   5x     5x 5x   5x 5x 5x   5x         5x 4x 4x   4x   4x     5x         5x 4x                 5x                 5x 5x       4x 4x 4x     4x             5x 5x 5x                                                                                                                                               12x          
 
 
export interface PointInterface{
  x: number;
  y: number;
}
 
export class Point{
    x: number;
    y: number;
    constructor(x:number, y:number){
        this.x = x;
        this.y = y;
    }
 
    distance(otherPoint: Point): number{
      return Math.sqrt((this.x - otherPoint.x)**2 + (this.y - otherPoint.y)**2);
  }
 
    middle(otherPoint: Point): Point{
        return new Point(Math.round(0.5*(this.x-otherPoint.x)), Math.round(0.5*(this.y-otherPoint.y)));
    }
 
    subtract(point: Point): Point{
      return new Point(this.x - point.x, this.y - point.y);
    }
 
    add(point: Point): Point{
      return new Point(this.x + point.x, this.y + point.y);
    }
}
 
export class ColorPalette{
  background: string;
  border: string;
  foreground: string;
  alert: string;
 
  constructor(background: string, border: string, foreground:string, alert:string){
      this.background = background;
      this.border = border;
      this.foreground = foreground;
      this.alert = alert;
  }
}
 
export const darkPalette = new ColorPalette("#292929", "#FFFFFF", "#474747", "#d1b036");
export const lightPalette = new ColorPalette("#FFFFFF", "#000000", "#a8a8a8", "#d1b036");
 
export class TextStyle{
  color: string;
  align: CanvasTextAlign;
  baseLine: CanvasTextBaseline;
 
  constructor(color: string, align: CanvasTextAlign="center", baseLine: CanvasTextBaseline="middle"){
    this.color = color;
    this.align = align;
    this.baseLine = baseLine;
  }
}
 
export class EdgeStyle{
  lineWidth: number;
  lineColor: string;
 
  constructor(lineWidth: number, lineColor: string){
    this.lineWidth = lineWidth;
    this.lineColor = lineColor;
  }
}
 
export class SurfaceStyle{
  fillColor: string | null;
  lineWidth: number;
  lineColor: string;
 
  constructor(fillColor: string | null, lineWidth: number, lineColor: string = ""){
    this.fillColor = fillColor;
    this.lineWidth = lineWidth;
    this.lineColor = lineColor;
  }
}
 
function getPerpendicularBisector(point1: Point, point2: Point) {
  const midX = (point1.x + point2.x) / 2;
  const midY = (point1.y + point2.y) / 2;
  const dx = point2.x - point1.x;
  const dy = point2.y - point1.y;
  return {
    x: midX,
    y: midY,
    slope: -dx / dy
  };
}
 
export class Drawer{
  context: CanvasRenderingContext2D;
 
  constructor(context: CanvasRenderingContext2D){
    this.context = context;
  }
 
  drawText(text: string, x:number, y:number, style:TextStyle, fontSize: number = 12){
    this.context.textAlign = style.align;
    this.context.textBaseline = style.baseLine;
    this.context.beginPath();
    this.context.fillStyle = style.color;
    this.context.font = this.context.font.replace(/(?<value>\d+\.?\d*)/, fontSize.toString());
    this.context.fillText(text, x, y);
    this.context.closePath();
  }
 
  drawRectangle(xmin: number, ymax:number, width:number, height:number, style:SurfaceStyle){
 
 
    this.context.beginPath();
    this.context.rect(xmin, ymax, width, height);
 
    Eif (style.fillColor !== null){
      this.context.fillStyle = style.fillColor;
      this.context.fill();
    }
 
    if (style.lineWidth > 0){
      this.context.lineWidth = style.lineWidth;
      this.context.strokeStyle = style.lineColor;
      this.context.stroke();
    }
 
    this.context.closePath();
  }
 
  drawCircle(x: number, y:number, radius:number, style:SurfaceStyle){
 
    this.drawArc(x, y, radius, 0, 2*Math.PI, style);
  }
 
  drawArc(x: number, y:number, radius:number, startAngle: number, endAngle: number, style:SurfaceStyle, trigo:boolean=true){
 
    this.context.beginPath();
    this.context.arc(x, y, radius, startAngle, endAngle, trigo);
 
    Eif (style.fillColor !== null){
      this.context.fillStyle = style.fillColor;
      this.context.fill();
    }
 
    Eif (style.lineWidth > 0){
      this.context.lineWidth = style.lineWidth;
      this.context.strokeStyle = style.lineColor;
      this.context.stroke();
    }
 
    this.context.closePath();
  }
 
  drawArcFromPoints(start:Point, interior:Point, end:Point, style:SurfaceStyle){
 
    this.context.beginPath();
 
    // Calculate the perpendicular bisectors of the segments start-interior and interior-end
    const bisector1 = getPerpendicularBisector(start, interior);
    const bisector2 = getPerpendicularBisector(interior, end);
 
    // Calculate the intersection of the two bisectors, which is the circle center
    const centerX = (bisector2.y - bisector1.y + bisector1.slope * bisector1.x - bisector2.slope * bisector2.x) /
                    (bisector1.slope - bisector2.slope);
    const centerY = bisector1.slope * (centerX - bisector1.x) + bisector1.y;
 
    // Calculate the radius as the distance from the center to the start point
    const radius = Math.sqrt((start.x - centerX) ** 2 + (start.y - centerY) ** 2);
 
    // Calculate start and end angles
    const startAngle = Math.atan2(start.y - centerY, start.x - centerX);
    const interiorAngle = Math.atan2(interior.y - centerY, interior.x - centerX);
    const endAngle = Math.atan2(end.y - centerY, end.x - centerX);
 
    // Determine direction based on the order of angles (clockwise or counter-clockwise)
    const isClockwise = (interiorAngle > startAngle && interiorAngle < endAngle);
 
    // Draw the arc
    this.context.beginPath();
    this.context.arc(centerX, centerY, radius, startAngle, endAngle, !isClockwise);
 
 
    Eif (style.fillColor !== null){
      this.context.fillStyle = style.fillColor;
      this.context.fill();
    }
 
    Eif (style.lineWidth > 0){
      this.context.lineWidth = style.lineWidth;
      this.context.strokeStyle = style.lineColor;
      this.context.stroke();
    }
 
    this.context.closePath();
  }
 
 
  drawArrow(start: Point, end:Point, style:EdgeStyle){
 
    this.context.beginPath();
 
    const headlen = 10;
    const angle = Math.atan2(end.y-start.y,end.x-start.x);
 
    this.context.moveTo(start.x, start.y);
    this.context.lineTo(end.x, end.y);
    this.context.lineWidth = style.lineWidth;
    this.context.stroke();
 
    //starting a new path from the head of the arrow to one of the sides of
    //the point
    this.context.beginPath();
    this.context.moveTo(end.x, end.y);
    this.context.lineTo(end.x-headlen*Math.cos(angle-Math.PI/7),
               end.y-headlen*Math.sin(angle-Math.PI/7));
 
    //path from the side point of the arrow, to the other side point
    this.context.lineTo(end.x-headlen*Math.cos(angle+Math.PI/7),
               end.y-headlen*Math.sin(angle+Math.PI/7));
 
    //path from the side point back to the tip of the arrow, and then
    //again to the opposite side point
    this.context.lineTo(end.x, end.y);
    this.context.lineTo(end.x-headlen*Math.cos(angle-Math.PI/7),
               end.y-headlen*Math.sin(angle-Math.PI/7));
 
    this.context.lineWidth = style.lineWidth;
    this.context.strokeStyle = style.lineColor;
    this.context.stroke();
 
 
    this.context.closePath();
  }
 
  drawPath(points: Point[], style:EdgeStyle){
    this.context.beginPath();
 
    this.context.moveTo(points[0].x, points[0].y);
    for (const point of points.slice(1)){
      this.context.lineTo(point.x, point.y);
    }
 
    this.context.lineWidth = style.lineWidth;
    this.context.strokeStyle = style.lineColor;
    this.context.stroke();
  }
 
  drawPolygon(points: Point[], style:SurfaceStyle){
    this.context.beginPath();
 
    this.context.moveTo(points[0].x, points[0].y);
    for (const point of points.slice(1)){
      this.context.lineTo(point.x, point.y);
    }
 
    this.context.lineWidth = style.lineWidth;
    this.context.strokeStyle = style.lineColor;
    this.context.stroke();
    if (style.fillColor){
      this.context.fillStyle = style.fillColor;
      this.context.fill();
    }
    this.context.closePath();
  }
 
  drawLine(start: Point, end: Point, style:EdgeStyle){
    this.context.beginPath();
    this.context.moveTo(start.x, start.y);
    this.context.lineTo(end.x, end.y);
    this.context.lineWidth = style.lineWidth;
    this.context.strokeStyle = style.lineColor;
    this.context.stroke();
    this.context.closePath();
 
  }
}
 
export class Zone{
    xMin: number;
    yMin: number;
    xMax: number;
    yMax: number;
 
    constructor(xMin: number, xMax: number, yMin: number, yMax: number){
 
      if (xMin == xMax){
        xMin -= 0.5;
        xMax += 0.5;
      }
 
      if (yMin == yMax){
        yMin -= 0.5;
        yMax += 0.5;
      }
 
      this.xMin = xMin;
      this.xMax = xMax;
      this.yMin = yMin;
      this.yMax = yMax;
 
  }
 
  get center(){
    return new Point(0.5*(this.xMin + this.xMax), 0.5*(this.yMin + this.yMax));
  }
 
  aspect_ratio(){
    return (this.xMax - this.xMin) / (this.yMax - this.yMin);
  }
 
  static fromPoints(points: Point[]): Zone{
    let xMin = points[0].x;
    let yMin = points[0].y;
    let xMax = points[0].x;
    let yMax = points[0].y;
    for (const point of points){
      xMin = Math.min(xMin, point.x);
      xMax = Math.max(xMax, point.x);
      yMin = Math.min(yMin, point.y);
      yMax = Math.max(yMax, point.y);
    }
    return new Zone(xMin, xMax, yMin, yMax);
  }
}
 
export class ViewWindow extends Zone{
    displayWidth: number;
    displayHeight: number;
 
    constructor(xmin: number, xmax: number, ymin: number, ymax: number, displayWidth: number, displayHeight: number){
      super(xmin, xmax, ymin, ymax);
      this.displayWidth = displayWidth;
      this.displayHeight = displayHeight;
    }
 
    get xScale(): number{
        return (this.xMax - this.xMin) / this.displayWidth;
    }
 
    get yScale(): number{
        return (this.yMax - this.yMin) / this.displayHeight;
    }
 
    get scale(): number{
      return Math.min(this.xScale, this.yScale);
    }
 
    size(size: number){
      return Math.round(size / this.scale);
    }
 
    resize(newDisplayWidth: number, newDisplayHeight: number): void {
      this.adjustToZone(this.toZone(), newDisplayWidth, newDisplayHeight);
      this.displayHeight = newDisplayHeight;
      this.displayWidth = newDisplayWidth;
    }
 
    toZone(){
      return new Zone(this.xMin, this.xMax, this.yMin, this.yMax);
    }
 
    adjustToZone(zone:Zone, displayWidth: number, displayHeight: number): void{
      // Try adjust height
      const ratio = zone.aspect_ratio() / (displayWidth / displayHeight);
      if (ratio < 1){
        // Height adjust
        this.yMin = zone.yMin;
        this.yMax = zone.yMax;
        const deltaX = (zone.xMax - zone.xMin) / ratio;
        this.xMin = 0.5*(zone.xMin + zone.xMax) - 0.5 * deltaX;
        this.xMax = this.xMin + deltaX;
      }
      else{
        // Width adjust
        this.xMin = zone.xMin;
        this.xMax = zone.xMax;
        const deltaY = (zone.yMax - zone.yMin) * ratio;
        this.yMin = 0.5*(zone.yMin + zone.yMax) - 0.5 * deltaY;
        this.yMax = this.yMin + deltaY;
      }
      this.displayWidth = displayWidth;
      this.displayHeight = displayHeight;
    }
 
 
    pixelMargins(xMargin: number, yMargin: number){
      this.xMin = this.xMin - xMargin*this.xScale;
      this.xMax = this.xMax + xMargin*this.xScale;
      this.yMin =  this.yMin - yMargin*this.yScale;
      this.yMax = this.yMax + yMargin*this.yScale;
    }
 
    zoneOffset(x: number, y: number){
      this.xMin += x;
      this.xMax += x;
      this.yMin += y;
      this.yMax += y;
    }
 
    displayOffset(pixelsX: number, pixelsY: number){
      const dx = pixelsX * this.xScale;
      const dy = pixelsY * this.yScale;
      this.xMin += dx;
      this.xMax += dx;
      this.yMin += dy;
      this.yMax += dy;
    }
 
    displayZoom(scale: number){
      const dX = 0.5 * scale * (this.xMax - this.xMin);
      const dY = 0.5 * scale * (this.yMax - this.yMin);
      this.xMin = this.center.x -dX;
      this.xMax = this.center.x + dX;
      this.yMin =  this.center.y - dY;
      this.yMax = this.center.y + dY;
 
    }
 
 
    canvasPoint(zonePoint: Point): Point{
      const x = Math.round((zonePoint.x - this.xMin) / (this.xMax - this.xMin) * this.displayWidth);
      const y = Math.round((1 - (zonePoint.y - this.yMin) / (this.yMax - this.yMin)) * this.displayHeight);
      return new Point(x, y);
    }
 
    zonePoint(canvasPoint: Point): Point {
      const x = (canvasPoint.x / this.displayWidth) * (this.xMax - this.xMin) + this.xMin;
      const y = this.yMax - (canvasPoint.y / this.displayHeight) * (this.yMax - this.yMin);
      return new Point(x, y);
    }
}
 
export abstract class DrawObject{
  abstract draw(context: CanvasRenderingContext2D, viewWindow: ViewWindow): void;
  abstract get zone(): Zone;
 
  abstract mouseDownCallback(clickPoint: Point, viewWindow: ViewWindow): boolean;
}
 
export class Canvas{
  context: CanvasRenderingContext2D;
  drawObject: DrawObject;
  viewWindow: ViewWindow;
  drag: boolean = false;
  lastDragPosition: Point;
  scaleSensitivity: number = 0.04;
 
  constructor(context: CanvasRenderingContext2D, drawObject: DrawObject){
      this.context = context;
      this.drawObject = drawObject;
 
      this.viewWindow = new ViewWindow(0, 0, 0, 0, 0, 0);
      this.viewWindow.adjustToZone(this.drawObject.zone, this.context.canvas.width, this.context.canvas.height);
      this.viewWindow.pixelMargins(50, 50);
 
      window.addEventListener('resize', () => {
          this.resize();
          this.draw();
      });
 
      context.canvas.addEventListener('mousedown', (event) => {
        this.drag = true;
        const clickPoint = this.clickPoint(context, event.clientX, event.clientY);
 
        this.drag = this.drawObject.mouseDownCallback(clickPoint, this.viewWindow);
 
        this.lastDragPosition = clickPoint;
      });
 
      context.canvas.addEventListener('mouseup', () => {
        this.drag = false;
 
      });
 
      context.canvas.addEventListener('mousemove', (event) => {
        Iif (this.drag){
          const position = this.clickPoint(context, event.clientX, event.clientY);
          const move = position.subtract(this.lastDragPosition);
          this.viewWindow.displayOffset(-move.x, move.y);
          this.lastDragPosition = position;
          this.draw();
        }
      });
 
      context.canvas.addEventListener('wheel', (event) => {
        let scale = 1 - this.scaleSensitivity;
        if ( event.deltaY > 0){
          scale = 1 + this.scaleSensitivity;
        }
        this.viewWindow.displayZoom(scale);
        this.draw();
      });
 
      this.resize();
      this.draw();
  }
 
  clickPoint(context: CanvasRenderingContext2D, clientX: number, clientY: number){
    const rect = context.canvas.getBoundingClientRect();
    const scaleX = context.canvas.width / rect.width;
    const scaleY = context.canvas.height / rect.height;
 
    // Adjust click coordinates by scaling
    return  new Point(
        (clientX - rect.left) * scaleX,
        (clientY - rect.top) * scaleY
    );
  }
 
  resize() {
      this.context.canvas.width = window.innerWidth;
      this.context.canvas.height = window.innerHeight;
      this.viewWindow.resize(this.context.canvas.width, this.context.canvas.height);
    }
 
 
  drawCanvasGrid(context: CanvasRenderingContext2D){
    const drawer = new Drawer(context);
    const nGrid = 10;
    const style = new EdgeStyle(1, "#bfbcbb");
 
    for (let i=0; i<=nGrid+1; i++){
      const x = this.viewWindow.xMin + (this.viewWindow.xMax - this.viewWindow.xMin) * i/nGrid;
      const start = this.viewWindow.canvasPoint(new Point(x, this.viewWindow.yMin));
      const end = this.viewWindow.canvasPoint(new Point(x, this.viewWindow.yMax));
      drawer.drawLine(start, end, style);
      drawer.drawText("x= "+(Math.round(x*100)/100).toString(), start.x, end.y+20, new TextStyle("#bfbcbb"));
    }
 
    for (let i=0; i<=nGrid+1; i++){
      const y = this.viewWindow.yMin + (this.viewWindow.yMax - this.viewWindow.yMin) * i/nGrid;
      const start = this.viewWindow.canvasPoint(new Point(this.viewWindow.xMin, y));
      const end = this.viewWindow.canvasPoint(new Point(this.viewWindow.xMax, y));
      drawer.drawLine(start, end, style);
      drawer.drawText("y= "+(Math.round(y*100)/100).toString(), start.x+20, end.y, new TextStyle("#bfbcbb"));
    }
  }
 
  drawPixelGrid(context: CanvasRenderingContext2D){
    const drawer = new Drawer(context);
    const gridPixels = 100;
    const style = new EdgeStyle(1, "#bfbcbb");
 
    const nX = Math.ceil(this.viewWindow.displayWidth/gridPixels);
    for (let i=0; i<=nX; i++){
      const x = i*gridPixels;
      const start = new Point(x, 0);
      const end = new Point(x, this.viewWindow.displayHeight);
      drawer.drawLine(start, end, style);
      drawer.drawText("px= "+x.toString()+ " ("+Math.round(x/this.viewWindow.displayWidth*100).toString()  +"%)", start.x, end.y-20, new TextStyle("#bfbcbb"));
    }
 
  }
 
  drawZoneGrid(context: CanvasRenderingContext2D){
    const drawer = new Drawer(context);
 
    const style = new EdgeStyle(1, "#bfbcbb");
    const redStyle = new EdgeStyle(1, "#ff4f42");
    const xStart = Math.floor(this.viewWindow.xMin);
    const xEnd = Math.ceil(this.viewWindow.xMax);
    const yStart = Math.floor(this.viewWindow.yMin);
    const yEnd = Math.ceil(this.viewWindow.yMax);
 
    for (let x=xStart; x<=xEnd; x++){
      const start = this.viewWindow.canvasPoint(new Point(x, yStart));
      const end = this.viewWindow.canvasPoint(new Point(x, yEnd));
      drawer.drawLine(start, end, style);
      drawer.drawText("x= "+(Math.round(x*100)/100).toString(), start.x, end.y+20, new TextStyle("#bfbcbb"));
    }
    drawer.drawLine(this.viewWindow.canvasPoint(new Point(0, yStart)), this.viewWindow.canvasPoint(new Point(0  , yEnd)), redStyle);
 
    for (let y=yStart; y<=xEnd; y++){
      const start = this.viewWindow.canvasPoint(new Point(xStart, y));
      const end = this.viewWindow.canvasPoint(new Point(xEnd, y));
      drawer.drawLine(start, end, style);
      drawer.drawText("x= "+(Math.round(y*100)/100).toString(), start.x, end.y+20, new TextStyle("#bfbcbb"));
    }
    drawer.drawLine(this.viewWindow.canvasPoint(new Point(xStart, 0)), this.viewWindow.canvasPoint(new Point(xEnd, 0)), redStyle);
 
 
  }
 
  draw(){
      this.drawObject.draw(this.context, this.viewWindow);
  }
 
 
}