All files canvasplus.mjs

45% Statements 63/140
80% Branches 4/5
21.42% Functions 3/14
45% Lines 63/140

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 1411x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x                     1x 1x                     1x 1x         1x 1x                     1x 1x     1x 1x           1x 1x           1x 1x                   1x 1x             1x 1x 1x                 1x 1x             1x 1x 1x 1x  
import logger from './logger.mjs';
 
 
class CanvasPlus {
  constructor(options = {}) {
    this.logger = new logger('CanvasPlus');
    this.options = options;
    this.screenCanvas = null;
    this.composeCanvas = null;
    this.screenContext = null;
    this.composeContext = null;
 
    this.width = options.width || 800;
    this.height = options.height || 600;
 
    this.initCanvas(this.width, this.height);
  }
  
  initCanvas(width = 800, height = 600) {
    this.width = width;
    this.height = height;
    this.screenCanvas = this.createCanvas(width, height);
    this.screenContext = this.screenCanvas.getContext('2d');
    this.composeCanvas = this.createCanvas(width, height);
    this.composeContext = this.composeCanvas.getContext('2d');
    if (this.options.appendTo) {
      this.options.appendTo.appendChild(this.screenCanvas);
    }
    this.logger.log(`Canvas initialized with dimensions ${width}x${height}`);
    return this;
  }
  
  createCanvas(width, height) {
    let canvas = document.createElement('canvas');
    canvas.width = width;
    canvas.height = height;
    return canvas;
  }
 
  get2DComposeContext(){
    if(this.composeContext===null){
      try {
        this.composeContext = this.composeCanvas.getContext('2d');
      }
      catch {
        throw new Error("Canvas context is not initialized. Call initCanvas() first.");
      }
    }           
    return this.composeContext;
  }
 
  get2DScreenContext(){
    if(this.screenContext===null){
      try {
        this.screenContext = this.screenCanvas.getContext('2d');
      }
      catch {
        throw new Error("Canvas context is not initialized. Call initCanvas() first.");
      }
    }           
    return this.screenContext;
  }
 
  clearScreenCanvas() {
    if (this.screenContext) {
      this.screenContext.clearRect(0, 0, this.width, this.height);
    }
  }
  
  clearComposeCanvas(color) {
    if (this.composeContext) {
      if(color){
        this.composeContext.fillStyle = color;
        this.composeContext.fillRect(0, 0, this.width, this.height);
        return;
      } else {
        this.composeContext.clearRect(0, 0, this.width, this.height);
      }
    }
  }
  
  clearCanvas(color = null) {
    this.clearComposeCanvas(color);
  }
 
  renderCanvas() {
    this.clearScreenCanvas();
    if (this.composeContext && this.screenContext) {
      this.screenContext.drawImage(this.composeCanvas, 0, 0, this.width, this.height);
    }
  }
 
  drawRectangle(x, y, width, height, color = 'black') {
    if (this.composeContext) {
      this.composeContext.fillStyle = color;
      this.composeContext.fillRect(x, y, width, height);
    }
  }
 
  drawLine(x1, y1, x2, y2, color = 'black', lineWidth = 1) {
    if (this.composeContext) {
      this.composeContext.strokeStyle = color;
      this.composeContext.lineWidth = lineWidth;
      this.composeContext.beginPath();
      this.composeContext.moveTo(x1, y1);
      this.composeContext.lineTo(x2, y2);
      this.composeContext.stroke();
    }
  }
 
  drawCircle(x, y, radius, color = 'black') {
    if (this.composeContext) {
      this.composeContext.fillStyle = color;
      this.composeContext.beginPath();
      this.composeContext.arc(x, y, radius, 0, Math.PI * 2);
      this.composeContext.fill();
    }
  } 
 
  drawImage(img, x, y, width = null, height = null) {
    if (this.composeContext) {
      if (width && height) {
        this.composeContext.drawImage(img, x, y, width, height);
      } else {
        this.composeContext.drawImage(img, x, y);
      }
    }
  }
 
  drawText(text, x, y, font = '16px Arial', color = 'black') {
    if (this.composeContext) {
      this.composeContext.font = font;
      this.composeContext.fillStyle = color;
      this.composeContext.fillText(text, x, y);
    }
  }
  
}
 
export default CanvasPlus;