Skip to content

Commit

Permalink
feat: max frames
Browse files Browse the repository at this point in the history
  • Loading branch information
tnantoka committed Mar 14, 2023
1 parent 61e2ff4 commit 5b01743
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
7 changes: 6 additions & 1 deletion evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ type Evaluator struct {
Scale int
Directory string
WithGIF bool
MaxFrames int
}

func New() *Evaluator {
return &Evaluator{length: DEFAULT_LENGTH, color: color.RGBA{0, 0, 0, 255}, Scale: 1, Directory: "", WithGIF: false}
return &Evaluator{length: DEFAULT_LENGTH, color: color.RGBA{0, 0, 0, 255}, Scale: 1, Directory: "", WithGIF: false, MaxFrames: 0}
}

func (e *Evaluator) Eval(input io.Reader, path string) image.Image {
Expand Down Expand Up @@ -297,6 +298,10 @@ func (e *Evaluator) addGIFFrame() {
return
}

if e.MaxFrames > 0 && len(e.GIF.Image) >= e.MaxFrames {
return
}

scaled := e.scale()
bounds := scaled.Bounds()
paletted := image.NewPaletted(bounds, colorPalette(scaled))
Expand Down
12 changes: 10 additions & 2 deletions evaluator/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,18 +682,26 @@ func TestNumber(t *testing.T) {

func TestGIF(t *testing.T) {
tests := []struct {
input string
expected string
input string
expected string
MaxFrames int
}{
{
"Repeat C 0 10 { Paper C }",
"gradation.gif",
0,
},
{
"Repeat C 0 10 { Paper C }",
"gradation-half.gif",
5,
},
}

for i, test := range tests {
e := New()
e.WithGIF = true
e.MaxFrames = test.MaxFrames
e.Eval(strings.NewReader(test.input), "test.dbn")

if len(e.Errors) > 0 {
Expand Down
Binary file added testdata/gradation-half.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 7 additions & 5 deletions wasm/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
<textarea class="form-control border-top-0" rows="10" data-js="text"></textarea>
</div>

<div class="mt-3 d-flex align-items-center">
<div class="mt-3">
<button class="btn btn-dark" data-js="run">Run</button>
<div class="ms-3 form-check">
<input type="checkbox" class="form-check-input" data-js="with-gif" checked>
<label class="form-check-label" for="exampleCheck1">GIF</label>
</div>
</div>

<div class="mt-3 form-check">
<input type="checkbox" class="form-check-input" data-js="with-gif" checked>
<label class="form-check-label" for="exampleCheck1">GIF</label><br>
</div>
<p class="text-muted fst-italic small">Limited to 200 frames to avoid hangs on wasm.</p>
</form>

<div class="mt-3">
Expand Down
1 change: 1 addition & 0 deletions wasm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func generatePNG(input string) string {
func generateGIF(input string) string {
e := evaluator.New()
e.WithGIF = true
e.MaxFrames = 200

e.Eval(strings.NewReader(input), "input")

Expand Down

0 comments on commit 5b01743

Please sign in to comment.