All files / lib/display canvas-display.js

0% Statements 0/87
0% Branches 0/49
0% Functions 0/10
0% Lines 0/78

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                                                                                                                                                                                                                                                                                                                                       
import roundedRect from 'rounded-rect'
 
import Display from './display'
 
export default class CanvasDisplay extends Display {
  constructor(transitive) {
    super(transitive)
 
    const { canvas, el } = transitive.options
 
    // Handle case of externally-provided canvas
    if (canvas) {
      // Set internal dimensions to match those of canvas
      this.setDimensions(canvas.width, canvas.height)
      this.setCanvas(canvas)
 
      // We have a DOM element; create canvas
    } else if (el) {
      this.setDimensions(el.clientWidth, el.clientHeight)
 
      const canvas = document.createElement('canvas')
      canvas.width = el.clientWidth
      canvas.height = el.clientHeight
      el.appendChild(canvas)
 
      // Check for Hi-PPI display
      if (window.devicePixelRatio > 1) makeCanvasHiPPI(canvas)
 
      this.setCanvas(canvas)
    }
  }
 
  setCanvas(canvas) {
    this.canvas = canvas
    this.ctx = this.canvas.getContext('2d')
  }
 
  clear() {
    this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)
  }
 
  drawRect(upperLeft, attrs) {
    this.ctx.strokeStyle = attrs.stroke
    this.ctx.lineWidth = attrs['stroke-width']
    this.ctx.fillStyle = attrs.fill
 
    this.ctx.beginPath()
    if (attrs.rx && attrs.ry && attrs.rx === attrs.ry) {
      roundedRect(
        this.ctx,
        upperLeft.x,
        upperLeft.y,
        attrs.width,
        attrs.height,
        attrs.rx
      )
      // TODO: handle case where rx != ry
    } else {
      // ordinary rectangle
      this.ctx.rect(upperLeft.x, upperLeft.y, attrs.width, attrs.height)
    }
    this.ctx.closePath()
 
    if (attrs.fill) this.ctx.fill()
    if (attrs.stroke) this.ctx.stroke()
  }
 
  drawCircle(center, attrs) {
    this.ctx.beginPath()
    this.ctx.arc(center.x, center.y, attrs.r, 0, Math.PI * 2, true)
    this.ctx.closePath()
 
    if (attrs.fill) {
      this.ctx.fillStyle = attrs.fill
      this.ctx.fill()
    }
    if (attrs.stroke) {
      this.ctx.strokeStyle = attrs.stroke
      this.ctx.lineWidth = attrs['stroke-width'] || 1
      this.ctx.stroke()
    }
  }
 
  drawEllipse(center, attrs) {
    // TODO: implement
  }
 
  drawPath(renderData, attrs) {
    if (!renderData || renderData.length === 0) return
 
    // Apply stroke/width styles
    this.ctx.strokeStyle = attrs.stroke
    this.ctx.lineWidth = attrs['stroke-width']
 
    // Apply line-dash style, if present
    if (attrs['stroke-dasharray']) {
      const arr = attrs['stroke-dasharray']
        .split(',')
        .map((str) => parseFloat(str.trim()))
      this.ctx.setLineDash(arr)
    }
 
    // Apply linecap style
    this.ctx.lineCap = attrs['stroke-linecap'] || 'butt'
 
    // Apply 'round' line join for smooth edge rendering.
    // (The default join is 'miter', which causes jagged edges when zoomed away.)
    this.ctx.lineJoin = 'round'
 
    // Draw the path
    this.ctx.beginPath()
    this.ctx.moveTo(renderData[0].x, renderData[0].y)
    for (let i = 1; i < renderData.length; i++) {
      const rd = renderData[i]
      if (rd.arc) this.ctx.arcTo(rd.ex, rd.ey, rd.x, rd.y, rd.radius)
      else this.ctx.lineTo(rd.x, rd.y)
    }
    this.ctx.stroke()
 
    // Revert line-dash style to default
    if (attrs['stroke-dasharray']) this.ctx.setLineDash([])
  }
 
  drawText(text, anchor, attrs) {
    // For equivalence w/ SVG text rendering
    this.ctx.textBaseline = 'top'
 
    this.ctx.font = `${attrs['font-size'] || '14px'} ${
      attrs['font-family'] || 'sans-serif'
    }`
    if (attrs['text-anchor']) this.ctx.textAlign = attrs['text-anchor']
 
    if (attrs.stroke) {
      this.ctx.strokeStyle = attrs.stroke
      if (attrs['stroke-opacity']) {
        this.ctx.globalAlpha = attrs['stroke-opacity']
      }
      this.ctx.lineWidth = attrs['stroke-width'] || 1
      this.ctx.strokeText(text, anchor.x, anchor.y)
    }
    if (attrs.fill) {
      this.ctx.fillStyle = attrs.fill
      if (attrs['fill-opacity']) this.ctx.globalAlpha = attrs['fill-opacity']
      this.ctx.fillText(text, anchor.x, anchor.y)
    }
 
    this.ctx.textAlign = 'start'
    this.ctx.globalAlpha = 1
  }
}
 
// Utility function to support HiPPI displays (e.g. Retina)
function makeCanvasHiPPI(canvas) {
  const PIXEL_RATIO = 2
  canvas.style.width = canvas.width + 'px'
  canvas.style.height = canvas.height + 'px'
 
  canvas.width *= PIXEL_RATIO
  canvas.height *= PIXEL_RATIO
 
  const context = canvas.getContext('2d')
  context.scale(PIXEL_RATIO, PIXEL_RATIO)
}