API: Canvas (AB) (r1)

API Uses canvas Graphics libraries AB framework Worlds using this API Starter Worlds
Canvas (AB) (r1) Yes None Yes 67 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:



Templates


ABWorld

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")

	ABWorld.canvas;		
		  
		
  // 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 } );	
 else                       
	 ctx = ABWorld.canvas.getContext ( "webgl", { preserveDrawingBuffer: false } );	

Examples

Examples of Worlds that use this API:


Cells
165 runs ♦ 0 likes
By Enhanced  
Created: 27 Jun 2018
Modified: 17 Sep 2023
'Cells' is an animation that displays 4 team of cells trying to infect each other, until total d...
CELLS WITH MIND ...
141 runs ♦ 1 like
By Enhanced  
Created: 10 Jul 2018
Modified: 17 Sep 2023
Cells is a game about cells trying to infect each other. It offers a playground for minds to com...
CELLS WITH MIND ...
176 runs ♦ 0 likes
By Enhanced  
Created: 10 Jul 2018
Modified: 17 Sep 2023
Cells is a game about cells trying to infect each other. It offers a playground for minds to com...