|
| 1 | +import roundedRect from 'rounded-rect' |
| 2 | + |
| 3 | +import Display from './display' |
| 4 | + |
| 5 | +export default class CanvasDisplay extends Display { |
| 6 | + constructor (transitive) { |
| 7 | + super(transitive) |
| 8 | + |
| 9 | + const { el, canvas } = transitive.options |
| 10 | + |
| 11 | + // Handle case of externally-provided canvas |
| 12 | + if (canvas) { |
| 13 | + // Set internal dimensions to match those of canvas |
| 14 | + this.setDimensions(canvas.width, canvas.height) |
| 15 | + this.setCanvas(canvas) |
| 16 | + |
| 17 | + // We have a DOM element; create canvas |
| 18 | + } else if (el) { |
| 19 | + this.setDimensions(el.clientWidth, el.clientHeight) |
| 20 | + |
| 21 | + const canvas = document.createElement('canvas') |
| 22 | + canvas.width = el.clientWidth |
| 23 | + canvas.height = el.clientHeight |
| 24 | + el.appendChild(canvas) |
| 25 | + |
| 26 | + // Check for Hi-PPI display |
| 27 | + if (window.devicePixelRatio > 1) makeCanvasHiPPI(canvas) |
| 28 | + |
| 29 | + this.setCanvas(canvas) |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + setCanvas (canvas) { |
| 34 | + this.canvas = canvas |
| 35 | + this.ctx = this.canvas.getContext('2d') |
| 36 | + } |
| 37 | + |
| 38 | + clear () { |
| 39 | + this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height) |
| 40 | + } |
| 41 | + |
| 42 | + drawRect (upperLeft, attrs) { |
| 43 | + this.ctx.strokeStyle = attrs['stroke'] |
| 44 | + this.ctx.lineWidth = attrs['stroke-width'] |
| 45 | + this.ctx.fillStyle = attrs['fill'] |
| 46 | + |
| 47 | + this.ctx.beginPath() |
| 48 | + if (attrs.rx && attrs.ry && attrs.rx === attrs.ry) { |
| 49 | + roundedRect(this.ctx, upperLeft.x, upperLeft.y, attrs.width, attrs.height, attrs.rx) |
| 50 | + // TODO: handle case where rx != ry |
| 51 | + } else { // ordinary rectangle |
| 52 | + this.ctx.rect(upperLeft.x, upperLeft.y, attrs.width, attrs.height) |
| 53 | + } |
| 54 | + this.ctx.closePath() |
| 55 | + |
| 56 | + if (attrs['fill']) this.ctx.fill() |
| 57 | + if (attrs['stroke']) this.ctx.stroke() |
| 58 | + } |
| 59 | + |
| 60 | + drawCircle (center, attrs) { |
| 61 | + this.ctx.beginPath() |
| 62 | + this.ctx.arc(center.x, center.y, attrs.r, 0, Math.PI * 2, true) |
| 63 | + this.ctx.closePath() |
| 64 | + |
| 65 | + if (attrs['fill']) { |
| 66 | + this.ctx.fillStyle = attrs['fill'] |
| 67 | + this.ctx.fill() |
| 68 | + } |
| 69 | + if (attrs['stroke']) { |
| 70 | + this.ctx.strokeStyle = attrs['stroke'] |
| 71 | + this.ctx.lineWidth = attrs['stroke-width'] || 1 |
| 72 | + this.ctx.stroke() |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + drawEllipse (center, attrs) { |
| 77 | + // TODO: implement |
| 78 | + } |
| 79 | + |
| 80 | + drawPath (renderData, attrs) { |
| 81 | + if (!renderData || renderData.length === 0) return |
| 82 | + |
| 83 | + // Apply stroke/width styles |
| 84 | + this.ctx.strokeStyle = attrs['stroke'] |
| 85 | + this.ctx.lineWidth = attrs['stroke-width'] |
| 86 | + |
| 87 | + // Apply line-dash style, if present |
| 88 | + if (attrs['stroke-dasharray']) { |
| 89 | + const arr = attrs['stroke-dasharray'].split(',').map(str => parseFloat(str.trim())) |
| 90 | + this.ctx.setLineDash(arr) |
| 91 | + } |
| 92 | + |
| 93 | + // Apply linecap style |
| 94 | + this.ctx.lineCap = attrs['stroke-linecap'] || 'butt' |
| 95 | + |
| 96 | + // Draw the path |
| 97 | + this.ctx.beginPath() |
| 98 | + this.ctx.moveTo(renderData[0].x, renderData[0].y) |
| 99 | + for (let i = 1; i < renderData.length; i++) { |
| 100 | + const rd = renderData[i] |
| 101 | + if (rd.arc) this.ctx.arcTo(rd.ex, rd.ey, rd.x, rd.y, rd.radius) |
| 102 | + else this.ctx.lineTo(rd.x, rd.y) |
| 103 | + } |
| 104 | + this.ctx.stroke() |
| 105 | + |
| 106 | + // Revert line-dash style to default |
| 107 | + if (attrs['stroke-dasharray']) this.ctx.setLineDash([]) |
| 108 | + } |
| 109 | + |
| 110 | + drawText (text, anchor, attrs) { |
| 111 | + // For equivalence w/ SVG text rendering |
| 112 | + this.ctx.textBaseline = 'top' |
| 113 | + |
| 114 | + this.ctx.font = `${attrs.fontSize || '14px'} ${attrs.fontFamily || 'sans-serif'}` |
| 115 | + if (attrs['text-anchor']) this.ctx.textAlign = attrs['text-anchor'] |
| 116 | + |
| 117 | + if (attrs['stroke']) { |
| 118 | + this.ctx.strokeStyle = attrs['stroke'] |
| 119 | + if (attrs['stroke-opacity']) this.ctx.globalAlpha = attrs['stroke-opacity'] |
| 120 | + this.ctx.lineWidth = attrs['stroke-width'] || 1 |
| 121 | + this.ctx.strokeText(text, anchor.x, anchor.y) |
| 122 | + } |
| 123 | + if (attrs['fill']) { |
| 124 | + this.ctx.fillStyle = attrs['fill'] |
| 125 | + if (attrs['fill-opacity']) this.ctx.globalAlpha = attrs['fill-opacity'] |
| 126 | + this.ctx.fillText(text, anchor.x, anchor.y) |
| 127 | + } |
| 128 | + |
| 129 | + this.ctx.textAlign = 'start' |
| 130 | + this.ctx.globalAlpha = 1 |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +// Utility function to support HiPPI displays (e.g. Retina) |
| 135 | +function makeCanvasHiPPI (canvas) { |
| 136 | + const PIXEL_RATIO = 2 |
| 137 | + canvas.style.width = canvas.width + 'px' |
| 138 | + canvas.style.height = canvas.height + 'px' |
| 139 | + |
| 140 | + canvas.width *= PIXEL_RATIO |
| 141 | + canvas.height *= PIXEL_RATIO |
| 142 | + |
| 143 | + var context = canvas.getContext('2d') |
| 144 | + context.scale(PIXEL_RATIO, PIXEL_RATIO) |
| 145 | +} |
0 commit comments