Atlaslog
Atlaslog / luma.gl / Tutorial / Canvas, sizing and device pixel ratio
luma.gl · Tutorial

Canvas, sizing and device pixel ratio

Quick answer

This tutorial separates the CSS size of a canvas from its drawing-buffer size and shows how the device pixel ratio connects them. It configures createCanvasContext with canvas, useDevicePixels capped at two and autoResize, explains why CSS width and height must be authored, and reads props.width and props.height from AnimationLoop for aspect. It also rebuilds offscreen framebuffers when the viewport changes. Readers can avoid blurry, stretched or misaligned rendering.

Last verified 2026-11-03 · luma.gl 9.4 · Cite this page
Key points
Example
const device = await luma.luma.createDevice({
  type: 'webgl',
  adapters: [luma.webgl2Adapter],
  createCanvasContext: {
    canvas: document.getElementById('cv'),
    useDevicePixels: Math.min(window.devicePixelRatio, 2),
    autoResize: true
  }
});
html, body { height: 100%; margin: 0; }

#cv {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;        /* 触摸设备上不让页面抢走手势 */
}
Notes
Runnable examplesChinese originalAll Tutorials
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Cite this page

Atlaslog (2026). Canvas, sizing and device pixel ratio. https://www.jingweizhi.com/en/lumagl/tutorials/canvas-sizing.html (accessed 2026-11-03).