Skip to content

Commit 860e6c8

Browse files
authored
🫧 evy: Add clear to drawing functions (#122)
Add `clear` to drawing functions as a builtin because it is very commonly used. Use an optional second parameter to specify the clear or background colour. Pull-Request: #122
1 parent d8cb1ba commit 860e6c8

File tree

7 files changed

+46
-26
lines changed

7 files changed

+46
-26
lines changed

frontend/courses/sample/animate/juggle.evy

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,6 @@ func delta:num d:num n:num
4848
return -d
4949
end
5050

51-
func clear
52-
color "white"
53-
move 0 0
54-
rect 100 100
55-
end
56-
5751
func draw dot:{}num col:string
5852
color col
5953
move dot.x dot.y

frontend/courses/sample/animate/movingdot.evy

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@ for i := range 0 100 0.1
55
sleep 0.01
66
end
77

8-
func clear
9-
color "white"
10-
move 0 0
11-
rect 100 100
12-
end
13-
148
func dot x:num y:num
159
color "red"
1610
move x y

frontend/courses/sample/animate/splashtrig.evy

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ for i := range (len dots)
6060
end
6161

6262
on animate
63-
clear
63+
// 2% opacity leaves trails on movement
64+
clear "hsl(0deg 100% 100% / 2%)"
6465

6566
for dot := range dots
6667
update dot
@@ -72,13 +73,6 @@ func update dot:{}num
7273
dot.phase = dot.phase + dot.s
7374
end
7475

75-
func clear
76-
// 2% opacity leaves trails on movement
77-
color "hsl(0deg 100% 100% / 2%)"
78-
move 0 0
79-
rect 100 100
80-
end
81-
8276
func draw dot:{}num col:string
8377
color col
8478
x := 50 + dot.orbit * (cos (tau * dot.phase))

frontend/courses/sample/tour/events.evy

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@ func dot x:num y:num radius:num hue:num
5050
circle radius
5151
end
5252

53-
func clear
54-
color "white"
55-
move 0 0
56-
rect 100 100
57-
end
58-
5953
func hsl hue:num sat:num light:num
6054
s := sprintf "hsl(%.0fdeg %.0f%% %.0f%%)" hue%360 sat light
6155
color s

frontend/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ function newEvyGo() {
5454
circle,
5555
rect,
5656
color,
57+
clear,
5758
}
5859
const go = new Go() // see wasm_exec.js
5960
go.importObject.env = Object.assign(go.importObject.env, evyEnv)
@@ -461,6 +462,20 @@ function circle(r) {
461462
ctx.fill()
462463
}
463464

465+
// clear is exported to evy go/wasm.
466+
function clear(ptr, len) {
467+
const ctx = canvas.ctx
468+
if (len === 0) {
469+
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height)
470+
return
471+
}
472+
const color = memToString(ptr, len)
473+
const prevColor = ctx.fillStyle
474+
ctx.fillStyle = color
475+
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height)
476+
ctx.fillStyle = prevColor
477+
}
478+
464479
function logicalX(e) {
465480
const scaleX = (canvas.width * canvas.scale.x) / e.target.offsetWidth
466481
return e.offsetX * scaleX - canvas.offset.x

pkg/evaluator/builtin.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func DefaultBuiltins(rt Runtime) Builtins {
8989
"width": numBuiltin("width", rt.Width),
9090
"color": stringBuiltin("color", rt.Color),
9191
"colour": stringBuiltin("colour", rt.Color),
92+
"clear": {Func: clearFunc(rt.Clear), Decl: clearDecl},
9293
}
9394
xyParams := []*parser.Var{
9495
{Name: "x", T: parser.NUM_TYPE},
@@ -135,6 +136,7 @@ type GraphicsRuntime interface {
135136
Circle(radius float64)
136137
Width(w float64)
137138
Color(s string)
139+
Clear(color string)
138140
}
139141

140142
type UnimplementedRuntime struct {
@@ -162,6 +164,7 @@ func (rt *UnimplementedRuntime) Rect(x, y float64) { rt.Unimplemented("rect"
162164
func (rt *UnimplementedRuntime) Circle(r float64) { rt.Unimplemented("circle") }
163165
func (rt *UnimplementedRuntime) Width(w float64) { rt.Unimplemented("width") }
164166
func (rt *UnimplementedRuntime) Color(s string) { rt.Unimplemented("color") }
167+
func (rt *UnimplementedRuntime) Clear(color string) { rt.Unimplemented("clear") }
165168

166169
var readDecl = &parser.FuncDeclStmt{
167170
Name: "read",
@@ -523,6 +526,26 @@ func rand1Func(_ *scope, args []Value) (Value, error) {
523526
return &Num{Val: rand.Float64()}, nil //nolint: gosec
524527
}
525528

529+
var clearDecl = &parser.FuncDeclStmt{
530+
Name: "clear",
531+
VariadicParam: &parser.Var{Name: "s", T: parser.STRING_TYPE},
532+
ReturnType: parser.NONE_TYPE,
533+
}
534+
535+
func clearFunc(clearFn func(string)) BuiltinFunc {
536+
return func(_ *scope, args []Value) (Value, error) {
537+
if len(args) > 1 {
538+
return nil, fmt.Errorf("%w: 'clear' takes 0 or 1 string arguments", ErrBadArguments)
539+
}
540+
color := ""
541+
if len(args) == 1 {
542+
color = args[0].(*String).Val
543+
}
544+
clearFn(color)
545+
return nil, nil
546+
}
547+
}
548+
526549
func xyDecl(name string) *parser.FuncDeclStmt {
527550
return &parser.FuncDeclStmt{
528551
Name: name,

pkg/wasm/imports.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func (rt *jsRuntime) Rect(x, y float64) { rect(x, y) }
3333
func (rt *jsRuntime) Circle(r float64) { circle(r) }
3434
func (rt *jsRuntime) Width(w float64) { width(w) }
3535
func (rt *jsRuntime) Color(s string) { color(s) }
36+
func (rt *jsRuntime) Clear(color string) { clear(color) }
3637
func (rt *jsRuntime) Yielder() evaluator.Yielder { return rt.yielder }
3738

3839
// sleepingYielder yields the CPU so that JavaScript/browser events
@@ -157,6 +158,11 @@ func width(w float64)
157158
//export color
158159
func color(s string)
159160

161+
// clear is imported from JS
162+
//
163+
//export clear
164+
func clear(s string)
165+
160166
// setEvySource is imported from JS
161167
//
162168
//export setEvySource

0 commit comments

Comments
 (0)