CSS 개발자를 위한 GPU 2D 에디터 만들기
Guide
CSS Matrix
Guide
CSS Matrix
  • Part 0. CSS에서 GPU로 사고 전환

    • CSS 박스가 사라지면 무엇이 남는가
    • Canvas는 DOM이 아니라 픽셀 버퍼다
    • CSS pixel, device pixel, backing store
    • scene model과 renderer 분리하기
    • retained mode vs immediate mode
    • DOM event target 없이 hit testing 하기
  • Part 1. Canvas editor의 기본 뼈대

    • render loop와 frame lifecycle
    • viewport와 camera
    • screen/world/local 좌표계 복습
    • cursor anchored zoom
    • grid와 ruler를 canvas에 그리기
    • overlay layer와 control layer 분리
  • Part 2. WebGL 2D renderer

    • WebGL context와 첫 triangle
    • clip space와 화면 좌표
    • shader를 CSS transform 관점으로 읽기
    • rectangle을 두 triangle로 그리기
    • matrix uniform으로 camera 적용하기
    • 색상, alpha, blending
    • 여러 rectangle을 buffer에 담기
    • batching과 draw call 줄이기
    • instanced rectangle renderer
    • line/outline renderer
  • Part 3. Editor tool math

    • pointer 좌표를 world 좌표로 바꾸기
    • CPU hit testing
    • selection bounds
    • hover outline
    • move tool
    • resize handles
    • rotation handle
    • marquee selection
    • snapping과 smart guides
    • group transform
  • Part 4. Figma-like scene graph

    • node tree 설계
    • transform inheritance
    • layer order와 z sorting
    • frame과 clipping
    • fill, stroke, effect 모델
    • command model과 undo/redo
    • JSON export/import
    • renderer-independent editor core
  • Part 5. Image, text, vector

    • texture로 image node 그리기
    • texture atlas 기본
    • text rendering 전략
    • SVG/text를 GPU editor에서 다루는 경계
    • vector path는 어디까지 직접 구현할 것인가
  • Part 6. WebGPU로 옮기기

    • WebGPU adapter/device/context
    • WGSL과 render pipeline
    • WebGL renderer를 WebGPU renderer로 바꾸기
    • uniform buffer와 bind group
    • WebGPU instancing
    • WebGL/WebGPU fallback 전략
  • Part 7. Capstone

    • editor shell 만들기
    • toolbar / layer panel / inspector 연결
    • mini Figma-like editor 완성
    • 성능 점검과 디버깅
    • 배포와 브라우저 호환성 체크
  • Part 8. Three.js로 WebGL 개발하기

    • Three.js를 WebGL renderer로 쓰는 기준
    • Scene, Camera, Renderer, render loop
    • OrthographicCamera로 2D editor 좌표계 만들기
    • BufferGeometry, Material, ShaderMaterial
    • Raycaster와 editor picking
    • Three.js renderer를 editor core 뒤에 붙이기
    • Three.js 프로젝트 세팅과 renderer lifecycle
    • Object3D transform과 editor scene graph 매핑
    • InstancedMesh로 많은 rectangle 그리기
    • Texture, CanvasTexture, Sprite로 이미지/텍스트 다루기
    • Three.js에서 outline, selection, overlay 만들기
    • dispose, cache, renderer.info로 성능 관리하기
    • WebGPURenderer와 TSL로 넘어가는 길
    • RenderTarget을 이용한 picking buffer
    • Three.js를 쓰면 안 좋은 경우
  • Appendix A. GPU editor debugging

    • WebGL/WebGPU 디버그 overlay 만들기
    • 좌표계, matrix, bounds readout 설계
    • frame time, draw call, buffer upload 측정하기
    • Spector.js / Chrome DevTools로 WebGL 프레임 보기
  • Appendix B. Browser and GPU compatibility

    • WebGL/WebGPU feature detection 체크리스트
    • DPR, resize, context lost 처리
    • Safari/Chrome/Firefox 차이와 fallback 정책
    • GPU memory와 texture size 제한
  • Appendix C. Asset pipeline

    • 이미지 로딩, ImageBitmap, texture upload
    • SVG를 texture로 쓸지 vector로 유지할지
    • 폰트 로딩과 text metrics
    • export용 PNG/SVG/JSON 생성 전략
  • Appendix D. Interaction polish and motion

    • inertial pan과 smooth zoom
    • snapping feedback animation
    • selection/hover transition
    • timeline 없이 필요한 최소 모션 수학
  • Appendix E. Production architecture

    • renderer worker / OffscreenCanvas를 고려하는 기준
    • document model versioning과 migration
    • plugin architecture와 command API
    • test 가능한 renderer abstraction 만들기
  • Appendix F. 2D renderer engine patterns

    • renderable type 선택: shape, sprite, mesh
    • static subtree를 texture cache로 굽기
    • render layer와 render group 설계
    • viewport culling과 spatial index
    • clipping 구현: scissor, stencil, mask texture
    • filters와 blend modes가 batch를 깨는 이유
    • interactivity budget: pickable, hitArea, skip children
    • texture GC와 idle resource eviction
    • dynamic text update 비용과 bitmap/glyph 전략
    • Canvas/WebGL editor의 accessibility layer
  • Appendix G. Rendering editor production gaps

    • render invalidation과 dirty flag
    • color space, premultiplied alpha, export 색상
    • stroke join/cap/dash/fill rule
    • editable text: DOM overlay, IME, caret, metrics
    • tool state machine과 pointer capture
    • pixel test와 renderer regression test

overlay layer와 control layer 분리

