All files / src/render canvas-2d-context.render.js

100% Statements 29/29
100% Branches 0/0
100% Functions 0/0
100% Lines 29/29

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 301x 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  
import { getPathData } from './svg.render.js'
import { getDefaultColors } from '../utils/css-colors.util.js'
 
/**
 *
 * @param {object} opts - function parameters
 * @param {CanvasRenderingContext2D} opts.context - canvas rendering context
 * @param {number} [opts.cellSize] - cell size in pixels, defaults to 2
 * @param {number} [opts.margin] - margin in pixels, defaults to {@link cellSize} * 4
 * @param {import('../qr-code.js').QrCode} opts.qrcode - QR Code data
 * @param {import('../utils/css-colors.util.js').QRCodeCssColors} [opts.colors] - qr code colors
 * @param {import('../utils/css-qrcode-style.util.js').QRCodeCssStyles} [opts.style] - qr code colors
 */
export function renderTo2dContext ({ context, margin, cellSize = 2, qrcode, colors = getDefaultColors(), style }) {
  margin ??= cellSize * 4
 
  context.save()
  const pathData = getPathData({ cellSize, margin, qrcode, style })
  context.fillStyle = colors.lightColor
  context.fill(new Path2D(pathData.bg), 'evenodd')
  context.fillStyle = colors.darkColor
  context.fill(new Path2D(pathData.dots))
  context.fillStyle = colors.cornerBorderColor
  context.fill(new Path2D(pathData.finderCorner), 'evenodd')
  context.fillStyle = colors.cornerCenterColor
  context.fill(new Path2D(pathData.finderCenter))
 
  context.restore()
}