• Stars
    star
    165
  • Rank 228,906 (Top 5 %)
  • Language
    JavaScript
  • License
    BSD 3-Clause "New...
  • Created over 7 years ago
  • Updated almost 2 years ago

Reviews

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

Repository Details

Paint editor for Scratch 3.0

scratch-paint

Scratch-paint provides a paint editor React component that takes and outputs SVGs or PNGs. It can convert between vector and bitmap modes.

Greenkeeper badge

Installation

It will be easiest if you develop on Mac or Linux. If you are using Windows, I recommend using Ubuntu on Windows, which will allow you to use Linux commands on Windows. You will need administrator permissions.

Scratch Paint requires you to have Git and Node.js installed. E.g.:

- sudo apt-get update
- sudo apt-get install git-core
- sudo apt-get install nodejs

For Ubuntu on Windows, the Windows install of nodejs may interfere with the Linux one, so installing nodejs requires more steps:

- curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
- sudo apt-get install -y nodejs
- PATH="/usr/bin:$PATH"

If you want to edit scratch-paint, or help contribute to our open-source project, fork the scratch-paint repo. Then:

git clone https://github.com/<YOUR_GITHUB_USERNAME>/scratch-paint.git
cd scratch-paint
npm install

Running locally (stand-alone)

You can try out your own copy of the paint editor by running the development server.

In the cloned scratch-paint directory, run:

npm run build
npm start

Then go to http://localhost:8078/playground/. 8078 is BLOB upside-down. The True Name of this repo is scratch-blobs.

(Note that the npm run build step above seems like it's only necessary for some user and environments, and not others; check for yourself if the server that npm start starts is hot-reloading correctly.)

Running locally (as part of Scratch)

So you've tried out your edits in the playground and they look good. You should now test with the rest of Scratch, to make sure that everything hooks up right, and so that you can use your custom paint editor to make costumes and sprites!

Get the rest of Scratch:

git clone https://github.com/LLK/scratch-gui.git

Go to your scratch-paint folder and run:

npm link

Now in another terminal, go back to the scratch-gui folder and run

npm install
npm link scratch-paint
npm start

Then go to http://localhost:8601. 601 is supposed to look like GUI (it's okay, I don't really see it either.) The Costumes tab should be running your local copy of scratch-paint!

How to include in your own Node.js App

If you want to use scratch-paint in your own Node environment/application, add it with:

npm install --save scratch-paint

For an example of how to use scratch-paint as a library, check out the scratch-paint/src/playground directory.

In your parent component:

import PaintEditor from 'scratch-paint';
...
<PaintEditor
    image={optionalImage}
    imageId={optionalId}
    imageFormat='svg'
    rotationCenterX={optionalCenterPointX}
    rotationCenterY={optionalCenterPointY}
    rtl={true|false}
    onUpdateImage={handleUpdateImageFunction}
    zoomLevelId={optionalZoomLevelId}
/>

image: may either be nothing, an SVG string or a base64 data URI) SVGs of up to size 480 x 360 will fit into the view window of the paint editor, while bitmaps of size up to 960 x 720 will fit into the paint editor. One unit of an SVG will appear twice as tall and wide as one unit of a bitmap. This quirky import behavior comes from needing to support legacy projects in Scratch.

imageId: If this parameter changes, then the paint editor will be cleared, the undo stack reset, and the image re-imported.

imageFormat: 'svg', 'png', or 'jpg'. Other formats are currently not supported.

rotationCenterX: x coordinate relative to the top left corner of the sprite of the point that should be centered. If left undefined, image will be horizontally centered.

rotationCenterY: y coordinate relative to the top left corner of the sprite of the point that should be centered. If left undefined, image will be vertcally centered.

rtl: True if the paint editor should be laid out right to left (meant for right to left languages)

onUpdateImage: A handler called with the new image (either an SVG string or an ImageData) each time the drawing is edited.

zoomLevelId: All costumes with the same zoom level ID will share the same saved zoom level. When a new zoom level ID is encountered, the paint editor will zoom to fit the current costume comfortably. Leave undefined to perform no zoom to fit.

In the top-level combineReducers function:

import {ScratchPaintReducer} from 'scratch-paint';
...
combineReducers({
	...
    scratchPaint: ScratchPaintReducer
});

Note that scratch-paint expects its state to be in state.scratchPaint, so the name must be exact.

Scratch-paint shares state with its parent component because it expects to share the parent's IntlProvider, which inserts translations into the state. See the IntlProvider setup in scratch-gui here.

Code organization

We use React and Redux. If you're just getting started with them, here are some good tutorials: https://egghead.io/courses/getting-started-with-redux

  • Under /src, our React/Redux code is divided mainly between components (presentational components), containers (container components), and reducers.

  • css contains only shared css. Most of the css is stored alongside its component.

  • helper contains pure javascript used by the containers. If you want to change how something works, it's probably here. For instance, the brush tool is in helper/blob-tools/, and the code that's run when you click the group button is in helper/group.js.

Testing

npm run test

Just unit tests:

npm run unit

An individual unit test: (run from scratch-paint directory)

./node_modules/.bin/jest ./test/unit/undo-reducer.test.js

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!

Scratch-paint couldn't exist without w00dn/papergrapher and Paper.js. If you are amazed and/or baffled by the insane boolean operation math that makes the brush and eraser tools possible, please check out and consider contributing to Paper. 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,594
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

scratch-vm

Virtual Machine used to represent, run, and maintain the state of programs for Scratch 3.0
JavaScript
1,199
star
6

scratchjr

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

scratch-html5

HTML 5 based Scratch project player
JavaScript
390
star
8

scratch-desktop

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

scratch-render

WebGL-based rendering engine for Scratch 3.0
JavaScript
260
star
10

scratchx

Scratch Extension Site
JavaScript
190
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
63
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

scratch-resources

Source files and images for translators to translate Scratch materials
JavaScript
15
star
24

scratchwikiskin

Skin for the Scratch wiki
PHP
15
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-auth

Authentication utilities for Scratch
JavaScript
11
star
28

scratch-sb1-converter

Convert Scratch .sb files (for Scratch 1) to Scratch 2 (.sb2) in JS.
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