UNPKG

@wayrisignals/kicad-webview

Version:

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

3 lines (2 loc) • 7.11 kB
/*! KiCad WebView 1.2.1 | Copyright (c) 2026 wayri | SPDX-License-Identifier: MPL-2.0 | Source: https://github.com/wayri/kicad-webview/tree/v1.2.1 | Keep LICENSE and THIRD-PARTY-NOTICES.md; bundled dependencies retain their licenses. */ import{b as _}from"./chunk-LCG4X7QG.js";import"./chunk-TZWR7E27.js";function W(u,b,m){let o=u||{x:0,y:0,w:100,h:100};return{x:o.x+o.w/2,y:o.y+o.h/2,zoom:Math.max(.001,Math.min(b/Math.max(.01,o.w),m/Math.max(.01,o.h))*.88)}}function q(u,b,m,o,r){return{x:u.x+(o-b/2)/u.zoom,y:u.y-(r-m/2)/u.zoom}}function Y(u,b,m,o,r,d){let n=q(u,b,m,o,r),l=Math.max(.001,Math.min(1e5,u.zoom*d));return{x:n.x-(o-b/2)/l,y:n.y+(r-m/2)/l,zoom:l}}var B=["#f56d78","#62a5ff","#e1b85c","#73d5ae","#c49af1","#73cfdd","#e995bf","#c5d673"],j=["#a9233b","#185797","#795000","#176747","#724097","#126477","#90315f","#4d6510"];function X(u,{dark:b=!1}={}){let m=u.ownerDocument,o=m.createElement("canvas"),r=m.createElement("canvas"),d=m.createElement("div");o.style.cssText="width:100%;height:100%;display:block;touch-action:none;outline:none",o.tabIndex=0,o.setAttribute("role","img"),o.setAttribute("aria-label","Interactive manufacturing layers; drag to pan, wheel to zoom, click to inspect; use toolbar to measure"),d.setAttribute("role","status"),d.style.cssText="position:absolute;left:12px;right:12px;bottom:12px;width:fit-content;max-width:calc(100% - 24px);padding:8px 12px;border-radius:8px;font:12px/1.5 system-ui;overflow-wrap:anywhere;pointer-events:none",d.hidden=!0,u.append(o,d);let n=o.getContext("2d"),l=r.getContext("2d");if(!n||!l)throw new Error("Canvas2D is required for manufacturing viewing.");let w=new EventTarget,M,i,f=1,c=1,p=1,z=!1,v=null,S=!1,y=null,g=null,T=!1,h=null,E=new Map,P=[],C=(e,t,a)=>{o.addEventListener(e,t,a),P.push([e,t,a])},s=Object.assign(w,{dark:b,visibleLayers:new Set,measuring:!1,setDocument(e){M=e,E.clear(),h=null,y=g=null;for(let t of M.camLayers)for(let a of t.operations)E.set(a,new Path2D(a.path));s.visibleLayers=new Set(M.layers),O(),s.zoom_to_board(),s.draw()},zoom_to_board(){i=W(M?.bounds,f,c)},clearSelection(){h=null,d.textContent="",k()},setMeasuring(e){s.measuring=!!e,y=g=null,T=!1,s.draw()},draw:k,dispose(){z=!0,A?.disconnect();for(let[e,t,a]of P)o.removeEventListener(e,t,a);E.clear(),o.remove(),d.remove(),o.width=o.height=r.width=r.height=1}});function O(){if(z)return;let e=u.getBoundingClientRect();f=Math.max(1,e.width),c=Math.max(1,e.height),p=Math.min(globalThis.devicePixelRatio||1,3,Math.sqrt(8e6/f/c),8192/f,8192/c),o.width=r.width=Math.max(1,Math.floor(f*p)),o.height=r.height=Math.max(1,Math.floor(c*p)),i||=W(M?.bounds,f,c),k()}function D(e){e.setTransform(p*i.zoom,0,0,-p*i.zoom,p*(f/2-i.x*i.zoom),p*(c/2+i.y*i.zoom))}function J(e,t){let a=E.get(t);t.fill===!1?(e.lineWidth=t.width,e.lineCap="round",e.lineJoin="round",e.stroke(a)):e.fill(a,"evenodd")}function k(){if(z||!i)return;let e=h?.operation.metadata;if(d.hidden=!h||s.measuring,d.style.background=s.dark?"#182330ed":"#ffffffed",d.style.color=s.dark?"#e1eaf3":"#253445",h){let t=h.layer+" \xB7 "+h.operation.kind+" \xB7 "+JSON.stringify(e||{}).slice(0,400);d.textContent!==t&&(d.textContent=t)}if(n.setTransform(1,0,0,1,0,0),n.globalAlpha=1,n.globalCompositeOperation="source-over",n.fillStyle=s.dark?"#111923":"#f1f4f8",n.fillRect(0,0,o.width,o.height),M?.camLayers.forEach((t,a)=>{if(s.visibleLayers.has(t.id)){l.setTransform(1,0,0,1,0,0),l.globalCompositeOperation="source-over",l.clearRect(0,0,r.width,r.height),D(l),l.fillStyle=l.strokeStyle=t.color||(s.dark?B:j)[a%B.length];for(let x of t.operations)l.globalCompositeOperation=x.polarity==="clear"?"destination-out":"source-over",J(l,x);n.globalAlpha=.82,n.drawImage(r,0,0),n.globalAlpha=1}}),h&&s.visibleLayers.has(h.layer)&&(D(n),n.globalCompositeOperation="source-over",n.strokeStyle=s.dark?"#ffffff":"#172d47",n.lineWidth=2/i.zoom,n.stroke(E.get(h.operation))),y&&g){D(n),n.strokeStyle=s.dark?"#f7e182":"#715800",n.lineWidth=1.5/i.zoom,n.beginPath(),n.moveTo(y.x,y.y),n.lineTo(g.x,g.y),n.stroke();let t=Math.hypot(g.x-y.x,g.y-y.y);n.setTransform(p,0,0,p,0,0),n.font="600 12px system-ui";let a=t.toFixed(3)+" mm";n.fillStyle=s.dark?"#172330":"#ffffff",n.fillRect(12,c-40,n.measureText(a).width+24,28),n.fillStyle=s.dark?"#f7e182":"#384353",n.fillText(a,24,c-21)}}let L=e=>{let t=o.getBoundingClientRect();return{x:e.clientX-t.left,y:e.clientY-t.top}},R=e=>{let t=L(e);return q(i,f,c,t.x,t.y)};function N(e){l.save(),l.setTransform(1,0,0,1,0,0);try{for(let t of[...M.camLayers].reverse())if(s.visibleLayers.has(t.id))for(let a of[...t.operations].reverse()){let x=a.bounds;if(x&&(e.x<x.x||e.x>x.x+x.w||e.y<x.y||e.y>x.y+x.h))continue;let I=E.get(a);if(l.lineWidth=a.width||.001,a.fill===!1?l.isPointInStroke(I,e.x,e.y):l.isPointInPath(I,e.x,e.y,"evenodd")){if(a.polarity==="clear")break;return{layer:t.id,operation:a}}}return null}finally{l.restore()}}C("pointerdown",e=>{e.button!==0&&e.button!==2||(o.focus(),o.setPointerCapture(e.pointerId),v={point:L(e),camera:{...i},button:e.button},S=!1)}),C("pointermove",e=>{let t=L(e);if(v){let a=t.x-v.point.x,x=t.y-v.point.y;Math.hypot(a,x)>3&&(S=!0),S&&(i={...v.camera,x:v.camera.x-a/i.zoom,y:v.camera.y+x/i.zoom},k())}else s.measuring&&y&&!T&&(g=R(e),k())}),C("pointerup",e=>{if(!v)return;let t=v.button===0;if(v=null,S||!t)return;let a=R(e);s.measuring?!y||T?(y=a,g=null,T=!1):(g=a,T=!0,s.dispatchEvent(new CustomEvent("measure",{detail:{start:y,end:g,distance:Math.hypot(a.x-y.x,a.y-y.y),unit:"mm"}}))):(h=N(a),s.dispatchEvent(new CustomEvent("kicad:select",{detail:{item:h?{...h.operation.metadata,id:h.operation.id,layer:h.layer,kind:M.format}:null}}))),k()}),C("pointercancel",()=>{v=null}),C("contextmenu",e=>e.preventDefault()),C("wheel",e=>{e.preventDefault();let t=L(e);i=Y(i,f,c,t.x,t.y,Math.exp(-Math.max(-200,Math.min(200,e.deltaY))*.005)),k()},{passive:!1}),C("keydown",e=>{e.key==="Escape"?(h=null,y=g=null,k()):e.key.toLowerCase()==="f"&&(s.zoom_to_board(),k())});let A=typeof ResizeObserver=="function"?new ResizeObserver(O):null;return A?.observe(u),O(),s}function G(u,b={}){let m=[],o=[],r=null,d="manufacturing",n=0,l=0;for(let w of u){let M=w.source??new TextDecoder("utf-8",{fatal:!0}).decode(w.bytes),i=_(M,{...b,...b.importOptions?.[w.path],path:w.path});u.length===1&&(d=i.format);for(let c of i.layers)for(let p of c.operations)if(n++,l+=p.path.length,n>2e5||l>16e6)throw new Error("Manufacturing collection exceeds 200000 operations or 16 million path characters.");for(let c of i.warnings)o.length<500&&o.push(w.path+": "+c);for(let c of i.layers){let p=w.path+(i.layers.length>1?" / "+c.id:"");m.push({...c,id:p,name:p,operations:c.operations.map(z=>({...z,id:w.path+":"+z.id}))})}let f=i.bounds;f&&(r=r?{x:Math.min(r.x,f.x),y:Math.min(r.y,f.y),w:Math.max(r.x+r.w,f.x+f.w)-Math.min(r.x,f.x),h:Math.max(r.y+r.h,f.y+f.h)-Math.min(r.y,f.y)}:{...f})}if(!m.length)throw new Error("No supported manufacturing layers were found.");return{format:d,layers:m.map(w=>w.id),camLayers:m,bounds:r,warnings:o,units:"mm",primitives:[]}}export{W as fitCamera,G as manufacturingDesign,X as mountManufacturing,q as screenToWorld,Y as zoomCamera};