canvas
Small helpers for creating a canvas element and drawing to its 2D context.
Import: @rtorcato/browser-common/canvas
๐ MDN: Canvas API
Browser supportโ
| API | Chrome | Edge | Firefox | Safari | iOS Safari | Baseline |
|---|---|---|---|---|---|---|
HTMLCanvasElement | โ 1+ | โ 12+ | โ 1.5+ | โ 2+ | โ 1+ | Widely available |
CanvasRenderingContext2D | โ 1+ | โ 12+ | โ 1.5+ | โ 2+ | โ 1+ | Widely available |
โ
supported ยท โ partial ยท โ not supported ยท [n] see note. Generated from MDN browser-compat-data and web-features. caniuse: HTMLCanvasElement ยท CanvasRenderingContext2D
Exampleโ
import {
createCanvas,
fillCanvas,
drawImageOnCanvas,
canvasToDataURL,
} from '@rtorcato/browser-common/canvas'
const canvas = createCanvas(640, 480)
document.body.append(canvas)
fillCanvas(canvas, '#fafafa')
drawImageOnCanvas(canvas, image, 10, 20)
const png = canvasToDataURL(canvas)
Exportsโ
createCanvas(width, height)โ creates anHTMLCanvasElementgetCanvasContext2D(canvas)โ returns the 2D rendering context, ornullclearCanvas(canvas)โ clears the entire canvasdrawImageOnCanvas(canvas, image, dx, dy)โ draws an image onto the canvascanvasToDataURL(canvas, type?, quality?)โ converts canvas content to a data URLfillCanvas(canvas, color)โ fills the entire canvas with a color
See the API reference for full signatures.