1+ class Extension {
2+ constructor ( ) {
3+ this . canvas = document . createElement ( "canvas" ) ;
4+ this . ctx = this . canvas . getContext ( "2d" ) ;
5+
6+ this . canvas . width = 480 ;
7+ this . canvas . height = 360 ;
8+
9+ this . seeds = [ ] ;
10+ this . enabled = false ;
11+ }
12+
13+ getInfo ( ) {
14+ return {
15+ id : "blackMold" ,
16+ name : "Black Mold" ,
17+ color1 : "#111111" ,
18+ color2 : "#000000" ,
19+ blocks : [
20+ {
21+ opcode : "start" ,
22+ blockType : Scratch . BlockType . COMMAND ,
23+ text : "start mold"
24+ } ,
25+ {
26+ opcode : "stop" ,
27+ blockType : Scratch . BlockType . COMMAND ,
28+ text : "stop mold"
29+ } ,
30+ {
31+ opcode : "clear" ,
32+ blockType : Scratch . BlockType . COMMAND ,
33+ text : "clear mold"
34+ } ,
35+ {
36+ opcode : "tick" ,
37+ blockType : Scratch . BlockType . COMMAND ,
38+ text : "grow mold"
39+ }
40+ ]
41+ } ;
42+ }
43+
44+ start ( ) {
45+ this . enabled = true ;
46+
47+ if ( this . seeds . length === 0 ) {
48+ this . seeds . push ( {
49+ x : Math . random ( ) * 480 ,
50+ y : Math . random ( ) * 360 ,
51+ r : 5
52+ } ) ;
53+ }
54+
55+ if ( ! this . overlay ) {
56+ this . overlay = document . createElement ( "canvas" ) ;
57+ this . overlay . width = 480 ;
58+ this . overlay . height = 360 ;
59+ this . overlay . style . position = "absolute" ;
60+ this . overlay . style . pointerEvents = "none" ;
61+ this . overlay . style . left = "0" ;
62+ this . overlay . style . top = "0" ;
63+ this . overlay . style . width = "100%" ;
64+ this . overlay . style . height = "100%" ;
65+ this . overlay . style . zIndex = "9999" ;
66+
67+ this . octx = this . overlay . getContext ( "2d" ) ;
68+
69+ const stage = document . querySelector ( "canvas" ) ;
70+ stage . parentElement . appendChild ( this . overlay ) ;
71+ }
72+ }
73+
74+ stop ( ) {
75+ this . enabled = false ;
76+ }
77+
78+ clear ( ) {
79+ this . seeds = [ ] ;
80+ if ( this . octx ) {
81+ this . octx . clearRect ( 0 , 0 , 480 , 360 ) ;
82+ }
83+ }
84+
85+ tick ( ) {
86+ if ( ! this . enabled ) return ;
87+
88+ const ctx = this . octx ;
89+
90+ // Grow existing blobs
91+ for ( const blob of this . seeds ) {
92+
93+ blob . r += Math . random ( ) * 0.5 ;
94+
95+ ctx . beginPath ( ) ;
96+ ctx . arc (
97+ blob . x + ( Math . random ( ) - 0.5 ) * blob . r * 0.3 ,
98+ blob . y + ( Math . random ( ) - 0.5 ) * blob . r * 0.3 ,
99+ blob . r ,
100+ 0 ,
101+ Math . PI * 2
102+ ) ;
103+ ctx . fillStyle = "#000" ;
104+ ctx . fill ( ) ;
105+
106+ // Chance to branch
107+ if ( Math . random ( ) < 0.04 ) {
108+ this . seeds . push ( {
109+ x : blob . x + ( Math . random ( ) - 0.5 ) * blob . r * 4 ,
110+ y : blob . y + ( Math . random ( ) - 0.5 ) * blob . r * 4 ,
111+ r : 2
112+ } ) ;
113+ }
114+ }
115+ }
116+ }
117+
118+ Scratch . extensions . register ( new Extension ( ) ) ;
0 commit comments