forked from tidbyt/pixlet
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a pure-browser version of the Pixlet UI (tidbyt#784)
* Support compiling with GOOS=js GOARCH=wasm `GOOS=js GOARCH=wasm make build` should work now. * Add a pure-browser version of the Pixlet UI Adds a WASM backend mode for Pixlet, which builds `pixlet.wasm` and loads it in the browser. This WASM binary then runs Starlark, without the need for the `pixlet` Go backend. Use `npm run start:wasm` to try it.
- Loading branch information
1 parent
f88aa23
commit dd68315
Showing
35 changed files
with
712 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build !js && !wasm | ||
|
||
package community | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build !js && !wasm | ||
|
||
package cmd | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package encode | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"image" | ||
"image/color" | ||
"image/draw" | ||
"image/gif" | ||
|
||
"github.com/ericpauley/go-quantize/quantize" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// Renders a screen to GIF. Optionally pass filters for postprocessing | ||
// each individual frame. | ||
func (s *Screens) EncodeGIF(maxDuration int, filters ...ImageFilter) ([]byte, error) { | ||
images, err := s.render(filters...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(images) == 0 { | ||
return []byte{}, nil | ||
} | ||
|
||
g := &gif.GIF{} | ||
|
||
remainingDuration := maxDuration | ||
for imIdx, im := range images { | ||
imRGBA, ok := im.(*image.RGBA) | ||
if !ok { | ||
return nil, fmt.Errorf("image %d is %T, require RGBA", imIdx, im) | ||
} | ||
|
||
palette := quantize.MedianCutQuantizer{}.Quantize(make([]color.Color, 0, 256), im) | ||
imPaletted := image.NewPaletted(imRGBA.Bounds(), palette) | ||
draw.Draw(imPaletted, imRGBA.Bounds(), imRGBA, image.Point{0, 0}, draw.Src) | ||
|
||
frameDelay := int(s.delay) | ||
if maxDuration > 0 { | ||
if frameDelay > remainingDuration { | ||
frameDelay = remainingDuration | ||
} | ||
remainingDuration -= frameDelay | ||
} | ||
|
||
g.Image = append(g.Image, imPaletted) | ||
g.Delay = append(g.Delay, frameDelay/10) // in 100ths of a second | ||
|
||
if maxDuration > 0 && remainingDuration <= 0 { | ||
break | ||
} | ||
} | ||
|
||
buf := &bytes.Buffer{} | ||
err = gif.EncodeAll(buf, g) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "encoding") | ||
} | ||
|
||
return buf.Bytes(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//go:build !js && !wasm | ||
|
||
package encode | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/tidbyt/go-libwebp/webp" | ||
) | ||
|
||
// Renders a screen to WebP. Optionally pass filters for | ||
// postprocessing each individual frame. | ||
func (s *Screens) EncodeWebP(maxDuration int, filters ...ImageFilter) ([]byte, error) { | ||
images, err := s.render(filters...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(images) == 0 { | ||
return []byte{}, nil | ||
} | ||
|
||
bounds := images[0].Bounds() | ||
anim, err := webp.NewAnimationEncoder( | ||
bounds.Dx(), | ||
bounds.Dy(), | ||
WebPKMin, | ||
WebPKMax, | ||
) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "initializing encoder") | ||
} | ||
defer anim.Close() | ||
|
||
remainingDuration := time.Duration(maxDuration) * time.Millisecond | ||
for _, im := range images { | ||
frameDuration := time.Duration(s.delay) * time.Millisecond | ||
|
||
if maxDuration > 0 { | ||
if frameDuration > remainingDuration { | ||
frameDuration = remainingDuration | ||
} | ||
remainingDuration -= frameDuration | ||
} | ||
|
||
if err := anim.AddFrame(im, frameDuration); err != nil { | ||
return nil, errors.Wrap(err, "adding frame") | ||
} | ||
|
||
if maxDuration > 0 && remainingDuration <= 0 { | ||
break | ||
} | ||
} | ||
|
||
buf, err := anim.Assemble() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "encoding animation") | ||
} | ||
|
||
return buf, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//go:build js && wasm | ||
|
||
package encode | ||
|
||
// Renders a screen to WebP. Optionally pass filters for | ||
// postprocessing each individual frame. | ||
func (s *Screens) EncodeWebP(maxDuration int, filters ...ImageFilter) ([]byte, error) { | ||
// lol you gullible sucker, you thought you could use webp in wasm? | ||
return s.EncodeGIF(maxDuration, filters...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//go:build !js && !wasm | ||
|
||
package main | ||
|
||
import ( | ||
"tidbyt.dev/pixlet/cmd" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(cmd.CreateCmd) | ||
} |
Oops, something went wrong.