cables DocumentationHow To UseWorking with filesKeyboard ShortcutsUser Interface WalkthroughBeginner TutorialBeginner 1: Drawing A CircleBeginner 2: TransformationsBeginner 3: ColorMore TransformationsIntermediateImage CompositionsPost-Processing 3D ScenesExporting And EmbeddingHow to serve files externally and fix CORS headersExporting PatchesExport using the cables command line interfaceExport via IframeExport creating a standalone executableExport to github (pages)Export and deploy to netlifyExport full PatchExport a ZIP fileExternal triggers / functionsUsing variablesPreviewing / uploading exported cables patchesExamples for EmbeddingPermissionsUsersPatchesTeamsOpsMultiplayerPatchlistsCoding OpsCreating AttachmentsGeneral op/Port CallbacksPortsDynamic PortsArray PortsBoolean portsInteger Number PortsObject PortsString portsTrigger PortsFloating Point Number PortsGUI/UI attributesHello Op - Part 1LibrariesDeveloping OpsRenaming / Creating a new versionCreating Viz OpsGuidelinesObject PortsPatching Ops / SubPatchOpsWriting ShadersWeb Audio Op DevelopmentDeveloping CablesRepositoriesSet up local environmentScriptsHow to work with forksGenerated DocumentationLightingLightsShadowsWorking With AudioBasic Audio SetupWorking with EffectsReal-Time Audio Analyzation & Audio VisualizationOffline Audio Visualization & AnalyzationCommunicationcables APIOptimizing Performance In PatchesTools for debugging your patchHow to optimize cables patches with the Performance opHow to optimize cables patches with the ProfilerCommon pitfalls with the "usual suspects"Optimizing arraysDebugging Shaders with ShaderInfoFAQAudio in web browsersHow to make demoscene demos with cables.glEmbeddingHow to integrate my cables patch into my CMS (webflow/wix/squarespace/...)?How to remove grey rectangles on touch (mobile)?Why doesn't the DownloadTexture op work on iOS?How to disable page scrolling on mobile?Mobile tippsHow to run exported patches on your local machineTransparent CanvasFeatures and SupportHow to contribute code to cablesWill there be support for (animated) GIFs?Can i use a website as a texture?Screenshots and Video recordingHow to report a bug in cablesHow can I share a patch to get help?How can I support cables?Video playback in cablesGeneral questionsWhat is dev.cables.glHTML And CSS In CablesJavascript Frameworkscordova / phonegapelectronreactvuejsLicenses and paymentWhat license do i need to use cables?Will I have to pay for it in the future?How is my work licensed when using cables?Does cables support midi and OSC?Patch PermissionsMy User Profile & Social MediaShadertoyCables at schools and universitiesTechnical questionsWebGL1 and WebGL2

General op/Port Callbacks

In order to get informed on port-value-changes, function-triggers (also see Ports) or general op-events there are a number of callbacks your op can implement.

To be informed of port-value-changes, function-triggers (also see Ports) or general op-events there are a number of callbacks your op can implement.

Tip: It’s always a good idea to inspect the code of existing ops by selecting an op and then pressing view code in the op-settings on the right.

Button

Shortcut - click on an op and press the 'e' key

Port Callbacks

onChange

Can be implemented for the following port types:

Number
String
Boolean
Array
Object

Every time a connected op calls the myOutPort.set(...) method, the in-port-callback onChange is called.

myPort.onChange = function()
{
  op.log('value of myPort changed to: ', myPort.get());
};

onTrigger

Can be implemented for the port type trigger.

Every time a connected op calls myInputPort.onTriggered() the connected in-ports’ onTrigger callback is called.

If your op needs to update its values continuously it should have an input port of type trigger , which you can then connect to the MainLoop

onLinkChanged

Gets called whenever a port is connected / disconnected. It may not have a value yet.

myPort.onLinkChanged = function()
{
    if( myPort.isLinked() )
    {
        // port connected
    }
    else
    {
        // port disconnected
    }
};

General Op Callbacks

init

In case you have some initialisation code for your op you can place it inside an init function or just plainly into
the op-code outside of any callbacks or functions.

When you inspect existing ops by pressing the View Code button in the op parameters, you will notice that most ops don’t use this function, as
op init is done asynchronously and most of the time it's better to just initalize outside of callbacks and handle port-value-changes in
the corresponding onChange;

Please be aware that this function will be called twice on patch load. If you need to initialize a variable global to the op,
you might be better off doing that outside of any callback. If you do this in init, create a variable, set them null and check for that.

const inPort = op.inFloat('In Value');

op.init = function()
{
    const value = inPort.get();
}

onLoaded

Gets called when the whole patch is loaded / all ops are linked / all external libraries loaded etc.
You normally won't need this, as op-specific init-code can just be put in your op-code without a callback.
op.onLoaded is not called when the op has just been added to the patch, only when the patch is loaded.

op.onLoaded = function()
{
    // do something on loading
};

onDelete

If your op needs to clean up after itself when it is deleted from the patch you can implement onDelete:

op.onDelete = function()
{
    // do some manual cleanup here
};

op.setUiError

Sometimes you will want to create a UI element to show if there is an error or a warning when some condition occurs in the code for an op.

To do this use the following format:

if(condition) op.setUiError("errorID", "error ID/must be unique per error","Error message to show in UI",0);
// this resets the error message so it disappears
else op.setUiError("errorID",null);

The number in the last part of the function defines what kind of error is shown
0 - hint / grey color
1 - warning / orange color
2 - error / red color / this will also place a red dot on the right hand side of the op


Button


example code to show an error:

// create a port of the type boolean
const switch1=op.inBool("Error",false);
// if port changes run this function
switch1.onChange=function()
{
    if(switch1.get()) op.setUiError("error1","switch 1 is true",2);
    else op.setUiError("error1",null);
}

Logging

To debug your ops you can press ctrl+shift+i (in chrome) to open the developer tools
The following line of of code will print 'hello world' to the console

op.log( 'hello world' );

Button

Do not use console.log()!

op.log() is not shown if the patch is embedded and the silent parameter is set, also you get a reference to the op which is producing the log-message in your browsers developer tools.
Be aware that logging things too often in the console can slow down the browser, use this only for debugging and remove all op.log() code when you are done

canvas resize

Whenever the canvas is resized a resize event is fired, you can add a listener to this event to handle canvas-size changes in your ops.

op.patch.cgl.addEventListener("resize",onResize);

function onResize()
{
    // do something
}

Found a problem? Edit this file on github and contribute to cables!