func main() { var err error gophersImage, _, err = common.AssetImage("gophers.jpg", ebiten.FilterNearest) if err != nil { log.Fatal(err) } fiveyearsImage, _, err = common.AssetImage("fiveyears.jpg", ebiten.FilterNearest) if err != nil { log.Fatal(err) } maskImage, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest) if err != nil { log.Fatal(err) } as := image.Point{128, 128} a := image.NewAlpha(image.Rectangle{image.ZP, as}) for j := 0; j < as.Y; j++ { for i := 0; i < as.X; i++ { r := as.X / 2 d := math.Sqrt(float64((i-r)*(i-r) + (j-r)*(j-r))) b := uint8(max(0, min(0xff, 3*0xff-int(d*3*0xff)/r))) a.SetAlpha(i, j, color.Alpha{b}) } } spotLightImage, err = ebiten.NewImageFromImage(a, ebiten.FilterNearest) if err != nil { log.Fatal(err) } if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Masking (Ebiten Demo)"); err != nil { log.Fatal(err) } }
func main() { var err error const a0, a1, a2 = 0x40, 0xc0, 0xff pixels := []uint8{ a0, a1, a1, a0, a1, a2, a2, a1, a1, a2, a2, a1, a0, a1, a1, a0, } brushImage, err = ebiten.NewImageFromImage(&image.Alpha{ Pix: pixels, Stride: 4, Rect: image.Rect(0, 0, 4, 4), }, ebiten.FilterNearest) if err != nil { log.Fatal(err) } canvasImage, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest) if err != nil { log.Fatal(err) } canvasImage.Fill(color.White) if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Paint (Ebiten Demo)"); err != nil { log.Fatal(err) } }
// DebugPrint prints the given text str on the given image r. func (d *debugPrintState) DebugPrint(r *ebiten.Image, str string) error { if d.textImage == nil { img, err := assets.TextImage() if err != nil { return err } d.textImage, err = ebiten.NewImageFromImage(img, ebiten.FilterNearest) if err != nil { return err } } if d.debugPrintRenderTarget == nil { width, height := 256, 256 var err error d.debugPrintRenderTarget, err = ebiten.NewImage(width, height, ebiten.FilterNearest) if err != nil { return err } } if err := d.drawText(r, str, 1, 1, color.NRGBA{0x00, 0x00, 0x00, 0x80}); err != nil { return err } if err := d.drawText(r, str, 0, 0, color.NRGBA{0xff, 0xff, 0xff, 0xff}); err != nil { return err } return nil }
func (g *Game) updateFrame() { m := image.NewRGBA(g.Bounds) draw.Draw(m, g.Bounds, g.Image, g.Bounds.Min, draw.Src) m.Set(g.X, g.Y, Orange) if f, err := ebiten.NewImageFromImage(m, ebiten.FilterLinear); err == nil { g.Frame = f } }
func init() { img, err := assets.ArcadeFontImage() if err != nil { panic(err) } eimg, err := ebiten.NewImageFromImage(img, ebiten.FilterNearest) if err != nil { panic(err) } ArcadeFont = &Font{eimg, img, 32, 16, 8, 8} }
func reloadSimulation() error { currentSimulation = simulation.New(simulationImage) currentSimulation.Draw(simulationImage) var err error backgroundImage, err = ebiten.NewImageFromImage(simulationImage, ebiten.FilterNearest) if err != nil { log.Fatal(err) } for _, img := range wireImages { if err = img.Dispose(); err != nil { return err } } wires := currentSimulation.Circuit().Wires() wireImages = make([]*ebiten.Image, len(wires)) for i, wire := range wires { img := drawMask(wire) var err error if wireImages[i], err = ebiten.NewImageFromImage(img, ebiten.FilterNearest); err != nil { return err } } return nil }
func AssetImage(path string, filter ebiten.Filter) (*ebiten.Image, image.Image, error) { b, err := assets.Asset("images/" + path) if err != nil { return nil, nil, err } img, _, err := image.Decode(bytes.NewBuffer(b)) if err != nil { return nil, nil, err } eimg, err := ebiten.NewImageFromImage(img, filter) if err != nil { return nil, nil, err } return eimg, img, nil }
func main() { var err error gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest) if err != nil { log.Fatal(err) } w, h := gophersImage.Size() const repeat = 5 repeatedGophersImage, err = ebiten.NewImage(w*repeat, h*repeat, ebiten.FilterNearest) if err != nil { log.Fatal(err) } for j := 0; j < repeat; j++ { for i := 0; i < repeat; i++ { op := &ebiten.DrawImageOptions{} op.GeoM.Translate(float64(w*i), float64(h*j)) if err := repeatedGophersImage.DrawImage(gophersImage, op); err != nil { log.Fatal(err) } } } groundWidth := screenWidth + 70 groundImage, err = ebiten.NewImage(groundWidth, screenHeight*2/3+50, ebiten.FilterNearest) if err != nil { log.Fatal(err) } const fogHeight = 8 fogRGBA := image.NewRGBA(image.Rect(0, 0, groundWidth, fogHeight)) for j := 0; j < fogHeight; j++ { a := uint32(float64(fogHeight-1-j) * 0xff / (fogHeight - 1)) clr := skyColor r, g, b, oa := uint32(clr.R), uint32(clr.G), uint32(clr.B), uint32(clr.A) clr.R = uint8(r * a / oa) clr.G = uint8(g * a / oa) clr.B = uint8(b * a / oa) clr.A = uint8(a) for i := 0; i < groundWidth; i++ { fogRGBA.SetRGBA(i, j, clr) } } fogImage, err = ebiten.NewImageFromImage(fogRGBA, ebiten.FilterNearest) if err != nil { log.Fatal(err) } if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Air Ship (Ebiten Demo)"); err != nil { log.Fatal(err) } }
// NewImageFromFile loads the file path and returns ebiten.Image and image.Image. // // The current directory for path depends on your environment. This will vary on your desktop or web browser. // It'll be safer to embed your resource, e.g., with github.com/jteeuwen/go-bindata instead of using this function. func NewImageFromFile(path string, filter ebiten.Filter) (*ebiten.Image, image.Image, error) { file, err := os.Open(path) if err != nil { return nil, nil, err } defer file.Close() img, _, err := image.Decode(file) if err != nil { return nil, nil, err } img2, err := ebiten.NewImageFromImage(img, filter) if err != nil { return nil, nil, err } return img2, img, err }
func update(screen *ebiten.Image) error { count++ m = mandelbrot.New(*width, *height, iterations(), mandelbrot.Colors(hex(*inside), hex(*outside))).Image() mandelbrotImage, err := ebiten.NewImageFromImage(m, ebiten.FilterLinear) if err == nil { op := &ebiten.DrawImageOptions{} op.GeoM.Translate(-float64(*width)/2, -float64(*height)/2) op.GeoM.Rotate(float64(count%360) * 2 * math.Pi / 360) op.GeoM.Translate(float64(*width)/2, float64(*height)/2) if err := screen.DrawImage(mandelbrotImage, op); err != nil { return err } } return nil }
func NewImageFromFile(path string, filter ebiten.Filter) (*ebiten.Image, image.Image, error) { var err error var content *js.Object ch := make(chan struct{}) req := js.Global.Get("XMLHttpRequest").New() req.Call("open", "GET", path, true) req.Set("responseType", "arraybuffer") req.Call("addEventListener", "load", func() { defer close(ch) status := req.Get("status").Int() if 200 <= status && status < 400 { content = req.Get("response") return } err = errors.New(fmt.Sprintf("http error: %d", status)) }) req.Call("addEventListener", "error", func() { defer close(ch) err = errors.New(fmt.Sprintf("XMLHttpRequest error: %s", req.Get("statusText").String())) }) req.Call("send") <-ch if err != nil { return nil, nil, err } data := js.Global.Get("Uint8Array").New(content).Interface().([]uint8) b := bytes.NewBuffer(data) img, _, err := image.Decode(b) if err != nil { return nil, nil, err } img2, err := ebiten.NewImageFromImage(img, filter) if err != nil { return nil, nil, err } return img2, img, nil }