Skip to content

Instantly share code, notes, and snippets.

c.processor = &triangle.Processor{
BlurRadius: 2,
Noise: 0,
BlurFactor: 2,
EdgeFactor: 4,
PointRate: c.pointRate,
MaxPoints: c.trianglePoints,
PointsThreshold: c.pointsThreshold,
Wireframe: c.wireframe,
StrokeWidth: c.strokeWidth,
uint8Arr := js.Global().Get("Uint8Array").New(width * height * 4)
js.CopyBytesToJS(uint8Arr2, pixels.ImgToPix(blurred))
uint8Clamped := js.Global().Get("Uint8ClampedArray").New(uint8Arr2)
rawData := js.Global().Get("ImageData").New(uint8Clamped, width, height)
c.ctx.Call("putImageData", rawData, 0, 0)
rgba := c.ctx.Call("getImageData", 0, 0, width, height).Get("data")
// Convert the rgba value of type Uint8ClampedArray to Uint8Array in order to
// be able to transfer it from Javascript to Go via the js.CopyBytesToGo function.
uint8Arr := js.Global().Get("Uint8Array").New(rgba)
js.CopyBytesToGo(data, uint8Arr)
// ImgToPix converts an image to an 1D uint8 pixel array.
// In order to preserve the color information per pixel we need to reconstruct the array to a specific format.
func ImgToPix(src *image.NRGBA) []uint8 {
size := src.Bounds().Size()
width, height := size.X, size.Y
pixels := make([][][3]uint8, height)
for y := 0; y < height; y++ {
row := make([][3]uint8, width)
for x := 0; x < width; x++ {
package draw
import (
"image"
"image/color"
)
// ellipse defines the struct components required to apply the ellipse's formula.
type ellipse struct {
cx int // center x
draw.DrawMask(source.(*image.NRGBA), source.Bounds(), ellipse, image.Point{}, mask, image.Point{}, draw.Over)
<html>
<head>
<meta charset="utf-8" />
<script src="js/wasm_exec.js"></script>
<script type="text/javascript">
function fetchAndInstantiate(url, importObject) {
return fetch(url).then(response =>
response.arrayBuffer()
).then(bytes =>
WebAssembly.instantiate(bytes, importObject)
ifeq ($(OS),Windows_NT)
BROWSER = start
else
UNAME := $(shell uname -s)
ifeq ($(UNAME), Linux)
BROWSER = xdg-open
endif
ifeq ($(UNAME), Darwin)
BROWSER = open
endif
// +build js,wasm
package main
import (
"fmt"
"github.com/esimov/pigo/wasm/canvas"
)
@esimov
esimov / caire_batch.go
Created July 2, 2019 09:55
Resize multiple images with Caire.
package main
import (
"fmt"
"log"
"math/rand"
"os"
"runtime"
"strconv"
"time"