@@ -9,7 +9,15 @@ function initWasm() {
99 wasm = obj . instance
1010 go . run ( wasm )
1111 } )
12- go . importObject . env = { 'main.jsPrint' : jsPrint }
12+ go . importObject . env = {
13+ 'main.jsPrint' : jsPrint ,
14+ 'main.move' : move ,
15+ 'main.line' : line ,
16+ 'main.width' : width ,
17+ 'main.circle' : circle ,
18+ 'main.rect' : rect ,
19+ 'main.color' : color ,
20+ }
1321 document . querySelectorAll ( 'header button' ) . forEach ( ( button ) => {
1422 button . onclick = handleRun
1523 button . disabled = false
@@ -18,17 +26,22 @@ function initWasm() {
1826}
1927
2028// jsPrint converts wasm memory bytes from ptr to ptr+len to string and
21- // writes it the output pane .
29+ // writes it to the output textarea .
2230function jsPrint ( ptr , len ) {
23- const buf = new Uint8Array ( wasm . exports . memory . buffer , ptr , len )
24- const str = new TextDecoder ( 'utf8' ) . decode ( buf )
31+ const s = memString ( ptr , len )
2532 const output = document . getElementById ( 'output' )
26- output . textContent += str
27- if ( str . toLowerCase ( ) . includes ( 'confetti' ) ) {
33+ output . textContent += s
34+ if ( s . toLowerCase ( ) . includes ( 'confetti' ) ) {
2835 showConfetti ( )
2936 }
3037}
3138
39+ function memString ( ptr , len ) {
40+ const buf = new Uint8Array ( wasm . exports . memory . buffer , ptr , len )
41+ const s = new TextDecoder ( 'utf8' ) . decode ( buf )
42+ return s
43+ }
44+
3245// handleRun retrieves the input string from the code pane and
3346// converts it to wasm memory bytes. It then calls the evy evaluate
3447// function.
@@ -39,6 +52,7 @@ function handleRun(event) {
3952 const mem = new Uint8Array ( wasm . exports . memory . buffer , ptr , bytes . length )
4053 mem . set ( new Uint8Array ( bytes ) )
4154 document . getElementById ( 'output' ) . textContent = ''
55+ resetCanvas ( )
4256 const fn = wasm . exports [ event . target . id ] // evaluate, tokenize or parse
4357 fn ( ptr , bytes . length )
4458}
@@ -66,7 +80,7 @@ function showConfetti() {
6680 . sort ( ( a , b ) => a . r - b . r )
6781
6882 const cssText = ( c ) =>
69- `background: ${ c . color } ;left: ${ c . x } %; top: ${ c . y } %; transform: scale(${ c . r } )`
83+ `background: ${ c . color } ; left: ${ c . x } %; top: ${ c . y } %; transform: scale(${ c . r } )`
7084 const confettiDivs = confetti . map ( ( c ) => {
7185 const div = document . createElement ( 'div' )
7286 div . style . cssText = cssText ( c )
@@ -98,3 +112,100 @@ function showConfetti() {
98112 confettiDivs . forEach ( ( div ) => div . classList . add ( 'fadeout' ) )
99113 } , 8500 )
100114}
115+
116+ // graphics
117+
118+ const canvas = {
119+ x :0 ,
120+ y :0 ,
121+ ctx : null ,
122+ scale : { x : 10 , y : - 10 } ,
123+ width : 100 ,
124+ height :100 ,
125+
126+ offset : { x : 0 , y :- 100 } , // height
127+ }
128+
129+ function scaleX ( x ) {
130+ return canvas . scale . x * x
131+ }
132+
133+ function scaleY ( y ) {
134+ return canvas . scale . y * y
135+ }
136+
137+ function transformX ( x ) {
138+ return scaleX ( x + canvas . offset . x )
139+ }
140+
141+ function transformY ( y ) {
142+ return scaleY ( y + canvas . offset . y )
143+ }
144+
145+ function move ( x , y ) {
146+ canvas . x = transformX ( x )
147+ canvas . y = transformY ( y )
148+ }
149+
150+ function line ( x2 , y2 ) {
151+ console . log ( "line x:" , x2 , "y:" , y2 )
152+ const { ctx, x, y} = canvas
153+ ctx . beginPath ( )
154+ ctx . moveTo ( x , y )
155+ ctx . lineTo ( transformX ( x2 ) , transformY ( y2 ) )
156+ ctx . stroke ( ) ;
157+ move ( x2 , y2 )
158+ }
159+
160+ function color ( ptr , len ) {
161+ const s = memString ( ptr , len )
162+ console . log ( "color:" , s )
163+ canvas . ctx . fillStyle = s ;
164+ canvas . ctx . strokeStyle = s ;
165+ }
166+
167+ function width ( n ) {
168+ console . log ( "lineWidth:" , n )
169+ canvas . ctx . lineWidth = scaleX ( n ) ;
170+ }
171+
172+ function rect ( dx , dy ) {
173+ console . log ( "rect dx:" , dx , "dy:" , dy )
174+ const { ctx, x, y} = canvas
175+ const sDX = scaleX ( dx )
176+ const sDY = scaleY ( dy )
177+ canvas . ctx . fillRect ( x , y , sDX , sDY )
178+ canvas . x = x + sDX
179+ canvas . y = y + sDY
180+ }
181+
182+ function circle ( r ) {
183+ console . log ( "circle r:" , r )
184+ const { x, y, ctx } = canvas
185+ ctx . beginPath ( )
186+ ctx . arc ( x , y , scaleX ( r ) , 0 , Math . PI * 2 , true )
187+ ctx . fill ( )
188+ }
189+
190+ function initCanvas ( ) {
191+ const c = document . getElementById ( "canvas" )
192+ const b = c . parentElement . getBoundingClientRect ( )
193+ const side = b . width < b . height ? b . width : b . height
194+ c . width = Math . abs ( scaleX ( canvas . width ) )
195+ c . height = Math . abs ( scaleY ( canvas . height ) )
196+ console . log ( "width" , c . width , "height" , c . height )
197+ c . style . width = `${ side } px`
198+ c . style . display = 'block'
199+ canvas . ctx = c . getContext ( "2d" )
200+ }
201+
202+ function resetCanvas ( ) {
203+ const ctx = canvas . ctx
204+ ctx . clearRect ( 0 , 0 , ctx . canvas . width , ctx . canvas . height )
205+ ctx . fillStyle = "black"
206+ ctx . strokeStyle = "black"
207+ ctx . lineWidth = 1
208+ move ( 0 , 0 )
209+ }
210+
211+ initCanvas ( )
0 commit comments