API: Canvas (r2)

API Uses canvas Graphics libraries AB framework Worlds using this API Starter Worlds
Canvas (r2) Yes None No 10 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 or with any Ancient Brain framework. Anything written in JS for a canvas should run using this API without change.

This API has the following features:



Templates


ABWorld

This API defines a global variable called "ABWorld" with not much in it:
 

 ABWorld;                 // instance of class ABWorldClass  
 

 ABWorld.canvas;          // a variable to point to the canvas   		
 
 // When making a screenshot, Ancient Brain will search for the canvas.
 // If it cannot find the canvas, set this variable to point to it.
 
 
 ABWorld.fullwidth();     // return full width of window on desktop / screen on mobile 
 ABWorld.fullheight();    // return full height of window on desktop / screen on mobile


getContext: preserveDrawingBuffer

The following is important if you are creating a "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.
You need something like:

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

Examples

Examples of Worlds that use this API:


Plain canvas web...
95 runs ♦ 0 likes
By Starter user  
Created: 1 Sep 2018
Modified: 13 Nov 2023
Example of porting webgl World (almost) unchanged to AB using plain canvas API
Plain canvas web...
76 runs ♦ 0 likes
By Starter user  
Created: 1 Sep 2018
Modified: 17 Sep 2023
Example of porting webgl World (almost) unchanged to AB using plain canvas API