All files / src/drawing canvas.js

0% Statements 0/11
100% Branches 0/0
0% Functions 0/9
0% Lines 0/11
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                                                                                                                           
/**
* Copyright 2018, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
 
import DrawingBase from './base';
import CanvasCharCache from './cache';
 
class Canvas extends DrawingBase {
  constructor(el) {
    super(el);
    this.ctx = el.getContext('2d');
    this.cache = new CanvasCharCache();
  }
 
  clear() {
    // fastest way to clear the canvas
    // http://jsperf.com/canvas-clear-speed
    this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
  }
 
  fillRect(x, y, width, height) {
    this.ctx.fillRect(x, y, width, height);
  }
 
  // TODO: rename as its effectively only one letter
  fillText(text, x, y, width, height) {
    //this.ctx.fillText(text, x, y);
    return this.ctx.drawImage(
      this.cache.getFontTile(text, width, height, this.ctx.font),
      x, y, width, height,
    );
  }
 
  // props
  font(fontName) {
    this.ctx.font = fontName;
  }
 
  fillStyle(fillStyle) {
    this.ctx.fillStyle = fillStyle;
  }
 
  globalAlpha(globalAlpha) {
    this.ctx.globalAlpha = globalAlpha;
  }
 
  save() {
    this.ctx.save();
  }
 
  restore() {
    this.ctx.restore();
  }
}
 
export {Canvas};
export default Canvas;