• Stars
    star
    1,165
  • Rank 38,710 (Top 0.8 %)
  • Language
    JavaScript
  • License
    BSD 3-Clause "New...
  • Created about 8 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Virtual Machine used to represent, run, and maintain the state of programs for Scratch 3.0

scratch-vm

Scratch VM is a library for representing, running, and maintaining the state of computer programs written using Scratch Blocks.

Build Status Coverage Status

Installation

This requires you to have Git and Node.js installed.

To install as a dependency for your own application:

npm install scratch-vm

To set up a development environment to edit scratch-vm yourself:

git clone https://github.com/LLK/scratch-vm.git
cd scratch-vm
npm install

Development Server

This requires Node.js to be installed.

For convenience, we've included a development server with the VM. This is sometimes useful when running in an environment that's loading remote resources (e.g., SVGs from the Scratch server). If you would like to use your modified VM with the full Scratch 3.0 GUI, follow the instructions to link the VM to the GUI.

Running the Development Server

Open a Command Prompt or Terminal in the repository and run:

npm start

Playground

To view the Playground, make sure the dev server's running and go to http://localhost:8073/playground/ - you will be directed to the playground, which demonstrates various tools and internal state.

VM Playground Screenshot

Standalone Build

npm run build
<script src="/path/to/dist/web/scratch-vm.js"></script>
<script>
    var vm = new window.VirtualMachine();
    // do things
</script>

How to include in a Node.js App

For an extended setup example, check out the /src/playground directory, which includes a fully running VM instance.

var VirtualMachine = require('scratch-vm');
var vm = new VirtualMachine();

// Block events
Scratch.workspace.addChangeListener(vm.blockListener);

// Run threads
vm.start();

Abstract Syntax Tree

Overview

The Virtual Machine constructs and maintains the state of an Abstract Syntax Tree (AST) by listening to events emitted by the scratch-blocks workspace via the blockListener. Each target (code-running object, for example, a sprite) keeps an AST for its blocks. At any time, the current state of an AST can be viewed by inspecting the vm.runtime.targets[...].blocks object.

Anatomy of a Block

The VM's block representation contains all the important information for execution and storage. Here's an example representing the "when key pressed" script on a workspace:

{
  "_blocks": {
    "Q]PK~yJ@BTV8Y~FfISeo": {
      "id": "Q]PK~yJ@BTV8Y~FfISeo",
      "opcode": "event_whenkeypressed",
      "inputs": {
      },
      "fields": {
        "KEY_OPTION": {
          "name": "KEY_OPTION",
          "value": "space"
        }
      },
      "next": null,
      "topLevel": true,
      "parent": null,
      "shadow": false,
      "x": -69.333333333333,
      "y": 174
    }
  },
  "_scripts": [
    "Q]PK~yJ@BTV8Y~FfISeo"
  ]
}

Testing

npm test
npm run coverage

Publishing to GitHub Pages

npm run deploy

This will push the currently built playground to the gh-pages branch of the currently tracked remote. If you would like to change where to push to, add a repo url argument:

npm run deploy -- -r <your repo url>

Donate

We provide Scratch free of charge, and want to keep it that way! Please consider making a donation to support our continued engineering, design, community, and resource development efforts. Donations of any size are appreciated. Thank you!

More Repositories

1

scratch-gui

Graphical User Interface for creating and running Scratch 3.0 projects.
JavaScript
4,239
star
2

scratch-blocks

Scratch Blocks is a library for building creative computing interfaces.
JavaScript
2,534
star
3

scratch-www

Standalone web client for Scratch
JavaScript
1,507
star
4

scratch-flash

Open source version of the Scratch 2.0 project editor. This is the basis for the online and offline versions of Scratch found on the website.
ActionScript
1,326
star
5

scratchjr

With ScratchJr, young children (ages 5-7) can program their own interactive stories and games.
JavaScript
662
star
6

scratch-html5

HTML 5 based Scratch project player
JavaScript
390
star
7

scratch-desktop

Scratch 3.0 as a self-contained desktop application
JavaScript
270
star
8

scratch-render

WebGL-based rendering engine for Scratch 3.0
JavaScript
254
star
9

scratchx

Scratch Extension Site
JavaScript
190
star
10

scratch-paint

Paint editor for Scratch 3.0
JavaScript
165
star
11

Scratch_1.4

Source Code repository for Scratch 1.4
C
107
star
12

scratch-link

Device interoperability layer for Windows and MacOS
C#
89
star
13

scratch-l10n

JavaScript
62
star
14

scratch-parser

Validation and parsing for Scratch projects
JavaScript
57
star
15

scratch-audio

Web Audio-based audio engine for Scratch 3.0
JavaScript
54
star
16

scratch-storage

Load and store project and asset files for Scratch 3.0
JavaScript
54
star
17

scratch-rest-api

Documentation for how to interact with the Scratch REST API
53
star
18

scratchjr-website

Code for the Scratch Jr Website
JavaScript
46
star
19

scratch-svg-renderer

Scratch SVG renderer
JavaScript
31
star
20

scratch-analysis

Analysis tool for summarizing the structure, composition, and complexity of Scratch programs.
JavaScript
24
star
21

scratch-extension-docs

[Deprecated] Sample code for Scratch 2.0 hardware and web extensions.
22
star
22

scratchr2_translations

Translations for the Scratch 2.0 website
16
star
23

scratchwikiskin

Skin for the Scratch wiki
PHP
15
star
24

scratch-resources

Source files and images for translators to translate Scratch materials
JavaScript
14
star
25

scratch-render-fonts

Fonts for Scratch SVG rendering
JavaScript
13
star
26

po2icu

An npm package for converting gettext to icu.
JavaScript
11
star
27

scratch-sb1-converter

Convert Scratch .sb files (for Scratch 1) to Scratch 2 (.sb2) in JS.
JavaScript
11
star
28

scratch-auth

Authentication utilities for Scratch
JavaScript
11
star
29

eslint-config-scratch

Shareable ESLint config for Scratch
JavaScript
8
star
30

scratch-docker

Scratch docker infrastructure npm module
Shell
7
star
31

scratch2-project-analyzer

Tools for analyzing Scratch 2.0 projects
Python
6
star
32

scratch-asset-types

Supported asset detection and types for Scratch backend
JavaScript
6
star
33

django-request-provider

Python
5
star
34

scratchx-example

Examples for working with Scratch and ScratchX extensions
JavaScript
5
star
35

swiki-navbar

A small extension for the Scratch Wiki that creates a limited navigation bar for users who are not logged in
PHP
4
star
36

scratchx-crossdomain

Standard crossdomain.xml file for use with ScratchX
4
star
37

.github

Scratch community health files
2
star
38

scratch-renovate-config

Scratch's shared configuration for Renovate
1
star