API: Canvas (AB) (r2)

API Uses canvas Graphics libraries AB framework Worlds using this API Starter Worlds
Canvas (AB) (r2) Yes None Yes 8 Worlds Starter Worlds

This API is for Worlds that use a canvas to do their own 2D or WebGL rendering. It does not come with a graphics library. It comes with an Ancient Brain framework to make writing Worlds easier.

This API has the following features:



This API defines a global variable called "ABWorld" as follows.

 ABWorld;       // instance of class ABWorldClass - basic functionality for this API
  // A canvas that may be drawn on using getContext("2d") or getContext("webgl")

  // create canvas:		

	ABWorld.init ( color );   
  // get drawing context:
	ABWorld.getContext ( type );

Custom getContext

ABWorld.getContext creates a drawing context of a certain type, with other arguments set to default:
 var ctx = ABWorld.getContext ( "2d" );
 var ctx = ABWorld.getContext ( "webgl" );
If you want to customise the drawing context, do this:

 var ctx = ABWorld.canvas.getContext ( type, args );

Custom getContext: preserveDrawingBuffer

The following is important if you are (1) creating a customised drawing context, for (2) "webgl" context.

Ancient Brain needs runs to be able to generate a screenshot.
If you are doing WebGL rendering (context "webgl"), you need in canvas.getContext to set "preserveDrawingBuffer" = true.
If you use ABWorld.getContext, this is taken care of for you.

If you use your own custom getContext, then to make sure your run can generate a screenshot, you need to do something like this:

 ctx = ABWorld.canvas.getContext ( "webgl", { preserveDrawingBuffer: true } );	
You might set it to true for "generate image" runs and false for normal runs:
 if ( AB.isScreenshotRun() )
	 ctx = ABWorld.canvas.getContext ( "webgl", { preserveDrawingBuffer: true } );	
	 ctx = ABWorld.canvas.getContext ( "webgl", { preserveDrawingBuffer: false } );	


Examples of Worlds that use this API:

canvas webgl
522 runs ♦ 2 likes
By Starter user  
Created: 17 Sep 2017
Modified: 13 Nov 2023
Minimal starter World for "Canvas" API (no graphics library). Program using getContext("webgl")....
canvas 2d
355 runs ♦ 2 likes
By Starter user  
Created: 4 Oct 2017
Modified: 17 Sep 2023
Minimal starter World for "Canvas" API (no graphics library). Program using getContext("2d"). Pa...