Runtime Notes
Content runs in JavaScriptCore, not in a browser. The type declarations (the API reference) are compiled from the engine's source, with only the APIs that really exist. The main points:
- There is no DOM and no Node.js: no
document, nowindow; see Network for the network, Storage and Files forlocalStorageand files, and Audio for sound. Modules in the runtime are CommonJSrequire(); to useimport, put your code insrc/and let the SDK bundle it (see Projects and Builds). DesktopEngine.systemStatustells how the Mac is doing: the processor, memory, the network, power and the battery, temperature and disk, with events, with thesystem-infopermission.DesktopEngine.inputfollows the mouse anywhere on screen with themousepermission, see Following the Mouse.DesktopEngine.windowstells where other apps' windows are with thewindow-positionspermission, see Other Apps' Windows.DesktopEngine.systemAudioanalyses the sound other apps play on the Mac, overall and in bands from low to high, for music visualizers, with thesystem-audiopermission, see What the Mac plays.
Globals
Besides the language's own (Promise, Intl, WebAssembly…), content has these globals of browsers:
- Timing:
setTimeout,setInterval,clearTimeout,clearInterval,requestAnimationFrame,cancelAnimationFrame,queueMicrotaskandperformance. console,globalThisandself, andrequire()for CommonJS modules.atobandbtoa;crypto.getRandomValues()andcrypto.randomUUID(), with the system's cryptographically strong random numbers (API 4).fetch,Headers,Request,Response,AbortController,WebSocketandXMLHttpRequest, see Network.URL,URLSearchParams,TextEncoder,TextDecoder,Blob,File,FileReader,FormDataand streams,structuredClone,MessageChannel, see Data.BroadcastChannel, to other running content (API 6), see Messages Between Content.FontFace,FontFaceSetandFontFaceSetLoadEvent, withDesktopEngine.fontsfor the content's own fonts (API 9), see Fonts.DOMException,Event,CustomEventandEventTarget.navigator:userAgent(a Mac, without Safari or WebKit),platform,languageandlanguages(the user's preferred languages, as browsers write them:zh-CN, not the system'szh-Hans-CN),hardwareConcurrencyandmaxTouchPoints(0). Nothing else of a browser's navigator.localStorageandsessionStorage, see Storage and Files.Audio,AudioContextand the other Web Audio classes, see Audio.
WebAssembly has only what JavaScript engines implement (the WebAssembly JS API), not the instantiateStreaming and compileStreaming browsers add for their Response. Read the file and compile what you read: WebAssembly.instantiate(await (await fetch('module.wasm')).arrayBuffer(), imports). Emscripten's and wasm-bindgen's loaders do this by themselves when the streaming functions aren't there.
Canvas
- Canvases have 2D, WebGL 1 and WebGL 2 (
getContext('webgl2'), OpenGL ES 3.0 on ANGLE).texImage3D/texSubImage3Dalso take images,ImageDataand canvases, with the layers top to bottom in the source image (spaced byUNPACK_IMAGE_HEIGHT, the height by default). Macs with Apple silicon haveWEBGL_compressed_texture_astc. - The 2D context has the web's compositing and blend modes (
multiply,screen,overlay… since API 5), linear, radial and conic gradients,letterSpacing/wordSpacing,Path2D(SVG path data too) andisPointInPath/isPointInStrokefor hit testing, since API 5; notfilter,fontKerningor the other text settings. Text is drawn in installed fonts and the content's own, see Fonts. AVideois an image source fordrawImage,createPattern,createImageBitmapand WebGL'stexImage2D(API 9), see Video. Drawing inlighten,darken, a blend mode, or a mode that clears the canvas outside what's drawn (copy,source-in…) goes through a layer the size of the canvas: about 0.25 ms of CPU a draw, so draw in them with few calls. createImageBitmap(API 5) makes anImageBitmapof a loadedCanvasImage, a canvas,ImageDataor aBlobof an image file (decoded away from the JavaScript thread), cropped, resized or flipped as its options say;drawImage,createPatternand WebGL'stexImage2Dtake it. As in browsers, WebGL uploads it as it is:UNPACK_FLIP_Y_WEBGLandUNPACK_PREMULTIPLY_ALPHA_WEBGLdon't apply to it. three.js'sImageBitmapLoader(whichGLTFLoaderuses for textures) works with it.new ImageData(width, height)makes pixel data too.- As in browsers, a WebGL 2 context isn't
instanceof WebGLRenderingContext: check the version withinstanceof WebGL2RenderingContext. - Canvases render with Metal. On old Macs whose graphics don't support Metal, every
getContextreturns null andCanvasImagefails to load, so check what they return. - Canvases have an alpha channel by default (as on the web). Widget and companion windows are transparent, so the desktop shows where nothing is drawn.
- A canvas's drawing buffer (
width×height) is stretched to the size of its style, smoothly by default. For pixel art, draw 1:1 in a small buffer and set the styleimageRendering: 'pixelated'to scale it up into sharp squares (the built-in pixel cat is drawn this way);'crisp-edges'doesn't smooth when scaling down either. - Canvases follow the scale of their display, see Displays and scale.
- The GPU gives a frame's work a few seconds at most. When a canvas takes longer (a huge draw, a shader that loops for long), macOS resets the GPU, which freezes the whole screen for a moment. The canvas' context is lost then, for good, so that it can't happen again: the canvas fires
webglcontextlost(contextlostfor 2D), keeps what it last showed, and draws nothing more; the context'sisContextLost()returns true. Keep each frame's GPU work small, especially on large canvases. WideshadowBlurshadows are blurred at a lower resolution, which looks the same. - WebGL buffers, textures and shader programs are freed by
gl.deleteXxx()or when the canvas is destroyed; dropping them in JavaScript doesn't delete them (they may still be bound). Programs that keep creating resources mustdeletethem.
Not there yet
crypto.subtle, Workers andOffscreenCanvas(aDesktopEngine.Canvasthat isn't in a window works off screen);location.- IndexedDB.
- Native ES modules in the runtime (bundling
src/covers them for now).
Ask for what you need on GitHub.
