English | 简体中文
You don't need to focus on implementation of Scratch3.0 functionality. Only use simple JavaScript language to call Scratch functions.
Implement API with minimal modify. It's can be easy to merge official latest version Scratch
The functionality encapsulates and provides the calling interface to the outside world without changing the Scratch code itself
The Teaching system is embed this project.
website:https://teaching.vip
source:http://github.com/open-scratch/teaching-open
install dependencies npm install
debug npm start
build for productioin npm run build:prod
After build, see index.html
and player.html
.
import lib.min.js
and chunks/gui.js
, scratch will render into<div id="scratch"></div>
。
Do not forget to edit window.scratchConfig
It's scratch-vm instance object, You can call scratch virtual machine API from outside.
Notice: This object need created before import lib.min.js
window.scratchConfig.logo
paramenter | description |
---|---|
show | is show log |
url | logo url |
handleClickShare | handle logo click event |
The support base64
window.scratchConfig.menuBar
paramenter | description |
---|---|
style | menu bar style |
button | description |
---|---|
newButton | new project button |
loadFileButton | load project from computer button |
saveFileButton | save project to computer button |
turboModeButton | turbo mode button |
helpButton | help button |
myStuff | my stuff button |
Button properties
paramenter | description |
---|---|
show | is show button |
handleBefore | handle before click button event, return 'true' to continue |
window.scratchConfig.menuBar.userAvatar
button | description |
---|---|
show | is show avatar |
username | user name |
avatar | user avatar |
handleClick | handle click event |
window.scratchConfig.menuBar.customButtons
Allow unlimit add button
Button properties:
paramenter | description |
---|---|
show | is show button |
buttonName | button label name |
style | button style |
handleClick | button click event |
Notict: Is recommended to configure in player mode
window.scratchConfig.stageArea
paramenter | description |
---|---|
scale | stage scale |
width | stage width |
height | stage height |
showControl | show stage area control bar |
fullscreenButton | fullscreen button config |
startButton | greenflag button config |
stopButton | stop button config |
window.scratchConfig.blocks
paramenter | description |
---|---|
scale | block scale |
hideCatagorys | hide block catagory (catagory codesee appendix) |
hideBlocks | hide block (block code see appendix) |
defaultProjectURL: "./static/project.sb3"
If you want to load default cat please remove this config
window.scratchConfig.assetCDN
window.scratchConfig.handleVmInitialized
paramenter | description |
---|---|
vm | scratch virtual machine |
example
window.scratchConfig.handleVmInitialized = (vm)=>{
window.vm = vm // export vm to window
}
window.scratchConfig.handleProjectLoaded
when create project or loaded project will call this function
window.scratchConfig.handleDefaultProjectLoaded
when default project loaded will call this function
window.scratch.loadPorject(url, callback)
You can also use vm.loadProject
window.scratch.loadPorject(url, ()=>{
console.log("load project finished")
})
window.scratch.getProjectFile(callback)
You can also use vm.saveProjectSb3
window.scratch.getProjectFile((file)=>{
console.log(file)
//upload this file to server
})
window.scratch.getProjectCover(callback)
window.scratch.getProjectCover((file)=>{
console.log(file)
//upload this file to server
})
window.scratch.getProjectName()
window.scratch.setProjectName(projectName)
window.scratch.setPlayerOnly(isPlayerOnly)
paramenter | description |
---|---|
isPlayerOnly | is player mode |
window.scratch.setPlayerOnly(true)
window.scratch.setFullScreen(isFullScreen)
paramenter | description |
---|---|
isFullScreen | is fullscreen mode |
window.scratch.setFullScreen(true)
window.scratchConfig = {
logo: {
show: true
, url: "./static/logo.png"
, handleClickLogo: () => {
}
},
menuBar: {
//menu bar style
style: {
background: 'hsla(215, 100%, 65%, 1)',
},
newButton:{
show: true,
handleBefore(){ //handle before click button event, return 'true' to continue
return true
}
},
//load project from computer button
loadFileButton:{
show: true,
handleBefore(){
return false
}
},
//save project to computer button
saveFileButton:{
show: true,
handleBefore(){
return true
}
},
//turbo mode button
turboModeButton:{
show: true
},
//help button
helpButton:{
show: true
},
//my stuff button
myStuff:{
show: true,
url: '/myProject'
},
//user avatar button
userAvatar:{
show: true,
username: 'Login',
avatar: './static/avatar.png',
handleClick(){
}
},
customButtons: [
{
show: true,
buttonName: 'Share',
style:{
color: 'white',
background: 'hsla(30, 100%, 55%, 1)',
},
handleClick:()=>{
console.log("custom button1 click");
window.scratch.getProjectCover(cover => {
//TODO upload
console.log(cover)
})
window.scratch.getProjectFile(file => {
//TODO upload
console.log(file)
})
var projectName = window.scratch.getProjectName()
console.log(projectName);
}
},
{
show: true,
buttonName: 'Button1',
style: {
color: 'white',
background: 'hsl(271deg 89% 70%)',
},
handleClick:()=>{
console.log("custom button click");
}
},
//You can add more button
]
},
blocks:{
scale: 0.8, // block scale
// to update workspace you need call window.vm.emitWorkspaceUpdate()
// hide block catagory (catagory codesee appendix)
hideCatagorys:[],
//hide block (block code see appendix)
hideBlocks:[],
},
stageArea:{ // Notict: Is recommended to configure in player mode
scale: 1, // stage scale
width: 480, // stage width
height: 360, // stage height
showControl: true, //show stage area control bar
fullscreenButton:{ //fullscreen button
show: true,
handleBeforeSetStageUnFull(){
return true
},
handleBeforeSetStageFull(){
return true
}
},
startButton:{ // greenflag button
show: true,
handleBeforeStart(){
return true
}
},
stopButton:{ // stop button
show: true,
handleBeforeStop(){
return true
}
}
},
handleVmInitialized: (vm) => {
window.vm = vm
console.log("VM initialized")
},
handleProjectLoaded:() => {
console.log("Project Loaded")
},
handleDefaultProjectLoaded:() => {
// window.scratch.loadProject("/project.sb3", () => {
// window.scratch.setProjectName("untitle")
// })
},
// If you want to load default cat please remove this config
defaultProjectURL: "./static/project.sb3",
assetCDN: './static'
}
motion looks sound events control sensing operators variables myBlocks
motion_movesteps motion_turnright motion_turnleft motion_goto motion_gotoxy motion_glideto motion_glidesecstoxy motion_pointindirection motion_pointtowards motion_changexby motion_setx motion_changeyby motion_sety motion_ifonedgebounce motion_setrotationstyle motion_xposition motion_yposition motion_direction
looks_sayforsecs looks_say looks_thinkforsecs looks_think looks_switchbackdropto looks_switchbackdroptoandwait looks_nextbackdrop looks_switchcostumeto looks_nextcostume looks_switchbackdropto looks_nextbackdrop looks_changesizeby looks_setsizeto looks_changeeffectby looks_seteffectto looks_cleargraphiceffects looks_show looks_hide looks_gotofrontback looks_goforwardbackwardlayers looks_backdropnumbername looks_costumenumbername looks_backdropnumbername looks_size
sound_playuntildone sound_play sound_stopallsounds sound_changeeffectby sound_seteffectto sound_cleareffects sound_changevolumeby sound_setvolumeto sound_volume
event_whenflagclicked event_whenkeypressed event_whenstageclicked event_whenthisspriteclicked event_whenbackdropswitchesto event_whengreaterthan event_whenbroadcastreceived event_broadcast event_broadcastandwait
control_wait control_repeat control_forever control_if control_if_else control_wait_until control_repeat_until control_stop control_create_clone_of control_start_as_clone control_create_clone_of control_delete_this_clone
sensing_touchingobject sensing_touchingcolor sensing_coloristouchingcolor sensing_distanceto sensing_askandwait sensing_answer sensing_keypressed sensing_mousedown sensing_mousex sensing_mousey sensing_setdragmode sensing_loudness sensing_timer sensing_resettimer sensing_of sensing_current sensing_dayssince2000 sensing_username
operator_add operator_subtract operator_multiply operator_divide operator_random operator_gt operator_lt operator_equals operator_and operator_or operator_not operator_join operator_letter_of operator_length operator_contains operator_mod operator_round operator_mathop
first import jQuery
function regKeyEvent(selector, key, keyCode) {
console.log("reg key event:" + key)
$(selector).on("touchstart", function(event) {
vm.postIOData("keyboard", {
keyCode: keyCode,
key: key,
isDown: true,
});
event.preventDefault();
});
$(selector).on("touchend", function() {
vm.postIOData("keyboard", {
keyCode: keyCode,
key: key,
isDown: false,
});
event.preventDefault();
});
}
regKeyEvent(".button_space", " ", 32)
regKeyEvent(".button_down", "ArrowDown", 40)
regKeyEvent(".button_up", "ArrowUp", 38)
regKeyEvent(".button_left", "ArrowLeft", 37)
regKeyEvent(".button_right", "ArrowRight", 39)
Sign in for post a comment
Comments ( 1 )