UNPKG

@wayrisignals/kicad-webview

Version:

Interactive KiCad schematic, PCB and 3D viewer for the web, with Gerber, Excellon, linked views and offline HTML exports

842 lines • 59.6 kB
/* Copyright (c) 2026 wayri. SPDX-License-Identifier: MPL-2.0 */
/* Owned browser Canvas2D engine. Geometry and source coordinates are in millimetres. */

((root) => {

  "use strict";

  const MOTION_MIN_PRIMITIVES=4096;
  const DENSE_PREVIEW_MIN_PRIMITIVES=20000;
  const DENSE_PREVIEW_PAINT_LIMIT=2800;
  const MOTION_SNAPSHOT_MAX_PIXELS=4000000;

  const finitePoint = p => Number.isFinite(p?.x) && Number.isFinite(p?.y);
  // Bound backing stores on high-DPI tablets without changing world/CSS units.
  function canvasViewport(width,height,deviceRatio=1){
    const w=Number.isFinite(width)&&width>0?width:1,h=Number.isFinite(height)&&height>0?height:1;
    const dpr=Number.isFinite(deviceRatio)&&deviceRatio>0?deviceRatio:1;
    const ratio=Math.min(dpr,3,Math.sqrt(8_000_000/w/h),8192/w,8192/h);
    return {ratio,width:Math.max(1,Math.floor(w*ratio)),height:Math.max(1,Math.floor(h*ratio))};
  }
  const schematicPalettes=Object.freeze({
    dark:Object.freeze({'#000000':'#e8edf5','#840000':'#f2bb74','#a90000':'#ff8587','#008400':'#59dba8','#008484':'#65c6ed','#000080':'#71aaff','#a0a0a0':'#718298','#725600':'#8bbcff'}),
    light:Object.freeze({'#000000':'#283447','#840000':'#8e4358','#a90000':'#9a4051','#008400':'#217864','#008484':'#2c6f89','#000080':'#3e64a3','#a0a0a0':'#a3adbb','#725600':'#48678f'})
  });
  function schematicColor(color,dark){return schematicPalettes[dark?'dark':'light'][String(color).toLowerCase()]||color;}

  function arcThrough(a,b,c) {

    if (![a,b,c].every(finitePoint)) return null;

    const d=2*(a.x*(b.y-c.y)+b.x*(c.y-a.y)+c.x*(a.y-b.y));

    if (Math.abs(d)<1e-10) return null;

    const aa=a.x*a.x+a.y*a.y,bb=b.x*b.x+b.y*b.y,cc=c.x*c.x+c.y*c.y;

    const x=(aa*(b.y-c.y)+bb*(c.y-a.y)+cc*(a.y-b.y))/d,y=(aa*(c.x-b.x)+bb*(a.x-c.x)+cc*(b.x-a.x))/d;

    const start=Math.atan2(a.y-y,a.x-x),mid=Math.atan2(b.y-y,b.x-x),end=Math.atan2(c.y-y,c.x-x);

    const turn=n=>(n+Math.PI*4)%(Math.PI*2);

    return {x,y,r:Math.hypot(a.x-x,a.y-y),start,end,ccw:turn(mid-start)>turn(end-start)};

  }

  function points(p) {

    if(p.type==='arc'){const a=arcThrough(p.start,p.mid,p.end);if(a){let delta=a.end-a.start;if(a.ccw){while(delta>0)delta-=Math.PI*2;}else{while(delta<0)delta+=Math.PI*2;}return Array.from({length:33},(_,i)=>({x:a.x+Math.cos(a.start+delta*i/32)*a.r,y:a.y+Math.sin(a.start+delta*i/32)*a.r}));}return [p.start,p.end].filter(finitePoint);}

    if(p.points)return p.points.filter(finitePoint);

    if(p.type==='circle'||p.type==='pad'&&p.r)return [{x:p.x-p.r,y:p.y-p.r},{x:p.x+p.r,y:p.y+p.r}];

    if(p.type==='text'){const size=p.fontHeight||p.size||1.27,lines=String(p.text||'').split('\n'),w=Math.max(...lines.map(line=>Array.from(line).length))*(p.fontWidth||size)*.65,h=size*1.4,span=(lines.length-1)*size*(p.lineSpacing||1.5)/2,angle=(p.angle||0)*Math.PI/180,left=p.align==='left'?0:p.align==='right'?-w:-w/2,top=(p.baseline==='top'?0:p.baseline==='bottom'?-h:-h/2)-span,bottom=top+h+span*2;return [{x:left,y:top},{x:left+w,y:top},{x:left+w,y:bottom},{x:left,y:bottom}].map(q=>({x:p.x+q.x*Math.cos(angle)-q.y*Math.sin(angle),y:p.y+q.x*Math.sin(angle)+q.y*Math.cos(angle)}));}

    return finitePoint(p)?[p]:[];

  }

  function bounds(primitives, includeFrame=false){

    let minX=Infinity,minY=Infinity,maxX=-Infinity,maxY=-Infinity;

    for (const p of primitives) {
      if (p.hidden || p.frame && !includeFrame) continue;
      const pad=(p.width||0)/2;
      if (p.zoneBounds) {
        const {x,y,w,h}=p.zoneBounds;
        minX=Math.min(minX,x-pad); minY=Math.min(minY,y-pad);
        maxX=Math.max(maxX,x+w+pad); maxY=Math.max(maxY,y+h+pad);
      } else for(const q of points(p)){
        minX=Math.min(minX,q.x-pad);minY=Math.min(minY,q.y-pad);
        maxX=Math.max(maxX,q.x+pad);maxY=Math.max(maxY,q.y+pad);
      }
    }

    return minX===Infinity?null:{x:minX,y:minY,w:Math.max(.01,maxX-minX),h:Math.max(.01,maxY-minY)};

  }

  function segmentDistance(p,a,b){const dx=b.x-a.x,dy=b.y-a.y,l=dx*dx+dy*dy,t=l?Math.max(0,Math.min(1,((p.x-a.x)*dx+(p.y-a.y)*dy)/l)):0;return Math.hypot(p.x-a.x-t*dx,p.y-a.y-t*dy);}

  function inside(p,poly){let hit=false;for(let i=0,j=poly.length-1;i<poly.length;j=i++){const a=poly[i],b=poly[j];if((a.y>p.y)!==(b.y>p.y)&&p.x<(b.x-a.x)*(p.y-a.y)/(b.y-a.y)+a.x)hit=!hit;}return hit;}

  function zoneDistance(p, at) {
    const xy = p.zoneCoordinates;
    const a = {x:xy[xy.length-2],y:xy[xy.length-1]}, b = {x:0,y:0};
    let insideZone=false, closest=Infinity;
    for (let i=0;i<xy.length;i+=2) {
      b.x=xy[i]; b.y=xy[i+1];
      if ((a.y>at.y)!==(b.y>at.y) && at.x<(b.x-a.x)*(at.y-a.y)/(b.y-a.y)+a.x) insideZone=!insideZone;
      closest=Math.min(closest,segmentDistance(at,a,b));
      a.x=b.x; a.y=b.y;
    }
    return p.fill && insideZone ? 0 : Math.max(0,closest-(p.width||.1)/2);
  }

  function hitPrimitive(p,at,tolerance){

    if(p.hole||p.frame)return false;
    if(p.zoneCoordinates)return zoneDistance(p,at)<=tolerance;

    if(p.type==='circle'||p.type==='pad'&&p.r){const dist=Math.hypot(at.x-p.x,at.y-p.y);return p.fill?dist<=p.r+tolerance:Math.abs(dist-p.r)<=tolerance+(p.width||.1)/2;}

    const poly=points(p);

    // Outline graphics own their stroke, not the objects enclosed by it.
    if((p.type==='pad'||p.type==='text'||p.type==='image'||p.type==='bezier'&&p.fill||p.type==='polygon'&&(p.fill||['symbol','footprint','footprint-hit','sheet','global_label','hierarchical_label','sheet-pin'].includes(p.kind)))&&inside(at,poly))return true;

    const limit=tolerance+(p.width||.1)/2;

    for(let i=1;i<poly.length;i++)if(segmentDistance(at,poly[i-1],poly[i])<=limit)return true;

    return p.type==='polygon'&&poly.length>2&&segmentDistance(at,poly.at(-1),poly[0])<=limit;

  }

  function primitiveDistance(p,at,tolerance=.01){
    if(!p||p.frame||p.hole)return Infinity;
    if(p.zoneCoordinates)return zoneDistance(p,at);
    if(p.type==='circle'||p.type==='pad'&&p.r){const distance=Math.hypot(at.x-p.x,at.y-p.y);return Math.max(0,(p.fill?distance-p.r:Math.abs(distance-p.r))-(p.width||.1)/2);}
    const path=points(p);let distance=Infinity;
    if((['pad','text','image'].includes(p.type)||p.type==='bezier'&&p.fill||p.type==='polygon'&&(p.fill||['symbol','footprint','footprint-hit','sheet','global_label','hierarchical_label','sheet-pin'].includes(p.kind)))&&inside(at,path))return 0;
    for(let i=1;i<path.length;i++)distance=Math.min(distance,segmentDistance(at,path[i-1],path[i]));
    if(['polygon','pad','image','text'].includes(p.type)&&path.length>2)distance=Math.min(distance,segmentDistance(at,path.at(-1),path[0]));
    return Math.max(0,distance-(p.width||.1)/2);
  }

  // Saved geometry is immutable between primitive-array replacements. Keep a
  // bounded grid, with large shapes in an overflow list rather than duplicating
  // them across thousands of cells. Text remains conservative for font metrics.
  function spatialBounds(p){
    if(p.type==='text')return null;
    if(p.zoneBounds){const {x,y,w,h}=p.zoneBounds,pad=(p.width||.15)/2;return{x:x-pad,y:y-pad,w:w+pad*2,h:h+pad*2};}
    if(p.type==='arc'){const a=arcThrough(p.start,p.mid,p.end);if(a){const r=a.r+(p.width||.15)/2;return {x:a.x-r,y:a.y-r,w:2*r,h:2*r};}}
    const b=bounds([{...p,hidden:false,width:p.width||.15}],true);
    return b&&[b.x,b.y,b.w,b.h].every(Number.isFinite)?b:null;
  }
  class SpatialIndex {
    constructor(primitives){
      this.primitives=primitives;this.length=primitives.length;this.cells=new Map();this.overflow=[];
      const boxes=primitives.map(spatialBounds);let x=Infinity,y=Infinity,right=-Infinity,bottom=-Infinity;
      for(const b of boxes)if(b){x=Math.min(x,b.x);y=Math.min(y,b.y);right=Math.max(right,b.x+b.w);bottom=Math.max(bottom,b.y+b.h);}
      this.box={x,y,w:Math.max(.01,right-x),h:Math.max(.01,bottom-y)};this.references=0;
      for(let i=0;i<boxes.length;i++){
        const b=boxes[i];if(!b){this.overflow.push(i);continue;}
        const range=this.range(b),count=(range.x1-range.x0+1)*(range.y1-range.y0+1);
        if(count>64||this.references+count>1000000){this.overflow.push(i);continue;}
        for(let cy=range.y0;cy<=range.y1;cy++)for(let cx=range.x0;cx<=range.x1;cx++){const key=cy*64+cx;if(!this.cells.has(key))this.cells.set(key,[]);this.cells.get(key).push(i);}
        this.references+=count;
      }
    }
    range(b){const grid=(value,origin,size)=>Math.max(0,Math.min(63,Math.floor((value-origin)/size*64)));return {x0:grid(b.x,this.box.x,this.box.w),x1:grid(b.x+b.w,this.box.x,this.box.w),y0:grid(b.y,this.box.y,this.box.h),y1:grid(b.y+b.h,this.box.y,this.box.h)};}
    query(b){
      const range=this.range(b);
      if(range.x0===0&&range.y0===0&&range.x1===63&&range.y1===63)return this.primitives;
      // Reuse workspace for partial camera queries instead of allocating and
      // sorting a Set of boxed entries for each gesture frame.
      const marks = this.queryMarks ??= new Uint8Array(this.length);
      const indices = this.queryIndices ??= new Uint32Array(this.length);
      let count = 0;
      const append = index => {
        if (marks[index]) return;
        marks[index] = 1;
        indices[count++] = index;
      };
      for (const index of this.overflow) append(index);
      if(b.x+b.w>=this.box.x&&b.x<=this.box.x+this.box.w&&b.y+b.h>=this.box.y&&b.y<=this.box.y+this.box.h){
        for(let y=range.y0;y<=range.y1;y++)for(let x=range.x0;x<=range.x1;x++){
          const cell = this.cells.get(y*64+x);
          if (cell) for (const index of cell) append(index);
        }
      }
      const ordered = indices.subarray(0,count).sort();
      const result = new Array(count);
      for (let offset=0; offset<count; offset++) {
        const index = ordered[offset];
        marks[index] = 0;
        result[offset] = this.primitives[index];
      }
      return result;
    }
  }

  // SPIKE-style copper labels: screen-sized, bounded, and derived only from saved nets.
  function netLabels(entries,camera,limit=300){
    const output=[],boxes=[],zoom=camera.zoom,view=camera.bbox;
    if(!(zoom>0)||!Number.isFinite(zoom))return output;
    const add=(p,name,x,y,angle,size)=>{
      if(output.length>=limit||!Number.isFinite(x+y+angle+size))return;
      const at=camera.world_to_screen({x,y}),r=angle*Math.PI/180,w=size*(Math.max(...name.split('\n').map(line=>line.length))*.65+1),h=size*1.4*name.split('\n').length;
      const bw=Math.abs(Math.cos(r))*w+Math.abs(Math.sin(r))*h,bh=Math.abs(Math.sin(r))*w+Math.abs(Math.cos(r))*h;
      const box={x:at.x-bw/2-3,y:at.y-bh/2-3,w:bw+6,h:bh+6};
      if(box.x<0||box.y<0||box.x+box.w>camera.width||box.y+box.h>camera.height||boxes.some(b=>box.x<b.x+b.w&&box.x+box.w>b.x&&box.y<b.y+b.h&&box.y+box.h>b.y))return;
      boxes.push(box);output.push({text:name,x:at.x,y:at.y,angle,size,primitive:p});
    };
    const labeledPads=new Set();
    for(const [p,rawName] of entries){
      if(output.length>=limit)break;
      let name=rawName;
      if(p.kind==='pad'&&!p.hole&&p.layer?.endsWith('.Cu')){
        // Most saved pads already carry exact dimensions. Reject unreadable
        // labels before allocating identity keys or walking polygon vertices.
        if(p.padWidth&&p.padHeight&&Math.min(p.padWidth,p.padHeight)*zoom<13)continue;
        const number=String(p.pinNumber??p.name??'');
        if(number.length>160||String(p.pinName||'').length>160||/[\x00-\x1f\x7f]/.test(number+String(p.pinName||'')))continue;
        const key=JSON.stringify([p.ownerUuid,p.uuid,number,p.padCenter]);
        if(labeledPads.has(key))continue;
        const b=p.padCenter&&p.padWidth&&p.padHeight?null:spatialBounds(p),center=p.padCenter||(b?{x:b.x+b.w/2,y:b.y+b.h/2}:null);
        if(!center)continue;
        const width=(p.padWidth||b?.w||0)*zoom,height=(p.padHeight||b?.h||0)*zoom;
        if(Math.min(width,height)<13)continue;
        const size=Math.min(18,Math.max(9,Math.min(width,height)*.25));
        const net=typeof name==='string'&&name.length<=160?name:'';
        const title=[number,p.pinName].filter(Boolean).join(' ');
        const lines=[title,net].filter(Boolean);
        if(!lines.length)continue;
        const label=lines.join('\n'),maxLength=Math.max(...lines.map(line=>line.length));
        if(maxLength*size*.65>Math.max(width,height)*1.7)continue;
        const start=output.length;add(p,label,center.x,center.y,0,size);
        if(output.length>start)labeledPads.add(key);
        continue;
      }
      if(typeof name!=='string'||!name.trim()||name.length>160||p.hole||!p.layer?.endsWith('.Cu'))continue;
      if(p.kind==='track'&&p.type==='line'&&p.points?.length===2){
        const [a,b]=p.points,dx=b.x-a.x,dy=b.y-a.y,size=Math.min(11,p.width*zoom*.8);
        if(!(size>=7))continue;
        let lo=0,hi=1;
        for(const [d,q] of [[-dx,a.x-view.x],[dx,view.x+view.w-a.x],[-dy,a.y-view.y],[dy,view.y+view.h-a.y]]){
          if(d===0){if(q<0)hi=-1;}else if(d<0)lo=Math.max(lo,q/d);else hi=Math.min(hi,q/d);
        }
        if(hi<=lo||Math.hypot(dx,dy)*(hi-lo)*zoom<size*(name.length*.65+1))continue;
        let angle=Math.atan2(dy,dx*(camera.flipped?-1:1))*180/Math.PI;
        if(angle>90)angle-=180;if(angle< -90)angle+=180;
        const t=(lo+hi)/2;add(p,name,a.x+dx*t,a.y+dy*t,angle,size);
      }else if(p.kind==='zone'&&p.fill&&p.points?.length>=3){
        // Even/odd scanlines also avoid cut-outs represented by bridged polygon rings.
        const poly=p.points,size=11,needed=size*(name.length*.65+1)/zoom;
        let low=Infinity,high=-Infinity;for(const q of poly){low=Math.min(low,q.y);high=Math.max(high,q.y);}
        low=Math.max(low,view.y);high=Math.min(high,view.y+view.h);
        const intervals=y=>{const hits=[];for(let i=0,j=poly.length-1;i<poly.length;j=i++){const a=poly[j],b=poly[i];if((a.y>y)!==(b.y>y))hits.push(a.x+(y-a.y)*(b.x-a.x)/(b.y-a.y));}hits.sort((a,b)=>a-b);const spans=[];for(let i=0;i+1<hits.length;i+=2)spans.push([Math.max(hits[i],view.x),Math.min(hits[i+1],view.x+view.w)]);return spans;};
        let best=null;
        for(const t of [.5,.25,.75]){const y=low+(high-low)*t;
          for(const [a,b] of intervals(y)){const x=(a+b)/2;
            if(b-a<needed||best&&b-a<=best.width)continue;
            if([-.8,0,.8].every(d=>intervals(y+d*size/zoom).some(([l,r])=>x-needed/2>=l&&x+needed/2<=r)))best={x,y,width:b-a};
          }
        }
        if(best)add(p,name,best.x,best.y,0,size);
      }
    }
    return output;
  }

  class Camera {
    constructor(width=800,height=600){this.width=width;this.height=height;this.zoom=4;this.center={x:0,y:0,set(x,y){this.x=x;this.y=y;}};this.flipped=false;this.quarterTurns=0;}
    get quarterTurns(){return this._quarterTurns;}
    set quarterTurns(value){this._quarterTurns=Number.isFinite(value)?Math.round(((value%4+4)%4)*90*1e9)/1e9/90%4:0;const angle=this._quarterTurns*Math.PI/2;this.rotationCos=Math.abs(Math.cos(angle))<1e-12?0:Math.cos(angle);this.rotationSin=Math.abs(Math.sin(angle))<1e-12?0:Math.sin(angle);}
    get bbox(){const co=Math.abs(this.rotationCos),si=Math.abs(this.rotationSin),w=(co*this.width+si*this.height)/this.zoom,h=(si*this.width+co*this.height)/this.zoom;return {x:this.center.x-w/2,y:this.center.y-h/2,w,h};}
    set bbox(b){if(!b||![b.x,b.y,b.w,b.h].every(Number.isFinite)||b.w<=0||b.h<=0)return;this.center.set(b.x+b.w/2,b.y+b.h/2);const co=Math.abs(this.rotationCos),si=Math.abs(this.rotationSin);this.zoom=Math.max(.001,Math.min(10000,Math.min(this.width/(co*b.w+si*b.h),this.height/(si*b.w+co*b.h))));}
    // Mirror saved world X, then rotate clockwise in screen coordinates.
    worldDeltaToScreen(p){const x=p.x*this.zoom*(this.flipped?-1:1),y=p.y*this.zoom;return{x:x*this.rotationCos-y*this.rotationSin,y:x*this.rotationSin+y*this.rotationCos};}
    screenDeltaToWorld(p){const x=p.x/this.zoom,y=p.y/this.zoom;return{x:(x*this.rotationCos+y*this.rotationSin)*(this.flipped?-1:1),y:-x*this.rotationSin+y*this.rotationCos};}
    screen_to_world(p){const d=this.screenDeltaToWorld({x:p.x-this.width/2,y:p.y-this.height/2});return{x:this.center.x+d.x,y:this.center.y+d.y};}
    world_to_screen(p){const d=this.worldDeltaToScreen({x:p.x-this.center.x,y:p.y-this.center.y});return{x:this.width/2+d.x,y:this.height/2+d.y};}

  }

  const objectTypes=Object.freeze([
    ['arcs','Arcs'],['pads','Pads'],['vias','Vias'],['tracks','Tracks'],['texts','Texts'],
    ['fills','Fills'],['polygons','Polygons'],['regions','Regions'],['graphics','Graphics'],
    ['bodies','3D bodies'],['dimensions','Dimensions'],['rooms','Rooms']
  ].map(([id,label])=>Object.freeze({id,label})));
  function objectCategory(p){
    if(!p||p.frame||p.kind==='footprint-hit')return null;
    if(p.objectCategory)return p.objectCategory;
    if(p.kind==='drill')return p.ownerUuid?'pads':'vias';
    if(p.kind==='pad')return 'pads';if(p.kind==='via')return 'vias';
    if(p.type==='text')return 'texts';if(p.type==='arc')return 'arcs';
    if(p.kind==='track'||p.kind==='segment')return 'tracks';
    if(p.kind==='zone')return 'fills';if(p.kind==='zone-outline')return 'polygons';
    if(['gr_poly','fp_poly'].includes(p.kind))return p.fill?'regions':'polygons';
    if(p.fill)return 'fills';return 'graphics';
  }
  function objectVisible(p,state={}){
    if(!p||p.frame)return true;
    const category=objectCategory(p),hidden=state.hiddenTypes;
    if(category&&(hidden?.has?.(category)||Array.isArray(hidden)&&hidden.includes(category)))return false;
    const layer=p.layer||'';return !(state.side==='top'&&layer.startsWith('B.'))&&!(state.side==='bottom'&&layer.startsWith('F.'));
  }
  class Viewport extends EventTarget {

    constructor(host,options={}){
      super();this.loadFontDecoder=options.loadFontDecoder;this.onFontWarning=options.onFontWarning;this.host=host;this.geometryBounds=new WeakMap();this.frameRequest=null;this.canvas=document.createElement('canvas');this.canvas.className='owned-canvas';this.canvas.tabIndex=0;this.canvas.setAttribute('aria-label','Interactive KiCad drawing; click to select, right-drag to pan, wheel to zoom');host.append(this.canvas);

      this.ctx=this.canvas.getContext('2d');if(!this.ctx)throw new Error('Canvas2D rendering is unavailable.');this.viewport={camera:new Camera()};this.primitives=[];this.visibleLayers=new Set();this.dark=Boolean(options.dark);this.pcb=Boolean(options.pcb);this.grid=false;this.selected=null;this.drag=null;this.netHighlight=null;this.netPrimitives=new Set();this.isolateNet=false;this.paperVisible=!this.pcb;this.activeLayer='F.Cu';this.focusedLayer=null;this.layerFocusMode='dim';this.layerSnapshot=null;this.layerPresetName=null;
      this.acceleration=options.acceleration==='off'?'off':'auto';this.motion=null;this.motionSnapshot=null;this.motionActive=false;this.motionFailed=false;this.motionTimer=null;this.wheelMotion=false;this.touchMotion=false;

      this.layers={query_point:at=>this.query(at).map(p=>({bbox:{context:{uuid:p.uuid,reference:p.reference,kind:p.kind,name:p.name,pinNumber:p.pinNumber,pinName:p.pinName,ownerUuid:p.ownerUuid,connectPoint:p.connectPoint,parent:p.ownerUuid?{uuid:p.ownerUuid}:null},primitive:p}}))};

      this.handlers=[];const listen=(name,fn,opts)=>{this.canvas.addEventListener(name,fn,opts);this.handlers.push(()=>this.canvas.removeEventListener(name,fn,opts));};

      listen('pointerdown',e=>{if(e.button===1||e.button===2){e.preventDefault();this.drag={x:e.clientX,y:e.clientY,id:e.pointerId,moved:false};this.canvas.setPointerCapture(e.pointerId);}});

      listen('pointerleave',()=>this.reportPartHover(null));
      listen('pointermove',event=>{
        if(!this.partHoverEnabled||this.drag||this.wheelMotion||this.touchMotion||event.pointerType==='touch'||event.buttons)return;
        const rect=this.canvas.getBoundingClientRect();
        const position=this.viewport.camera.screen_to_world({x:event.clientX-rect.left,y:event.clientY-rect.top});
        const hit=this.query(position,5,primitive=>Boolean(primitive.ownerUuid||primitive.reference))[0];
        this.reportPartHover(hit||null);
      });
      listen('pointermove',e=>{if(!this.drag||e.pointerId!==this.drag.id)return;const dx=e.clientX-this.drag.x,dy=e.clientY-this.drag.y;if(!dx&&!dy)return;this.drag.moved=true;const c=this.viewport.camera;c.center.x-=dx/c.zoom*(c.flipped?-1:1);c.center.y-=dy/c.zoom;this.drag.x=e.clientX;this.drag.y=e.clientY;this.requestDraw();});

      const endDrag=e=>{if(!this.drag||this.drag.id!==e.pointerId)return;const moved=this.drag.moved;this.drag=null;if(moved)this.requestDraw();};
      listen('pointerup',e=>{endDrag(e);if(this.canvas.hasPointerCapture(e.pointerId))this.canvas.releasePointerCapture(e.pointerId);});listen('pointercancel',endDrag);listen('lostpointercapture',endDrag);

      listen('click',e=>{if(e.pointerType==='touch'&&this.touchNetPick?.(e))return;const r=this.canvas.getBoundingClientRect(),at=this.viewport.camera.screen_to_world({x:e.clientX-r.left,y:e.clientY-r.top});const hit=this.query(at)[0];this.select(hit?{primitive:hit,context:{uuid:hit.uuid,reference:hit.reference,kind:hit.kind,name:hit.name,pinNumber:hit.pinNumber,pinName:hit.pinName,ownerUuid:hit.ownerUuid,connectPoint:hit.connectPoint,parent:hit.ownerUuid?{uuid:hit.ownerUuid}:null}}:null,{multi:Boolean(e.ctrlKey||e.metaKey||e.shiftKey)});});

      listen('wheel',e=>{e.preventDefault();const r=this.canvas.getBoundingClientRect(),p={x:e.clientX-r.left,y:e.clientY-r.top},c=this.viewport.camera,before=c.screen_to_world(p);c.zoom=Math.max(.001,Math.min(10000,c.zoom* Math.exp(-Math.max(-100,Math.min(100,e.deltaY))*.004)));const after=c.screen_to_world(p);c.center.x+=before.x-after.x;c.center.y+=before.y-after.y;if(typeof setTimeout==='function'){this.wheelMotion=true;if(this.motionTimer!==null)clearTimeout(this.motionTimer);this.motionTimer=setTimeout(()=>{this.motionTimer=null;this.wheelMotion=false;this.requestDraw();},this.primitives.length>=DENSE_PREVIEW_MIN_PRIMITIVES?280:120);}this.requestDraw();},{passive:false});

      listen('contextmenu',e=>e.preventDefault());

      this.observer=new ResizeObserver(()=>{const prior=this.viewport.camera.zoom;this.resize();this.viewport.camera.zoom=prior;this.draw();});this.observer.observe(host);this.resize();

    }

    resize(){
      if(this.disposed)return;
      const r=this.host.getBoundingClientRect(),c=this.viewport.camera;
      this.hasSize=Number.isFinite(r.width)&&Number.isFinite(r.height)&&r.width>0&&r.height>0;
      if(!this.hasSize){this.hideMotion();return;}
      c.width=r.width;c.height=r.height;
      const view=canvasViewport(c.width,c.height,globalThis.devicePixelRatio);
      this.pixelRatio=view.ratio;
      if(this.canvas.width!==view.width)this.canvas.width=view.width;
      if(this.canvas.height!==view.height)this.canvas.height=view.height;
      this.canvas.style.width=c.width+'px';this.canvas.style.height=c.height+'px';
    }

    setDocument(design,path,inheritedFonts){
      this.resize();this.design=design;this.document={filename:path};this.primitives=design.primitives;this.visibleLayers=new Set((design.layers||[]).filter(layer=>!this.pcb||['F.Cu','B.Cu','F.SilkS','B.SilkS','Edge.Cuts'].includes(layer)));this.selected=null;this.focusedLayer=null;this.layerSnapshot=null;this.layerPresetName=null;this.setNetHighlight(null);
      const ownFonts=design.embeddedFonts;
      const assets=ownFonts||inheritedFonts,faces=new Set();
      if(assets?.length&&root.KiCadEmbeddedFonts){
        for(const p of design.primitives)if(typeof p.fontFace==='string')faces.add(p.fontFace.trim().toLowerCase());
      }
      if(this.embeddedFontSet&&this.fontSource===assets&&this.inheritedFonts===inheritedFonts&&faces.size&&
         [...faces].every(face=>this.fontFaces.has(face))){this.zoom_to_board();this.draw();return;}
      this.embeddedFontSet?.dispose();
      this.embeddedFontSet=null;
      this.fontSource=assets;
      this.inheritedFonts=inheritedFonts;
      this.fontFaces=faces;
      this.zoom_to_board();
      this.draw();
      if(!faces.size)return;
      const localNames=new Set(ownFonts?.map(asset=>asset.name));
      const candidates=ownFonts&&inheritedFonts&&ownFonts!==inheritedFonts
        ? [...ownFonts,...inheritedFonts.filter(asset=>!localNames.has(asset.name))]
        : assets;
      this.embeddedFontSet=root.KiCadEmbeddedFonts.mount(candidates,faces,()=>{
        this.geometryBounds=new WeakMap();
        this.textBoundsCache=new WeakMap();
        this.spatialIndex=null;
        this.releaseMotion();
        this.draw();
      },(name,error)=>{
        const message='Embedded font '+name+': '+error;
        if(design.warnings&&!design.warnings.includes(message)){design.warnings.push(message);this.onFontWarning?.();}
      },this.canvas.ownerDocument?.defaultView||root,this.loadFontDecoder);
    }

    get primitives(){return this._primitives;}
    set primitives(value){this.hoverParts=null;if(this.hoverCanvas)this.hoverCanvas.hidden=true;this.releaseImages();this._primitives=value;this.spatialIndex=null;this.geometryBounds=new WeakMap();this.textBoundsCache=new WeakMap();this.ownedGeometry=new Map();for(const p of value||[])if(p.ownerUuid&&p.kind!=='footprint-hit'){const list=this.ownedGeometry.get(p.ownerUuid)||[];list.push(p);this.ownedGeometry.set(p.ownerUuid,list);}}
    releaseImages(){for(const entry of this.images?.values()||[]){if(!entry.image)continue;entry.image.onload=entry.image.onerror=null;entry.image.src='';}this.images=new Map();this.imagePixels=0;}
    imageResource(p){
      if(this.images.has(p))return this.images.get(p);
      const invalid={ready:false};this.images.set(p,invalid);
      const width=p.pixelWidth,height=p.pixelHeight,pixels=width*height;
      if(typeof root.Image!=='function'||typeof p.dataUrl!=='string'||p.dataUrl.length>5600000||!/^data:image\/(?:png;base64,iVBORw0KGgo|bmp;base64,Qk[0123])[A-Za-z0-9+/]*={0,2}$/.test(p.dataUrl)||!Number.isSafeInteger(width)||!Number.isSafeInteger(height)||width<1||height<1||pixels>16000000||this.imagePixels+pixels>16000000||this.images.size>16)return invalid;
      this.imagePixels+=pixels;const image=new root.Image(),entry={image,ready:false};this.images.set(p,entry);image.decoding='async';
      image.onload=()=>{if(this.disposed||this.images.get(p)!==entry)return;entry.ready=image.naturalWidth===width&&image.naturalHeight===height;if(entry.ready)this.requestDraw();};
      image.onerror=()=>{entry.ready=false;};image.src=p.dataUrl;return entry;
    }
    candidates(box){
      if(this.primitives.length<512)return this.primitives;
      if(!this.spatialIndex||this.spatialIndex.length!==this.primitives.length)this.spatialIndex=new SpatialIndex(this.primitives);
      return this.spatialIndex.query(box);
    }
    geometryVisible(p){if(p.hidden||!objectVisible(p,this.objectVisibility)||(p.kind==='zone'&&this.showFilledCopper===false)||(p.kind==='zone-outline'&&this.showZoneOutlines===false))return false;if(p.kind==='footprint-hit'){const children=this.ownedGeometry?.get(p.ownerUuid||p.uuid);if(children?.length)return children.some(child=>this.geometryVisible(child)&&(!child.layer||this.visibleLayers.has(child.layer)));}return true;}
    setObjectVisibility({hiddenTypes=[],side='all'}={}){this.objectVisibility={hiddenTypes:new Set(hiddenTypes.filter(id=>objectTypes.some(type=>type.id===id))),side:['all','top','bottom'].includes(side)?side:'all'};if(this.selected&&!this.geometryVisible(this.selected))this.selected=null;this.finishMotion();}
    setZoneVisibility({filled=this.showFilledCopper!==false,outlines=this.showZoneOutlines!==false}={}){this.showFilledCopper=Boolean(filled);this.showZoneOutlines=Boolean(outlines);this.finishMotion();}
    setNetNames(nets){this.netNames=new Map();for(const net of nets||[])for(const p of net.primitives||[])this.netNames.set(p,net.name);}
    drawNetNames(){
      if(!this.pcb||!this.showNetNames||!this.netNames||this.objectVisibility?.hiddenTypes.has('texts'))return;
      const viewport=this;
      // Consume in saved draw order, stopping visibility/name work at the cap.
      function* entries(){for(const p of viewport.candidates(viewport.viewport.camera.bbox))if((viewport.netNames.has(p)||p.kind==='pad')&&viewport.geometryVisible(p)&&viewport.visibleLayers.has(p.layer)&&(!viewport.focusedLayer||p.layer===viewport.focusedLayer)&&(!(viewport.netHighlight&&viewport.highlightMode!=='bright')||viewport.netPrimitives.has(p)))yield [p,viewport.netNames.get(p)||p.netName||''];}
      const labels=netLabels(entries(),this.viewport.camera),ctx=this.ctx;
      ctx.save();ctx.font='11px monospace';ctx.textAlign='center';ctx.textBaseline='middle';ctx.lineJoin='round';ctx.lineWidth=3;
      ctx.fillStyle=this.dark?'#f3f8fa':'#17212b';ctx.strokeStyle=this.dark?'#101820':'#ffffff';
      for(const label of labels){ctx.save();ctx.translate(label.x,label.y);ctx.rotate(label.angle*Math.PI/180);ctx.font=label.size+'px monospace';const lines=label.text.split('\n');lines.forEach((line,i)=>{const y=(i-(lines.length-1)/2)*label.size*1.4;ctx.strokeText(line,0,y);ctx.fillText(line,0,y);});ctx.restore();}ctx.restore();
    }
    setSelectionMode(mode){this.selectionMode=['all','part','net','pin'].includes(mode)?mode:'all';}
    pickDistance(p,at){
      if(!p||p.frame||p.hole)return Infinity;
      const c=this.viewport.camera;if(p.type==='text'){
        this.textBounds(p);const rects=this.textBoundsCache.get(p)?.rects;
        if(rects){const origin=c.world_to_screen(p),point=c.world_to_screen(at),angle=((p.angle||0)*(c.flipped?-1:1)+c.quarterTurns*90)*Math.PI/180,dx=point.x-origin.x,dy=point.y-origin.y,x=dx*Math.cos(angle)+dy*Math.sin(angle),y=-dx*Math.sin(angle)+dy*Math.cos(angle);let distance=Infinity;
          for(const rect of rects)distance=Math.min(distance,Math.hypot(Math.max(rect.left-x,0,x-rect.right),Math.max(rect.top-y,0,y-rect.bottom)));return distance;
        }
      }
      return primitiveDistance(p,at,.25/c.zoom)*c.zoom;
    }
    query(at,pixels=5,accept){
      const camera = this.viewport.camera;
      const tolerance = pixels / camera.zoom;
      const preferred = this.focusedLayer || (camera.flipped ? 'B.Cu' : 'F.Cu');
      const ranked = this.primitives.slice(0,0);
      const compare = (a,b) => a.score-b.score;
      let cutoff = Infinity;
      for (const p of this.candidates({x:at.x-tolerance,y:at.y-tolerance,w:2*tolerance,h:2*tolerance})) {
        if (accept && !accept(p) || p.pick === false || p.hole || p.frame || !(p.uuid || p.reference) || !this.geometryVisible(p)) continue;
        if (this.isolateNet && this.netHighlight && !this.netPrimitives.has(p)) continue;
        if (!accept && this.selectionMode === 'pin' && !['pad','pin','pin-marker','pin-name','pin-number'].includes(p.kind)) continue;
        if (p.layer && !this.visibleLayers.has(p.layer)) continue;
        if (this.focusedLayer && this.layerFocusMode === 'isolate' && p.layer !== this.focusedLayer) continue;
        const useDistance = p.type === 'text' || p.zoneCoordinates;
        if (!useDistance && !hitPrimitive(p,at,tolerance)) continue;
        const distance = this.pickDistance(p,at);
        if (useDistance && distance > pixels) continue;
        const kind = p.electricalKind || p.kind;
        const rank = ['pad','pin','via'].includes(kind) ? 0 :
          ['wire','track','arc','junction','label','global_label','hierarchical_label','sheet-pin','pin-number','pin-name'].includes(kind) ? 1 :
          ['footprint','symbol','footprint-hit','sheet'].includes(kind) ? 2 : p.type === 'image' ? 12 : 3;
        const bias = this.pcb && p.layer && p.layer !== preferred ? .25 : 0;
        const zone = p.kind === 'zone' || p.kind === 'zone-outline';
        const score = distance + rank * .35 + bias + (zone ? pixels + 1 : 0);
        if (score >= cutoff) continue;
        ranked.push({p,score});
        if (ranked.length === 80) {
          ranked.sort(compare);
          ranked.length = 40;
          cutoff = ranked[39].score;
        }
      }
      return ranked.sort(compare).slice(0,40).map(row=>row.p);
    }

    reportPartHover(primitive){
      if(!this.partHoverEnabled||this.hoveredPrimitive===primitive)return;
      this.hoveredPrimitive=primitive;
      this.dispatchEvent(new CustomEvent('kicad:parthover',{detail:primitive}));
    }

    setPartHover(primitives){
      this.hoverParts=primitives;
      this.paintPartHover();
    }

    paintPartHover(){
      if(!this.hoverParts?.length||!this.hasSize||this.canvas.hidden){
        if(this.hoverCanvas)this.hoverCanvas.hidden=true;
        return;
      }
      if(!this.hoverCanvas){
        this.hoverCanvas=this.canvas.ownerDocument.createElement('canvas');
        this.hoverCanvas.setAttribute('aria-hidden','true');
        this.hoverCanvas.style.cssText='position:absolute;inset:0;width:100%;height:100%;pointer-events:none';
        this.canvas.after(this.hoverCanvas);
      }
      const overlay=this.hoverCanvas, context=overlay.getContext('2d');
      if(!context)return;
      if(overlay.width!==this.canvas.width||overlay.height!==this.canvas.height){
        overlay.width=this.canvas.width;overlay.height=this.canvas.height;
      }
      overlay.hidden=false;
      context.setTransform(1,0,0,1,0,0);
      context.clearRect(0,0,overlay.width,overlay.height);
      const dpr=this.pixelRatio||1;
      context.setTransform(dpr,0,0,dpr,0,0);
      const previous=this.ctx, view=this.viewport.camera.bbox;
      this.ctx=context;
      try {
        let painted=0;
        for(const primitive of this.hoverParts){
          if(primitive.kind==='footprint-hit'||primitive.hole||!this.geometryVisible(primitive)||
             primitive.layer&&!this.visibleLayers.has(primitive.layer)||!this.inView(primitive,view))continue;
          this.paint(primitive,{color:this.dark?'#ffbf52':'#a45600',opacity:primitive.fill ? .65 : 1});
          if(++painted===1800)break;
        }
      } finally { this.ctx=previous; }
    }

    select(hit,options={}){this.selected=hit?.primitive||null;this.draw();this.dispatchEvent(new CustomEvent('kicad:select',{detail:{item:hit?.context||null,multi:Boolean(options.multi)}}));}

    setNetHighlight(net,options={}){this.setNetHighlights(net?[net]:[],options);}
    setNetHighlights(nets,options={}){const list=Array.isArray(nets)?nets.slice(0,200):[];this.netHighlight=list.length?list[0]:null;this.highlightCount=list.length;
      // Rebuild from live arrays on every call; avoid a second, potentially
      // board-sized flatMap array without caching mutable selection inputs.
      const primitives=new Set();list.forEach(net=>{const members=net.primitives||[];if(Array.isArray(members))members.forEach(p=>primitives.add(p));else primitives.add(members);});this.netPrimitives=primitives;
      this.isolateNet=Boolean(options.isolate);this.highlightMode=['bright','dim','isolate'].includes(options.mode)?options.mode:(this.isolateNet?'isolate':'dim');this.isolateNet=this.highlightMode==='isolate';this.selectionColor=options.selectionColor==='source'?'source':'accent';}

    focusLayer(layer,mode='dim'){

      if(!(this.design?.layers||[]).includes(layer)||!['dim','isolate'].includes(mode))return false;

      if(!this.layerSnapshot)this.layerSnapshot=new Set(this.visibleLayers);

      this.focusedLayer=layer;this.layerPresetName=null;this.layerFocusMode=mode;this.activeLayer=layer;this.visibleLayers.add(layer);return true;

    }

    restoreLayers(){if(this.layerSnapshot)this.visibleLayers=new Set(this.layerSnapshot);this.layerSnapshot=null;this.focusedLayer=null;this.layerPresetName=null;}

    layerPreset(preset){

      const all=this.design?.layers||[];if(!['all','none','front','back','copper','overview','outline'].includes(preset))return false;

      if(!this.layerSnapshot)this.layerSnapshot=new Set(this.visibleLayers);

      this.focusedLayer=null;this.layerPresetName=preset;this.visibleLayers=new Set(all.filter(layer=>preset==='all'||preset!=='none'&&(layer==='Edge.Cuts'||preset==='copper'&&layer.endsWith('.Cu')||preset==='front'&&layer.startsWith('F.')||preset==='back'&&layer.startsWith('B.')||preset==='overview'&&['F.Cu','B.Cu','F.SilkS','B.SilkS'].includes(layer))));return true;

    }

    fitLayer(){const primitives=this.primitives.filter(p=>this.geometryVisible(p)&&!p.frame&&p.layer===this.focusedLayer);const b=bounds(primitives);if(!b)return false;const margin=Math.max(1,Math.max(b.w,b.h)*.08);this.viewport.camera.bbox={x:b.x-margin,y:b.y-margin,w:b.w+2*margin,h:b.h+2*margin};this.paperVisible=false;return true;}

    zoom_to_board(){this.paperVisible=!this.pcb;const edges=this.pcb?this.primitives.filter(p=>p.layer==='Edge.Cuts'):[];const b=bounds(edges.length?edges:this.primitives);if(b){const margin=Math.max(3,Math.max(b.w,b.h)*.06);this.viewport.camera.bbox={x:b.x-margin,y:b.y-margin,w:b.w+2*margin,h:b.h+2*margin};}else this.zoom_to_page();}

    zoom_to_page(){this.paperVisible=true;const p=this.design?.page||{width:297,height:210};this.viewport.camera.bbox={x:-5,y:-5,w:p.width+10,h:p.height+10};}

    background(){return this.dark?'#151820':'#e7ebf1';}

    paperColor(){return this.dark?'#1c2029':'#fdfdff';}

    cameraState(){const c=this.viewport.camera;return {x:c.center.x,y:c.center.y,zoom:c.zoom,width:c.width,height:c.height,flipped:c.flipped};}
    canUseMotionTexture(){return this.hasSize&&!this.disposed&&this.acceleration!=='off'&&!this.motionFailed&&this.primitives.length>=MOTION_MIN_PRIMITIVES&&this.primitives.length<DENSE_PREVIEW_MIN_PRIMITIVES&&!this.renderPrimitives&&Boolean(root.KiCadMotionWebGL);}
    prepareMotion(){
      if(!this.canUseMotionTexture())return false;
      try{
        if(!this.motion){this.motion=new root.KiCadMotionWebGL.Motion(this.host);this.motion.onLost=()=>{this.motionFailed=true;this.hideMotion();this.releaseMotion();this.requestDraw();};}
        const c=this.viewport.camera,w=c.width,h=c.height,snapshotWidth=w*1.5,snapshotHeight=h*1.5;
        const max=this.motion.gl.getParameter(this.motion.gl.MAX_TEXTURE_SIZE),maxPixels=Math.min(MOTION_SNAPSHOT_MAX_PIXELS,root.KiCadMotionWebGL.MAX_PIXELS);
        const ratio=Math.min(this.pixelRatio||1,Math.sqrt(maxPixels/snapshotWidth/snapshotHeight),max/snapshotWidth,max/snapshotHeight);
        const width=Math.max(1,Math.floor(snapshotWidth*ratio)),height=Math.max(1,Math.floor(snapshotHeight*ratio));
        const snapshot=this.motionSnapshot||=document.createElement('canvas');
        // Shrink the old width first to stay within the pixel cap during reallocation.
        if(snapshot.width>width)snapshot.width=width;
        if(snapshot.height!==height)snapshot.height=height;
        if(snapshot.width!==width)snapshot.width=width;
        const context=snapshot.getContext('2d');if(!context)throw Error('Canvas2D motion snapshot is unavailable.');
        const originalCanvas=this.canvas,originalContext=this.ctx;let saved;
        try{this.canvas=snapshot;this.ctx=context;c.width=snapshotWidth;c.height=snapshotHeight;saved=this.cameraState();this.drawGeometry(true,ratio);}
        finally{this.canvas=originalCanvas;this.ctx=originalContext;c.width=w;c.height=h;}
        this.motion.upload(snapshot,saved);return true;
      }catch{this.motionFailed=true;this.releaseMotion();return false;}
    }
    motionFrame(){
      if(!this.canUseMotionTexture())return false;
      const state=this.cameraState(),dpr=this.pixelRatio||1;
      if(this.motion?.saved&&!this.motion.show(state,dpr))this.motion.saved=null;
      if(!this.motion?.saved&&(!this.prepareMotion()||!this.motion.show(state,dpr)))return false;
      this.canvas.style.opacity='0';this.motionActive=true;return true;
    }
    bitmapMotionFrame(){
      if(!this.hasSize||this.acceleration==='off'||this.renderPrimitives||this.primitives.length<500)return false;
      const current=this.cameraState();
      let saved=this.bitmapMotionState||this.paintedCamera;
      if(!saved||saved.width!==current.width||saved.height!==current.height||saved.flipped!==current.flipped)return false;
      const offset=source=>{
        const scale=current.zoom/source.zoom;
        return {scale,x:(source.x-current.x)*current.zoom*(current.flipped?-1:1)+(1-scale)*current.width/2,
          y:(source.y-current.y)*current.zoom+(1-scale)*current.height/2};
      };
      let transform=offset(saved);
      const outside=transform.scale<.76||transform.scale>1.55||
        Math.abs(transform.x-(1-transform.scale)*current.width/2)>current.width*.24||
        Math.abs(transform.y-(1-transform.scale)*current.height/2)>current.height*.24;
      if(outside){
        this.drawGeometry(true);
        saved=current;
        transform=offset(saved);
      }
      if(!this.bitmapMotion){
        this.bitmapMotion=document.createElement('canvas');
        this.bitmapMotion.setAttribute('aria-hidden','true');
        this.bitmapMotion.style.cssText='position:absolute;inset:0;width:100%;height:100%;pointer-events:none;transform-origin:0 0';
        this.bitmapMotion.hidden=true;
        this.host.append(this.bitmapMotion);
      }
      const bitmap=this.bitmapMotion;
      if(outside||!this.bitmapMotionState){
        const ratio=Math.min(this.pixelRatio||1,Math.sqrt(1600000/current.width/current.height));
        bitmap.width=Math.max(1,Math.floor(current.width*ratio));
        bitmap.height=Math.max(1,Math.floor(current.height*ratio));
        const context=bitmap.getContext('2d');
        if(!context)return false;
        context.drawImage(this.canvas,0,0,bitmap.width,bitmap.height);
        this.bitmapMotionState=saved;
      }
      bitmap.style.transform=`translate3d(${transform.x}px,${transform.y}px,0) scale(${transform.scale})`;
      bitmap.hidden=false;
      this.canvas.style.opacity='0';this.motionActive=true;
      return true;
    }
    hideMotion(){
      this.motion?.hide();
      if(this.bitmapMotion&&!this.bitmapMotion.hidden){
        this.bitmapMotion.hidden=true;
        this.bitmapMotion.width=this.bitmapMotion.height=1;
      }
      this.bitmapMotionState=null;
      this.canvas.style.opacity='';this.motionActive=false;
    }
    releaseMotion(){
      this.motion?.dispose();this.motion=null;
      if(this.motionSnapshot){this.motionSnapshot.width=this.motionSnapshot.height=1;this.motionSnapshot=null;}
    }
    finishMotion(){if(this.motionTimer!==null){clearTimeout(this.motionTimer);this.motionTimer=null;}if(this.frameRequest!==null&&typeof cancelAnimationFrame==='function')cancelAnimationFrame(this.frameRequest);this.frameRequest=null;this.wheelMotion=false;if(!this.disposed)this.draw();}
    requestDraw(){
      if(this.disposed||this.frameRequest!==null)return;
      if(typeof requestAnimationFrame!=='function'){this.draw();return;}
      this.frameRequest=requestAnimationFrame(()=>{
        this.frameRequest=null;
        if(this.disposed)return;
        if(this.hoverCanvas)this.hoverCanvas.hidden=true;
        if(!this.drag?.moved&&!this.wheelMotion&&!this.touchMotion){this.draw();return;}
        if(!this.motionFrame()&&(root.KiCadMotionWebGL||!this.bitmapMotionFrame())){
          this.hideMotion();
          this.drawGeometry(true);
        }
        this.dispatchEvent(new CustomEvent('kicad:viewframe'));
      });
    }
    // Font metrics include alignment and baseline. Cache in world coordinates so
    // panning avoids both measuring and painting offscreen labels; zoom/flip
    // invalidate the entry, including minimum screen-size fonts. Without full
    // glyph metrics, retain the previous conservative always-visible behavior.
    displayTextLines(p){
      const lines=String(p.text||'').split('\n');if(!Number.isFinite(p.maxTextWidth))return lines;
      const zoom=this.viewport.camera.zoom,cached=this.overviewTextCache?.get(p);if(cached?.zoom===zoom&&cached.text===p.text)return cached.lines;
      const height=p.fontHeight||p.size||1.27,scale=p.fontWidth&&height>0?p.fontWidth/height:1,width=p.maxTextWidth*zoom/scale;
      let result=[];
      if(p.maxTextWidth*zoom>=(p.minDisplayWidthPx||0)&&width>0){
        const ctx=this.ctx;ctx.save();ctx.font=this.textFont(p);
        result=lines.map(line=>{const measure=text=>ctx.measureText(text)?.width;if(measure(line)<=width)return line;if(!(measure('…')<=width))return '';
          const chars=Array.from(line);let low=0,high=chars.length;while(low<high){const mid=Math.ceil((low+high)/2);if(measure(chars.slice(0,mid).join('')+'…')<=width)low=mid;else high=mid-1;}return chars.slice(0,low).join('')+'…';});ctx.restore();
      }
      this.overviewTextCache||=new WeakMap();this.overviewTextCache.set(p,{zoom,text:p.text,lines:result});return result;
    }
    textBounds(p){
      const c=this.viewport.camera,cached=this.textBoundsCache.get(p);
      if(cached&&cached.zoom===c.zoom&&cached.flipped===c.flipped&&cached.quarterTurns===c.quarterTurns)return cached.box;
      const ctx=this.ctx,height=p.fontHeight||p.size||1.27,scale=p.fontWidth&&height>0?p.fontWidth/height:1;
      let box=null,rects=[],minX=Infinity,minY=Infinity,maxX=-Infinity,maxY=-Infinity;
      ctx.save();
      try{
        ctx.font=this.textFont(p);ctx.textAlign=p.align||'center';ctx.textBaseline=p.baseline||'middle';
        const lines=this.displayTextLines(p),angle=((p.angle||0)*(c.flipped?-1:1)+c.quarterTurns*90)*Math.PI/180,cos=Math.cos(angle),sin=Math.sin(angle);
        for(let i=0;i<lines.length;i++){
          const m=ctx.measureText(lines[i]);
          if(!m||![m.actualBoundingBoxLeft,m.actualBoundingBoxRight,m.actualBoundingBoxAscent,m.actualBoundingBoxDescent].every(Number.isFinite)){rects=null;return null;}
          const offset=(i-(lines.length-1)/2)*height*c.zoom*(p.lineSpacing||1.5);
          const mirror=Boolean(p.mirrored??p.mirror)!==Boolean(c.flipped);
          const left=-(mirror?m.actualBoundingBoxRight:m.actualBoundingBoxLeft)*scale,right=(mirror?m.actualBoundingBoxLeft:m.actualBoundingBoxRight)*scale,top=offset-m.actualBoundingBoxAscent,bottom=offset+m.actualBoundingBoxDescent;
          rects.push({left,right,top,bottom});
          for(const [x,y] of [[left,top],[right,top],[right,bottom],[left,bottom]]){
            const d=c.screenDeltaToWorld({x:x*cos-y*sin,y:x*sin+y*cos}),wx=p.x+d.x,wy=p.y+d.y;
            minX=Math.min(minX,wx);maxX=Math.max(maxX,wx);minY=Math.min(minY,wy);maxY=Math.max(maxY,wy);
          }
        }
        if([minX,minY,maxX,maxY].every(Number.isFinite))box={x:minX,y:minY,w:maxX-minX,h:maxY-minY};
      }finally{ctx.restore();this.textBoundsCache.set(p,{zoom:c.zoom,flipped:c.flipped,quarterTurns:c.quarterTurns,box,rects});}
      return box;
    }
    textFont(p){
      const embedded=this.embeddedFontSet?.family(p.fontFace);
      const font=embedded?embedded+',sans-serif':p.fontFace?'sans-serif':'"Arial",sans-serif';
      return `${p.italic?'italic ':''}${p.bold?'bold ':''}${Math.max(1,p.minPixelSize||1,(p.fontHeight||p.size||1.27)*this.viewport.camera.zoom*4/3)}px ${font}`;
    }

    inView(p,view){let box;if(p.type==='text')box=this.textBounds(p);else{box=this.geometryBounds.get(p);if(box===undefined){box=spatialBounds(p);this.geometryBounds.set(p,box||null);}}if(!box)return true;const margin=3/this.viewport.camera.zoom;return box.x+box.w>=view.x-margin&&box.x<=view.x+view.w+margin&&box.y+box.h>=view.y-margin&&box.y<=view.y+view.h+margin;}
    draw(){
      if(this.motionTimer!==null){clearTimeout(this.motionTimer);this.motionTimer=null;}this.wheelMotion=false;
      this.hideMotion();
      if(!this.canUseMotionTexture())this.releaseMotion();
      else if(this.motion)this.motion.saved=null;
      this.drawGeometry();this.paintedCamera=this.hasSize?this.cameraState():null;this.paintPartHover();this.dispatchEvent(new CustomEvent('kicad:viewframe'));
    }
    drawGeometry(preview=false,dpr=this.pixelRatio||1){

      if(this.disposed||!this.hasSize)return;
      const c=this.viewport.camera,ctx=this.ctx;ctx.setTransform(1,0,0,1,0,0);ctx.fillStyle=this.background();ctx.fillRect(0,0,this.canvas.width,this.canvas.height);ctx.setTransform(dpr,0,0,dpr,0,0);

      if(this.grid&&c.zoom>2){let step=Number.isFinite(this.gridPitch)&&this.gridPitch>=.001&&this.gridPitch<=100?this.gridPitch:(c.zoom>12?1:5);while(step*c.zoom<5)step*=10;const b=c.bbox;ctx.fillStyle=this.dark?'#2b3b46':'#d3d3c7';let count=0;for(let x=Math.ceil(b.x/step)*step;x<b.x+b.w&&count<20000;x+=step)for(let y=Math.ceil(b.y/step)*step;y<b.y+b.h&&count++<20000;y+=step){const s=c.world_to_screen({x,y});ctx.fillRect(s.x,s.y,1,1);}}

      if(this.paperVisible){const page=this.design?.page||{width:297,height:210},a=c.world_to_screen({x:0,y:0}),b=c.world_to_screen({x:page.width,y:page.height});ctx.save();ctx.shadowColor='#00000055';ctx.shadowBlur=18;ctx.shadowOffsetY=4;ctx.fillStyle=this.paperColor();ctx.fillRect(Math.min(a.x,b.x),a.y,Math.abs(b.x-a.x),b.y-a.y);ctx.restore();}

      const view=c.bbox;
      const margin=3/c.zoom,selectedPaint=[],drillPaint=[];
      const candidates=this.renderPrimitives?[]:this.candidates({x:view.x-margin,y:view.y-margin,w:view.w+2*margin,h:view.h+2*margin});
      const densePreview=preview&&this.primitives.length>=DENSE_PREVIEW_MIN_PRIMITIVES&&candidates.length>DENSE_PREVIEW_PAINT_LIMIT;
      const step=densePreview?Math.ceil(candidates.length/DENSE_PREVIEW_PAINT_LIMIT):1;
      const selectedOwner=densePreview&&this.selected?.kind==='footprint-hit'?(this.selected.ownerUuid||this.selected.uuid):null;
      // Coalesce consecutive opaque straight copper strokes. Preserve painter
      // order across style changes, fills, and selected geometry.
      let pending = 0, strokeColor, strokeWidth;
      const flushTracks = () => {
        if (!pending) return;
        ctx.stroke();
        ctx.restore();
        pending = 0;
      };
      const batchTrack = (p, color) => {
        const width = Math.max(.65,(p.width||.15)*c.zoom);
        if (pending && (strokeColor !== color || strokeWidth !== width || pending === 256)) flushTracks();
        if (!pending) {
          ctx.save();
          ctx.strokeStyle = strokeColor = color;
          ctx.lineWidth = strokeWidth = width;
          ctx.lineCap = 'round';
          ctx.lineJoin = 'round';
          ctx.globalAlpha = 1;
          ctx.setLineDash?.([]);
          ctx.beginPath();
        }
        const start = c.world_to_screen(p.points[0]), end = c.world_to_screen(p.points[1]);
        ctx.moveTo(start.x,start.y);
        ctx.lineTo(end.x,end.y);
        pending++;
      };
      if(this.renderPrimitives)this.renderPrimitives(ctx,c,this);else for(let i=0;i<candidates.length;i++){
        const p=candidates[i],matches=this.netPrimitives.has(p);
        if(densePreview&&!matches&&p!==this.selected&&!(selectedOwner&&p.ownerUuid===selectedOwner)&&!p.frame&&p.layer!=='Edge.Cuts'){
          // Stagger the sample across groups so interleaved layers can remain visible.
          if(i%step!==Math.floor(i/step)%step||p.type==='text'||p.type==='image'||p.kind==='zone'||p.hole)continue;
        }

        if(!this.geometryVisible(p)||!this.inView(p,view))continue;

        if(p.frame&&!this.paperVisible)continue;

        if(!p.frame&&p.layer&&!this.visibleLayers.has(p.layer))continue;

        const dim=this.netHighlight&&!p.frame&&!matches,layerDim=this.focusedLayer&&!p.frame&&p.layer!==this.focusedLayer;

        if(layerDim&&this.layerFocusMode==='isolate'&&!p.hole&&p.layer!=='Edge.Cuts')continue;

        if(dim&&this.isolateNet&&!p.hole&&p.layer!=='Edge.Cuts')continue;

        const style={color:dim&&this.highlightMode!=='bright'&&!p.hole?(this.dark?'#87919d':'#8b919a'):p.kind==='via'?'#c5aa6c':matches&&!p.hole&&this.selectionColor!=='source'?(this.pcb?'#ffe18b':this.dark?'#78d5ff':'#006ca8'):this.netClassColors?.get(p)||null,opacity:Math.min(dim&&!p.hole&&this.highlightMode!=='bright'&&p.layer!=='Edge.Cuts'?(this.pcb?.045:.38):1,layerDim&&!p.hole?.18:1)};
        if (this.pcb && !this.netHighlight && !layerDim && !p.frame && !p.hole &&
            p.kind === 'track' && p.type === 'line' && p.points?.length === 2 &&
            !p.fill && !p.lineStyle && p.stroke !== false &&
            (p.opacity === undefined || p.opacity === 1) && this.paint === Viewport.prototype.paint) {
          batchTrack(p,this.strokeColor(p,style.color));
          continue;
        }
        flushTracks();
        if(this.pcb&&this.netHighlight&&this.highlightMode!=='bright'){
          if(p.hole)drillPaint.push({p,style});else if(matches)selectedPaint.push({p,style});else this.paint(p,style);
        }else this.paint(p,style);

      }

      flushTracks();
      // Keep saved net copper above faint context, with physical drill cutouts last.
      for(const {p,style}of selectedPaint)this.paint(p,style);
      for(const {p,style}of drillPaint)this.paint(p,style);
      if(!preview)this.drawNetNames();
      if(this.selected&&!this.netHighlight&&this.geometryVisible(this.selected)&&(!this.selected.layer||this.visibleLayers.has(this.selected.layer))&&(!this.focusedLayer||this.layerFocusMode!=='isolate'||this.selected.layer===this.focusedLayer)&&(!this.netHighlight||!this.isolateNet||this.netPrimitives.has(this.selected))){const b=bounds([this.selected],true);if(b){const a=c.world_to_screen(b);ctx.strokeStyle='#80f0b7';ctx.lineWidth=2;ctx.setLineDash([4,3]);ctx.strokeRect(a.x-4,a.y-4,(c.flipped?-1:1)*b.w*c.zoom+8,b.h*c.zoom+8);ctx.setLineDash([]);}}

    }

    fillGraphic(p,path,worldSpace){
      const ctx=this.ctx;
      if(['hatch','reverse_hatch','cross_hatch'].includes(p.fill)){
        this.hatchPatterns||=new Map();const color=ctx.fillStyle,key=p.fill+'|'+color+'|'+(p.width||.15);let pattern=this.hatchPatterns.get(key);
        if(!pattern){const tile=document.createElement('canvas');tile.width=tile.height=16;const ink=tile.getContext('2d');ink.strokeStyle=color;ink.globalAlpha=.4;ink.lineWidth=Math.max(.025,(p.width||.15)/2)*16/(Math.SQRT2*4);ink.beginPath();
          if(p.fill!=='reverse_hatch')for(const x of [-16,0,16]){ink.moveTo(x,0);ink.lineTo(x+16,16);}
          if(p.fill!=='hatch')for(const x of [-16,0,16]){ink.moveTo(x,16);ink.lineTo(x+16,0);}
          ink.stroke();pattern=ctx.createPattern(tile,'repeat');if(this.hatchPatterns.size>=32)this.hatchPatterns.clear();if(pattern)this.hatchPatterns.set(key,pattern);
        }
        if(pattern){const period=Math.SQRT2*4,scale=worldSpace?period/16:Math.max(4,period*this.viewport.camera.zoom)/16;pattern.setTransform?.({a:scale,b:0,c:0,d:scale,e:0,f:0});ctx.fillStyle=pattern;}
      }
      if(path)ctx.fill(path);else ctx.fill();
    }
    strokeColor(p,override){
      const color=override||p.color||'#819baa';
      if(p.hole)return this.paperVisible?this.paperColor():this.background();
      if(override)return color;
      if(!this.pcb&&!p.sourceColor)return schematicColor(color,this.dark);
      if(p.layer&&root.KiCadLayerColors?.reviewColorFor)return root.KiCadLayerColors.reviewColorFor(p.layer,this.dark);
      return color;
    }
    paint(p,style={}){
      const ctx=this.ctx,c=this.viewport.camera;ctx.save();
      const color=this.strokeColor(p,style.color);

      ctx.strokeStyle=color;ctx.fillStyle=style.color?color:['color','hatch','reverse_hatch','cross_hatch'].includes(p.fill)&&p.fillColor?p.fillColor:p.fill==='background'?(this.dark?'#32322a':'#ffffc2'):color;ctx.globalAlpha=Math.max(0,Math.min(1,(this.netPrimitives.has(p)&&!p.hole?1:Number.isFinite(p.opacity)?p.opacity:1)*(style.opacity??1)));ctx.lineWidth=Math.max(.65,(p.width||.15)*c.zoom);if(this.netPrimitives.has(p)&&!p.hole)ctx.lineWidth+=1.5;ctx.lineCap='round';ctx.lineJoin='round';const dashUnit=Math.max(1,ctx.lineWidth),dash={dash:[11,4],dot:[.2,4],dash_dot:[11,4,.2,4],dash_dot_dot:[11,4,.2,4,.2,4]}[p.lineStyle];ctx.setLineDash?.(dash?dash.map(n=>n*dashUnit):[]);

      if(p.type==='text'){const q=c.world_to_screen(p);ctx.translate(q.x,q.y);ctx.rotate(((p.angle||0)*(c.flipped?-1:1)+c.quarterTurns*90)*Math.PI/180);if(Boolean(p.mirrored??p.mirror)!==Boolean(c.flipped))ctx.scale(-1,1);ctx.font=this.textFont(p);ctx.textAlign=p.align||'center';ctx.textBaseline=p.baseline||'middle';const height=p.fontHeight||p.size||1.27;if(p.fontWidth&&height>0)ctx.scale(p.fontWidth/height,1);const lines=this.displayTextLines(p);lines.forEach((text,i)=>ctx.fillText(text,0,(i-(lines.length-1)/2)*height*c.zoom*(p.lineSpacing||1.5)));ctx.restore();return;}
      if(p.type==='image'){const entry=this.imageResource(p);if(entry.ready&&p.points?.length===4){const [a,b,,d]=p.points.map(point=>c.world_to_screen(point));ctx.transform((b.x-a.x)/p.pixelWidth,(b.y-a.y)/p.pixelWidth,(d.x-a.x)/p.pixelHeight,(d.y-a.y)/p.pixelHeight,a.x,a.y);ctx.drawImage(entry.image,0,0);}ctx.restore();return;}

      ctx.beginPath();

      if(p.type==='arc'){const a=arcThrough(p.start,p.mid,p.end);if(a){const q=c.world_to_screen(a);const start=c.flipped?Math.PI-a.start:a.start,end=c.flipped?Math.PI-a.end:a.end;ctx.arc(q.x,q.y,a.r*c.zoom,start,end,c.flipped?!a.ccw:a.ccw);}else{const ps=points(p);if(ps.length){const q=c.world_to_screen(ps[0]);ctx.moveTo(q.x,q.y);for(const t of ps.slice(1)){const s=c.world_to_screen(t);ctx.lineTo(s.x,s.y);}}}}

      else if(p.type==='circle'||p.type==='pad'&&p.r){const q=c.world_to_screen(p);ctx.arc(q.x,q.y,p.r*c.zoom,0,2*Math.PI);}

      else if(p.zoneCoordinates){
        const xy=p.zoneCoordinates,origin=c.world_to_screen({x:0,y:0}),
          x=c.worldDeltaToScreen({x:1,y:0}),y=c.worldDeltaToScreen({x:0,y:1});
        ctx.moveTo(origin.x+xy[0]*x.x+xy[1]*y.x,origin.y+xy[0]*x.y+xy[1]*y.y);
        for(let i=2;i<xy.length;i+=2)
          ctx.lineTo(origin.x+xy[i]*x.x+xy[i+1]*y.x,origin.y+xy[i]*x.y+xy[i+1]*y.y);
        ctx.closePath();
      }
      else{const ps=points(p);if(ps.length){const q=c.world_to_screen(ps[0]);ctx.moveTo(q.x,q.y);for(const t of ps.slice(1)){const s=c.world_to_screen(t);ctx.lineTo(s.x,s.y);}if(p.type==='polygon'||p.type==='pad')ctx.closePath();}}

      if(p.fill)this.fillGraphic(p,null,false);if(!p.hole&&p.stroke!==false)ctx.stroke();ctx.restore();

    }

    dispose(){if(this.disposed)return;this.disposed=true;this.bitmapMotion?.remove();this.bitmapMotion=null;this.bitmapMotionState=null;this.paintedCamera=null;this.hoverCanvas?.remove();this.hoverCanvas=null;this.hoverParts=null;this.hoveredPrimitive=null;this.embeddedFontSet?.dispose();this.embeddedFontSet=null;this.fontSource=null;this.inheritedFonts=null;this.fontFaces=null;this.hatchPatterns?.clear();this.releaseImages();if(this.frameRequest!==null&&typeof cancelAnimationFrame==='function')cancelAnimationFrame(this.frameRequest);this.frameRequest=null;if(this.motionTimer!==null)clearTimeout(this.motionTimer);this.motionTimer=null;this.releaseMotion();this.observer.disconnect();for(const stop of this.handlers)stop();this.handlers=[];this.primitives=[];this.design=null;this.document=null;this.selected=null;this.netHighlight=null;this.netPrimitives.clear();this.netNames?.clear();this.netClassColors?.clear();this.layerSnapshot=null;this.canvas.width=this.canvas.height=1;this.canvas.remove();}

  }

  function paintBoardPrimitives(ctx,camera,primitives,color){
    const painter=Object.create(Viewport.prototype);
    Object.assign(painter,{ctx,viewport:{camera},pcb:true,netPrimitives:new Set(),pointCache:new WeakMap(),pathCache:new WeakMap(),pathCacheCount:0,pathCachePoints:0});
    for(const primitive of primitives)painter.paint(primitive,{color});
  }

  root.KiCadOwnedCanvas={paintBoardPrimitives,netLabels,Camera,arcThrough,bounds,hitPrimitive,primitiveDistance,schematicColor,objectTypes,objectCategory,objectVisible,canvasViewport,mount:(host,options)=>new Viewport(host,options)};

  if(typeof module==='object'&&module.exports)module.exports=root.KiCadOwnedCanvas;

})(globalThis);