API: Canvas (AB) (r2)

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


canvas 2d
359 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...
canvas webgl
536 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")....