Figma 같은 에디터 화면에는 실제 문서 도형만 있는 것이 아닙니다. 선택 박스, hover outline, resize handle, ruler, toolbar, inspector가 함께 있습니다.

이것들을 모두 같은 방식으로 그리면 금방 혼란스러워집니다.

세 레이어를 따로 렌더링하는 코드

content는 GPU renderer가 그리고, overlay는 같은 camera를 기준으로 screen 좌표에 그리며, controls는 DOM UI로 둡니다.

function renderEditorFrame(editor) {
  const snapshot = editor.getSnapshot();

  contentRenderer.render({
    scene: snapshot.scene,
    camera: snapshot.camera
  });

  overlayRenderer.clear();
  overlayRenderer.drawSelection({
    selection: snapshot.selection,
    scene: snapshot.scene,
    camera: snapshot.camera
  });
  overlayRenderer.drawGuides(snapshot.tool.guides);

  controls.render({
    tool: snapshot.tool.current,
    selection: snapshot.selection,
    documentName: snapshot.document.name
  });
}

selection handle처럼 화면 크기가 고정되어야 하는 요소는 world 크기가 아니라 screen pixel 크기로 그립니다.

function drawHandle(ctx, screenPoint) {
  const size = 8;
  ctx.fillRect(screenPoint.x - size / 2, screenPoint.y - size / 2, size, size);
}

selection bounds를 screen으로 투영한다

overlay는 문서 geometry를 읽지만 최종 draw는 screen 좌표에서 하는 편이 단순합니다.

function worldBoundsToScreen(bounds, camera) {
  const a = worldToScreen({ x: bounds.x, y: bounds.y }, camera);
  const b = worldToScreen({
    x: bounds.x + bounds.width,
    y: bounds.y + bounds.height
  }, camera);

  return {
    x: Math.min(a.x, b.x),
    y: Math.min(a.y, b.y),
    width: Math.abs(b.x - a.x),
    height: Math.abs(b.y - a.y)
  };
}

function drawSelectionOverlay(ctx, selectionBounds, camera) {
  const screen = worldBoundsToScreen(selectionBounds, camera);
  ctx.strokeStyle = "#0d9488";
  ctx.lineWidth = 1;
  ctx.strokeRect(screen.x, screen.y, screen.width, screen.height);

  for (const point of selectionHandlePoints(screen)) {
    drawHandle(ctx, point);
  }
}

handle 위치는 screen bounds에서 바로 계산합니다.

function selectionHandlePoints(bounds) {
  const x0 = bounds.x;
  const x1 = bounds.x + bounds.width / 2;
  const x2 = bounds.x + bounds.width;
  const y0 = bounds.y;
  const y1 = bounds.y + bounds.height / 2;
  const y2 = bounds.y + bounds.height;

  return [
    { x: x0, y: y0 },
    { x: x1, y: y0 },
    { x: x2, y: y0 },
    { x: x2, y: y1 },
    { x: x2, y: y2 },
    { x: x1, y: y2 },
    { x: x0, y: y2 },
    { x: x0, y: y1 }
  ];
}

content layer는 문서 자체다

content layer에는 사용자가 만든 도형을 그립니다.

rect
image
text
frame
group

이 레이어는 camera의 영향을 받습니다. 사용자가 zoom하면 도형도 같이 커지고, pan하면 같이 움직입니다.

overlay layer는 편집 보조선이다

selection outline, smart guide, resize handle 같은 것은 문서 도형이 아닙니다. 문서를 편집하기 위해 화면 위에 얹는 정보입니다.

overlay는 보통 world 정보를 기반으로 계산하지만, 최종적으로는 screen 좌표에 그리는 편이 다루기 쉽습니다.

selected node world bounds
  -> camera
  -> screen bounds
  -> draw outline and handles

handle 크기는 zoom에 따라 커지면 안 됩니다. 도형을 400% 확대해도 resize handle이 화면에서 32px짜리 거대한 점이 되면 이상합니다. 그래서 handle은 screen pixel 기준 크기를 유지합니다.

function drawOverlayFrame(ctx, snapshot) {
  ctx.clearRect(0, 0, snapshot.viewport.width, snapshot.viewport.height);

  if (snapshot.selection.bounds) {
    drawSelectionOverlay(ctx, snapshot.selection.bounds, snapshot.camera);
  }

  for (const guide of snapshot.tool.guides) {
    drawGuide(ctx, guide, snapshot.camera);
  }
}

여기서 selection.bounds는 world 기준이고, handle size는 screen 기준입니다. 이 둘을 섞지 않는 것이 overlay 구현의 핵심입니다.

control layer는 앱 UI다

toolbar, layer panel, property inspector는 문서 좌표계와 무관합니다. 이들은 일반 DOM UI로 두는 편이 좋습니다.

content: WebGL/WebGPU canvas
overlay: canvas/SVG/DOM overlay
controls: DOM app UI

처음부터 모든 것을 GPU로 그릴 필요는 없습니다. 그래픽 문서는 GPU로 그리고, 앱 UI는 DOM으로 만드는 혼합 구조가 현실적입니다.

오늘의 핵심

에디터 화면은 하나의 canvas처럼 보여도 내부적으로는 역할이 다릅니다.

content layer: camera 적용, 문서 도형
overlay layer: 편집 보조, screen pixel 기준 요소 포함
control layer: 앱 UI, DOM으로 유지

이 분리를 해두면 selection, handle, toolbar가 서로의 좌표계를 망가뜨리지 않습니다.

최근 수정: 26. 5. 16. PM 12:53
Contributors: jinho.park.s3
Prev
grid와 ruler를 canvas에 그리기