Jens Mönig, Bernat Romagosa, September 6, 2024
This document describes how Snap! can be accessed from an outside program to start scripts, send and retrieve information. The model use case is embedding interactive Snap! projects in other websites such as MOOCs or other adaptive learning platforms.
This experimental Snap! API is a set of methods for an IDE_Morph containing a Snap! project. These methods are maintained to work with future versions of Snap! They can be used to trigger scripts, get feedback from running scripts, and access the project's global variables.
Currently the API consists of the following methods:
IDE_Morph.prototype.getScenes()
IDE_Morph.prototype.getCurrentScene()
IDE_Morph.prototype.switchTo()
IDE_Morph.prototype.isRunning()
IDE_Morph.prototype.stop()
IDE_Morph.prototype.broadcast()
IDE_Morph.prototype.addMessageListenerForAll()
IDE_Morph.prototype.addMessageListener()
IDE_Morph.prototype.getMessages()
IDE_Morph.prototype.getVarNames()
IDE_Morph.prototype.getVar()
IDE_Morph.prototype.setVar()
IDE_Morph.prototype.newList()
IDE_Morph.prototype.getProjectXML()
IDE_Morph.prototype.loadProjectXML()
IDE_Morph.prototype.unsavedChanges()
IDE_Morph.prototype.resetUnsavedChanges()
IDE_Morph.prototype.getSpriteScriptsXML()
IDE_Morph.prototype.loadSpriteScriptsXML()
IDE_Morph.prototype.flashSpriteScripts()
IDE_Morph.prototype.flashSpriteScriptAt()
IDE_Morph.prototype.unflashSpriteScripts()
IDE_Morph.prototype.flashSpriteScriptOutlineAt()
IDE_Morph.prototype.unflashSpriteScriptsOutline()
IDE_Morph.prototype.showScriptBalloonAt()
IDE_Morph.prototype.closePopUps()
IDE_Morph.prototype.setTranslation()
There are two ways in which Snap! can be used as an extension editor for other web applications: Either by directly embedding the Snap! IDE as a Canvas element in another web page, or by embedding Snap! in an iframe.
Embedding Snap! directly into another web page involves loading all the source scripts, setting up a Canvas for the IDE, configuring the desired looks and behavior of the Snap! editor and starting an animation loop to bring it to life.
A page that embeds its own Snap! editor might be structured like this:
<!DOCTYPE html>
<html>
<head>
<title>Embedded Snap!</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="https://snap.berkeley.edu/snap/src/morphic.js"></script>
<script ... ></script>
...
<script>
var world;
window.onload = function () {
var ide = new IDE_Morph({ /* configurations dictionary ...*/ }),
loop = () => {
requestAnimationFrame(loop);
world.doOneCycle();
};
world = new WorldMorph(document.getElementById('world'), false); // don't fill
ide.openIn(world);
requestAnimationFrame(loop);
};
</script>
</head>
<body>
<canvas id="world" tabindex="1" width="500" height="300"></canvas>
</body>
</html>
Note that in this setup you are explicitly creating an instance of the IDE and can simply assign it to a variable for further communication. Also note that when instantiating a new WorldMorph you can specify a flag indicating whether it is to take over all the available browser real-eastate or not.
You can configure the looks and behavior of the IDE by passing it a configuration dictionary object. Currently the following preferences are supported:
keyword | type | setting |
---|---|---|
noAutoFill | bool | do not let the IDE fill the whole World canvas |
path: | str | path to additional resources (translations) |
load: | str | microworld file name (xml) |
onload: | callback | called when the microworld is loaded |
design: | str | currently "flat" or "classic" (skeuomorphic) |
theme: | str | currently "bright" or "dark" |
border: | num | pixels surrounding the IDE, default is none (zero) |
lang: | str | translation to be used, e.g. "de" for German |
mode: | str | currently "presentation" or "edit" |
hideControls: | bool | hide/show the tool bar |
hideCategories: | bool | hide/show the palette block category buttons |
hideProjectName: | bool | hide/show the project title in the tool bar |
hideProjects: | bool | hide/show the projects menu button in the tool bar |
hideSettings: | bool | hide/show the settings menu button in the tool bar |
noProjectItems: | bool | hide/show project specific menu items in the file/project menu |
noDefaultCat: | bool | hide/show the default built-in category buttons |
noSpriteEdits: | bool | hide/show the corral & sprite controls/menus |
noSprites: | bool | hide/show the stage, corral, sprite editor |
noPalette: | bool | hide/show the palette including the categories |
noImports: | bool | disable/allow importing files via drag&drop, hides the project menu button |
noCloud: | bool | disable/enable functionalities to access the Snap! cloud |
noOwnBlocks: | bool | hide/show "make a block" and "make a category" buttons |
noRingify: | bool | disable/enable "ringify" / "unringify" in context menus |
noUserSettings: | bool | disable/enable persistent user preferences |
noDevWarning: | bool | ignore development version incompatibility warning |
noExitWarning: | bool | do not show a browser warning when closing the IDE with unsaved changes |
preserveTitle: | bool | do not set the tab title dynamically to reflect the current Snap! version |
blocksZoom: | num | zoom factor for blocks, e.g. 1.5 |
blocksFade: | num | fading percentage for blocks, e.g. 85 |
zebra: | num | contrast percentage for nesting same-color blocks |
Note that such configurations will not affect the user's own preference settings, e.g. configuring the blocks zoom or language will not overwrite the user's own settings which are kept in localstorage.
Getting hold of an ide can usually be achieved by evaluating:
var ide = world.children[0];
The model case in mind is embedding Snap! in an iframe following a pattern such as this example:
<!DOCTYPE html>
<html>
<head>
<title>Snap! iFrame</title>
</head>
<body>
<iframe id="inlineFrameExample"
title="Inline Frame Example"
width="1024"
height="720"
src="snap.html">
</iframe>
</body>
</html>
In such a set up the ide can be accessed through the contentWindow
property, e.g.
var ide = document.getElementsByTagName("iframe")[0].contentWindow.world.children[0];
If the iframe and the container do not share domains, you won't be able to reach the world
and, thus, the API. For that particular case, you should use the postMessage
mechanism,
as follows:
document.querySelector('iframe').contentWindow.postMessage(
{ selector: <API selector>, params: <param array> },
'*'
);
For instance, to get the value of a variable named "foo", you would do:
document.querySelector('iframe').contentWindow.postMessage(
{ selector: 'getVar', params: [ 'foo' ] },
'*'
);
The way to capture the return values of these messages from the page containing the iframe
is to define an onmessage
listener:
winndow.addEventListener('message',function(e) {
console.log('the response to', e.data.selector, 'is', e.data.response);
},false);
Note that e.data.selector
carries the original selector back, so you can tie it to the
request, while e.data.response
carries the return value of the API method call.
The getScenes() method returns an array with the names of all scenes in the projects. The minimum number of elements is 1, since there is always at least one scene per project. The scene names are unique strings within the array. Note that the empty string ('') is a valid scene identifier.
ide.getScenes();
an Array of Strings, minimum length 1
The getCurrentScene() method returns a string representing the name of the currently active scene in the project. If the scene is unnamed and empty string is returned.
ide.getCurrentScene();
a String, can be an empty String
The switchTo() method displays the specified scene. It suspends all processes and clones of the previously active scene and passes control to the new scene.
ide.switchTo(sceneName);
- sceneName
- string, the name of the scene to be activated
undefined
The isRunning() method returns true
if the active scene is currently running one or more threads, false
if the scene is idle.
ide.isRunning();
a Boolean
The stop() method immediately terminates all currently running threads in the active scene and removes all temporary clones. It does not trigger a "When I am stopped" event.
ide.stop();
undefined
The broadcast() method triggers all scripts whose hat block listens to the specified message. An optional callback can be added to be run after all triggered scripts have terminated.
ide.broadcast(message [, callback]);
- message
- string, the message to be sent to all listeners
- callback | optional
- function to execute after all scripts terminate, no arguments
undefined
The addMessageListenerForAll() method sets up a function that will be called whenever a message is broadcast. The function takes one argument, the message being broadcast, and can be used to react to any message. Multiple message listeners can be set up, they all get executed in the order in which they were added.
ide.addMessageListenerForAll(callback);
- callback
- function to execute whenever a message is sent, takes one argument: The message string
undefined
The addMessageListener() method sets up a function that will be called whenever the specified message is broadcast. Multiple message listeners can be set up per message, they all the executed in the order in which they were added.
ide.addMessageListener(message, callback);
- message
- string, the message to which the listener will react. If the message is an empty string the callback will be executed at any broadcast, passing the message as argument
- callback
- function to execute whenever the specified message is sent, takes no argument, except when the message to listen to is the empty string, then it takes the message as argument
undefined
The getMessage() method returns a new Array that contains all the message strings that occur in the project, both in hat blocks and in broadcast blocks.
ide.getMessages();
an Array of Strings, or an empty Array
The getVarNames() method returns a new Array that contains all the global variable names in the project.
ide.getVarNames();
an Array of Strings, or an empty Array
The getVar() method returns the value of the global variable indicated by the specified name.
ide.getVar(name);
whatever value the variable holds.
The setVar() methods assigns a value to the a global variable specified by name.
ide.setVar(name, value);
undefined
The newList() methods returns a new Snap! list. Optionally a source array containing the list elements can be specified.
ide.newList([array]);
a new Snap! List
the getProjectXML() method returns a string in XML format representing the serialized project currently loaded into the IDE.
ide.getProjectXML();
an XML String
the loadProjectXML() method replaces the current project of the IDE with another serialized one encoded in a string in XML format. Note that no user acknowledgement is required, all unsaved edits to the prior project are lost.
ide.loadProjectXML(projectData);
- projectData
- XML string representing a serialized project
undefined
the getSpriteScriptsXML() method returns a string in XML format representing the serialized scripts of the sprite identified by name or the currently edited sprite stripped of all dependenies, i.e. without custom block definitions or data (variables)
ide.getSpriteScriptsXML([spriteName]);
- spriteName
- name of sprite or stage whose scripts to fetch, or none, in which case the currently edited object will be taken
an XML String
the loadSpriteScriptsXML() method replaces the scripts of the specified sprite or stage with a set of serialized ones encoded in a string in XML format, no questions asked. Note: No dependency handling is expected, i.e. the xml-String is meant to be stripped of all dependenies, i.e. without custom block definitions or data (variables)
loadSpriteScriptsXML(scriptsXML);
- scriptsXML
- XML string representing a set of serialized scripts stripped of their dependencies
an XML String
the flashSpriteScripts() method highlights the blocks of the scripts of the sprite indicated by name - or the current sprite or stage if none - that correspond to the portion of the text between the start- and end lines when using the current codification mapping
flashSpriteScripts(fromLOC, toLOC[, spriteName[, colorCSV]]);
- fromLOC
- integer representing the first line of mapped code to be signaled, starting at 1
- toLOC
- integer representing the last line of mapped code to be signaled
- spriteName
- name of sprite or stage whose scripts to fetch, or none, in which case the currently edited object will be taken
- colorCSV
- string with comma-separated integer values representing a color in the form "r,g,b[,a]", or none, in which case the default highlight color will be used. Color components are numbers between 0 and 255, alpha a fraction between 0 and 1.
undefined
the flashSpriteScriptAt() method highlights the innermost block of the scripts of the sprite indicated by name - or the current sprite or stage if none - that corresponds to the position of the given character index when using the current codification mapping
flashSpriteScriptAt(charIdx[, spriteName[, colorCSV]]);
- charIdx
- integer representing the character index of mapped code to be signaled, starting at 0
- spriteName
- name of sprite or stage whose scripts to fetch, or none, in which case the currently edited object will be taken
- colorCSV
- string with comma-separated integer values representing a color in the form "r,g,b[,a]", or none, in which case the default highlight color will be used. Color components are numbers between 0 and 255, alpha a fraction between 0 and 1.
undefined
the unflashSpriteScripts() method un-highlights the blocks of the scripts of the sprite indicated by name - or the current sprite or stage if none -
unflashSpriteScripts([spriteName]);
- spriteName
- name of sprite or stage whose scripts to fetch, or none, in which case the currently edited object will be taken
undefined
the flashSpriteScriptOutlineAt() method highlights the outline of the innermost block of the scripts of the sprite indicated by name - or the current sprite or stage if none - that corresponds to the position of the given character index when using the current codification mapping
flashSpriteScriptOutlineAt(charIdx[, spriteName[, colorCSV[, border]]]);
- charIdx
- integer representing the character index of mapped code to be signaled, starting at 0
- spriteName
- name of sprite or stage whose scripts to fetch, or none, in which case the currently edited object will be taken
- colorCSV
- string with comma-separated integer values representing a color in the form "r,g,b[,a]", or none, in which case the default highlight color will be used. Color components are numbers between 0 and 255, alpha a fraction between 0 and 1.
- border
- integer representing the pixel width of the outline highlight
undefined
the unflashSpriteScriptsOutline() method un-highlights the blocks outlines of the scripts of the sprite indicated by name - or the current sprite or stage if none -
unflashSpriteScriptsOutline([spriteName]);
- spriteName
- name of sprite or stage whose scripts to fetch, or none, in which case the currently edited object will be taken
undefined
the showScriptBalloonAt() method highlights the outline of the innermost block of the scripts of the sprite indicated by name - or the current sprite or stage if none - that corresponds to the position of the given character index when using the current codification mapping
showScriptBalloonAt(contents, charIdx[, spriteName]);
- contents
- data to be displayed inside the speech balloon, can be a string, number, costume, morph, canvas, list, table etc. - anything first-class in Snap!
- charIdx
- integer representing the character index of mapped code to be signaled, starting at 0
- spriteName
- name of sprite or stage whose scripts to fetch, or none, in which case the currently edited object will be taken
undefined
the closePopUps() method removes all pop-up menus and speech balloon, if any
closePopUps();
undefined
the unsavedChanges() method returns a Boolean value indicating whether the currently edited project has been modifed since it was last saved.
ide.unsavedChanges();
a Boolean
the resetUnsavedChanges() method resets the value returned by unsavedChanges() to false.
ide.resetUnsavedChanges();
undefined
the setTranslation() method switches to the specified language, formatted as ISO 639-1 code, and optionally runs a callback afterwards, e.g. to broadcast an event. Note that switching to another translation involves serializing and deserializing the current project and thus stops all running processes. If you wish to "continue" a project afterwards you can use the callback to trigger an event, such as the green flag ('__shout__go__'). Also note that the language setting does not overwrite the user's own setting which is stored in the browser this way, so that the next time the user opens Snap their own language preference again takes effect.
ide.setTranslation(countryCode [, callback]);
- countryCode
- string representing a country in ISO 639-1 format
- callback | optional
- function to execute after the language has been set, no arguments
undefined
Snap! lists can be accessed and manipulated through a set of methods described in the file lists.js