// Draws the active document into the workspace canvas: checkerboard, composite, // pixel grid, selection outline ("marching ants") and tool overlays. import { createCanvas, clamp } from '../core/util.js'; import { compositeDocument } from './compositor.js'; import { RULER } from './rulers.js'; export const MIN_ZOOM = 0.02; export const MAX_ZOOM = 64; export class Renderer { constructor(app, container) { this.app = app; this.container = container; this.canvas = document.createElement('canvas'); this.canvas.className = 'view-canvas'; container.appendChild(this.canvas); this.ctx = this.canvas.getContext('2d'); this.composite = createCanvas(1, 1); this.lastVersion = -1; this.lastDoc = null; this.antsOffset = 0; this.pending = false; this.pointer = null; // last pointer position in screen (CSS px) coords this.showPixelGrid = true; this.dpr = window.devicePixelRatio || 1; this.checker = this._makeChecker(); new ResizeObserver(() => this.resize()).observe(container); this.resize(); setInterval(() => { const doc = app.doc; if (doc && (doc.selection || app.tool?.animatedOverlay)) { this.antsOffset = (this.antsOffset + 1) % 16; this.request(); } }, 90); } _makeChecker() { const c = createCanvas(16, 16); const ctx = c.getContext('2d'); ctx.fillStyle = '#ffffff'; ctx.fillRect(0, 0, 16, 16); ctx.fillStyle = '#d6d6d6'; ctx.fillRect(0, 0, 8, 8); ctx.fillRect(8, 8, 8, 8); return this.ctx.createPattern(c, 'repeat'); } get width() { return this.canvas.width / this.dpr; } get height() { return this.canvas.height / this.dpr; } resize() { const r = this.container.getBoundingClientRect(); this.dpr = window.devicePixelRatio || 1; this.canvas.width = Math.max(1, Math.round(r.width * this.dpr)); this.canvas.height = Math.max(1, Math.round(r.height * this.dpr)); this.canvas.style.width = `${r.width}px`; this.canvas.style.height = `${r.height}px`; const doc = this.app.doc; if (doc && doc.view.needsFit) this.fit(doc); this.request(); } request() { if (this.pending) return; this.pending = true; requestAnimationFrame(() => { this.pending = false; this.draw(); }); } // ---- coordinates ---- clientToScreen(e) { const r = this.canvas.getBoundingClientRect(); return { x: e.clientX - r.left, y: e.clientY - r.top }; } screenToDoc(p, doc = this.app.doc) { const v = doc.view; return { x: (p.x - v.panX) / v.zoom, y: (p.y - v.panY) / v.zoom }; } docToScreen(p, doc = this.app.doc) { const v = doc.view; return { x: p.x * v.zoom + v.panX, y: p.y * v.zoom + v.panY }; } fit(doc = this.app.doc, allowUpscale = false) { if (!doc) return; const pad = 32; const inset = this.app.guides?.rulers ? RULER : 0; // keep the image clear of the rulers const w = this.width - inset, h = this.height - inset; if (w < 10 || h < 10) return; let z = Math.min((w - pad * 2) / doc.width, (h - pad * 2) / doc.height); if (!allowUpscale) z = Math.min(1, z); z = clamp(z, MIN_ZOOM, MAX_ZOOM); doc.view.zoom = z; doc.view.panX = inset + Math.round((w - doc.width * z) / 2); doc.view.panY = inset + Math.round((h - doc.height * z) / 2); doc.view.needsFit = false; this.request(); this.app.emit('view'); } setZoom(z, anchor = null, doc = this.app.doc) { if (!doc) return; z = clamp(z, MIN_ZOOM, MAX_ZOOM); const a = anchor || { x: this.width / 2, y: this.height / 2 }; const d = this.screenToDoc(a, doc); doc.view.zoom = z; doc.view.panX = a.x - d.x * z; doc.view.panY = a.y - d.y * z; this.request(); this.app.emit('view'); } /** Step through "nice" zoom levels. */ zoomStep(dir, anchor) { const doc = this.app.doc; if (!doc) return; const levels = [0.02, 0.03, 0.05, 0.0625, 0.083, 0.125, 0.167, 0.25, 0.333, 0.5, 0.667, 1, 1.5, 2, 3, 4, 5, 6, 8, 12, 16, 24, 32, 48, 64]; const z = doc.view.zoom; let next; if (dir > 0) next = levels.find((l) => l > z * 1.001) ?? MAX_ZOOM; else next = [...levels].reverse().find((l) => l < z * 0.999) ?? MIN_ZOOM; this.setZoom(next, anchor); } pan(dx, dy) { const doc = this.app.doc; if (!doc) return; doc.view.panX += dx; doc.view.panY += dy; this.request(); this.app.emit('view'); } // ---- drawing ---- draw() { const { ctx, app } = this; const doc = app.doc; ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.clearRect(0, 0, this.canvas.width, this.canvas.height); if (!doc) return; if (doc.view.needsFit) this.fit(doc); if (this.lastDoc !== doc || this.lastVersion !== doc.version || app.forceComposite) { compositeDocument(doc, this.composite); this.lastDoc = doc; this.lastVersion = doc.version; app.forceComposite = false; } const { zoom, panX, panY } = doc.view; const dpr = this.dpr; ctx.setTransform(dpr, 0, 0, dpr, 0, 0); // Drop shadow + checkerboard behind the document. const dw = doc.width * zoom, dh = doc.height * zoom; ctx.save(); ctx.shadowColor = 'rgba(0,0,0,0.5)'; ctx.shadowBlur = 12; ctx.fillStyle = this.checker; ctx.translate(panX, panY); ctx.fillRect(0, 0, dw, dh); ctx.restore(); ctx.save(); ctx.imageSmoothingEnabled = zoom < 1.5; ctx.imageSmoothingQuality = 'high'; ctx.drawImage(this.composite, panX, panY, dw, dh); ctx.restore(); if (this.showPixelGrid && zoom >= 10) this._drawPixelGrid(doc); if (doc.guides.show) this._drawGrid(doc); app.guides?.drawGuides(ctx, this, doc); if (doc.selection && !app.tool?.hidesSelection) this._drawAnts(doc); ctx.save(); try { app.tool?.drawOverlay?.(ctx, this, doc); } catch (err) { console.error(err); } ctx.restore(); this._drawCursor(doc); app.guides?.drawOverlay(ctx, this, doc); } _visibleDocRect(doc) { const a = this.screenToDoc({ x: 0, y: 0 }, doc); const b = this.screenToDoc({ x: this.width, y: this.height }, doc); return { x0: clamp(Math.floor(a.x), 0, doc.width), y0: clamp(Math.floor(a.y), 0, doc.height), x1: clamp(Math.ceil(b.x), 0, doc.width), y1: clamp(Math.ceil(b.y), 0, doc.height), }; } _drawPixelGrid(doc) { const { ctx } = this; const { zoom, panX, panY } = doc.view; const r = this._visibleDocRect(doc); ctx.save(); ctx.strokeStyle = 'rgba(128,128,128,0.35)'; ctx.lineWidth = 1; ctx.beginPath(); for (let x = r.x0; x <= r.x1; x++) { const sx = Math.round(x * zoom + panX) + 0.5; ctx.moveTo(sx, r.y0 * zoom + panY); ctx.lineTo(sx, r.y1 * zoom + panY); } for (let y = r.y0; y <= r.y1; y++) { const sy = Math.round(y * zoom + panY) + 0.5; ctx.moveTo(r.x0 * zoom + panX, sy); ctx.lineTo(r.x1 * zoom + panX, sy); } ctx.stroke(); ctx.restore(); } _drawGrid(doc) { const { ctx } = this; const { zoom, panX, panY } = doc.view; const step = doc.guides.grid; if (step * zoom < 4) return; const r = this._visibleDocRect(doc); ctx.save(); ctx.strokeStyle = 'rgba(0,160,255,0.45)'; ctx.lineWidth = 1; ctx.beginPath(); for (let x = Math.ceil(r.x0 / step) * step; x <= r.x1; x += step) { const sx = Math.round(x * zoom + panX) + 0.5; ctx.moveTo(sx, r.y0 * zoom + panY); ctx.lineTo(sx, r.y1 * zoom + panY); } for (let y = Math.ceil(r.y0 / step) * step; y <= r.y1; y += step) { const sy = Math.round(y * zoom + panY) + 0.5; ctx.moveTo(r.x0 * zoom + panX, sy); ctx.lineTo(r.x1 * zoom + panX, sy); } ctx.stroke(); ctx.restore(); } _drawAnts(doc) { const { ctx } = this; const { zoom, panX, panY } = doc.view; const path = doc.selection.outline; ctx.save(); ctx.translate(panX, panY); ctx.scale(zoom, zoom); ctx.lineWidth = 1 / zoom; ctx.strokeStyle = '#000'; ctx.setLineDash([]); ctx.stroke(path); ctx.strokeStyle = '#fff'; ctx.setLineDash([4 / zoom, 4 / zoom]); ctx.lineDashOffset = -this.antsOffset / zoom / 2; ctx.stroke(path); ctx.restore(); } _drawCursor(doc) { const tool = this.app.tool; if (!this.pointer || !tool?.cursorRadius) return; const r = tool.cursorRadius(doc) * doc.view.zoom; if (r < 2) return; const { ctx } = this; ctx.save(); ctx.beginPath(); ctx.arc(this.pointer.x, this.pointer.y, r, 0, Math.PI * 2); ctx.lineWidth = 1; ctx.strokeStyle = 'rgba(0,0,0,0.8)'; ctx.stroke(); ctx.beginPath(); ctx.arc(this.pointer.x, this.pointer.y, Math.max(1, r - 1), 0, Math.PI * 2); ctx.strokeStyle = 'rgba(255,255,255,0.8)'; ctx.stroke(); ctx.restore(); } // ---- overlay helpers for tools ---- /** Stroke a doc-space rectangle/path with a two-tone outline. */ outlineRect(r, doc = this.app.doc) { const a = this.docToScreen({ x: r.x, y: r.y }, doc); const b = this.docToScreen({ x: r.x + r.w, y: r.y + r.h }, doc); const { ctx } = this; ctx.save(); ctx.lineWidth = 1; ctx.strokeStyle = '#000'; ctx.strokeRect(Math.round(a.x) + 0.5, Math.round(a.y) + 0.5, Math.round(b.x - a.x), Math.round(b.y - a.y)); ctx.setLineDash([4, 4]); ctx.lineDashOffset = -this.antsOffset / 2; ctx.strokeStyle = '#fff'; ctx.strokeRect(Math.round(a.x) + 0.5, Math.round(a.y) + 0.5, Math.round(b.x - a.x), Math.round(b.y - a.y)); ctx.restore(); } outlinePath(path, doc = this.app.doc) { const { ctx } = this; const { zoom, panX, panY } = doc.view; ctx.save(); ctx.translate(panX, panY); ctx.scale(zoom, zoom); ctx.lineWidth = 1 / zoom; ctx.strokeStyle = '#000'; ctx.stroke(path); ctx.setLineDash([4 / zoom, 4 / zoom]); ctx.lineDashOffset = -this.antsOffset / zoom / 2; ctx.strokeStyle = '#fff'; ctx.stroke(path); ctx.restore(); } handle(p, size = 8, doc = this.app.doc) { const s = this.docToScreen(p, doc); const { ctx } = this; ctx.save(); ctx.fillStyle = '#fff'; ctx.strokeStyle = '#1a73e8'; ctx.lineWidth = 1.5; ctx.fillRect(s.x - size / 2, s.y - size / 2, size, size); ctx.strokeRect(s.x - size / 2, s.y - size / 2, size, size); ctx.restore(); } }