Atlaslog
Atlaslog / luma.gl / API reference / CanvasContext
luma.gl · API reference

CanvasContext

Quick answer

CanvasContext abstracts a canvas for the device: it maps CSS size to drawing-buffer size, tracks the device pixel ratio, handles resizing and position, and hands back the framebuffer to render into. Create one with device.createCanvasContext and read its width and height instead of the DOM attributes.

Last verified 2026-11-03 · luma.gl 9.4 · Cite this page
device. createCanvasContext ( props ) → CanvasContext
Key points
Example
const device = await luma.luma.createDevice({
  type: 'webgl',
  adapters: [luma.webgl2Adapter],
  createCanvasContext: {
    canvas: 'map-canvas',
    useDevicePixels: Math.min(window.devicePixelRatio, 2),
    autoResize: true
  }
});

const ctx = device.canvasContext;

console.log(ctx.getCSSSize());            // [800, 600]
console.log(ctx.getDrawingBufferSize());  // [1600, 1200],2 倍像素比
console.log(ctx.getDevicePixelRatio());   // 2
canvas.addEventListener('click', function (e) {
  const rect = canvas.getBoundingClientRect();

  const cssPixel = [e.clientX - rect.left, e.clientY - rect.top];
  const devicePixel = ctx.cssToDevicePixels(cssPixel, true);

  onPick(devicePixel.x, devicePixel.y);
});
Notes
Runnable examplesChinese originalAll API references
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Cite this page

Atlaslog (2026). CanvasContext. https://www.jingweizhi.com/en/lumagl/api/canvas-context.html (accessed 2026-11-03).