<!DOCTYPE html>
<meta charset="utf-8">
<meta property="og:image" itemprop="image primaryImageOfPage" content="https://hajimehoshi.github.io/ebiten/images/examples/mandelbrot.png">
<meta name="description" content="Ebiten example - mandelbrot">
<link rel="shortcut icon" href="../favicon.png" type="image/png" >
<link rel="icon" href="../favicon.png" type="image/png" >
<title>Ebiten example - mandelbrot</title>

<link rel="stylesheet" href="../stylesheets/bootstrap.min.css">
<link rel="stylesheet" href="../stylesheets/highlight-github.css">
<link rel="stylesheet" href="../stylesheets/ebiten.css">
<script src="../scripts/googleanalytics.js"></script>

<nav class="navbar"><div class="container">
  <nav class="d-flex flex-row" style="width: 100%;">
    <div class="nav mr-auto"><a class="navbar-brand" href="../"><img src="../images/logo_white.svg" alt="EBITEN"></a></div>
    <ul class="nav">
      <li class="nav-item"><a class="nav-link" href="https://github.com/hajimehoshi/ebiten">GitHub</a></li>
      <li class="nav-item"><a class="nav-link" href="https://godoc.org/github.com/hajimehoshi/ebiten">GoDoc</a></li>
      <li class="nav-item"><a class="nav-link" href="https://github.com/hajimehoshi/ebiten/wiki">Wiki</a>
      <li class="nav-item"><a class="nav-link" href="https://ebiten-playground.github.io/">Playground</a>
    </ul>
  </nav>
</div></nav>

<main><div class="container">

  <h2>Ebiten example - mandelbrot</h2>
  <iframe src="mandelbrot.content.html" width="640" height="640"></iframe>
  <div class="card"><pre class="card-body"><code class="language-go">// &#43;build example jsgo

package main

import (
        &#34;log&#34;
        &#34;math&#34;

        &#34;github.com/hajimehoshi/ebiten&#34;
)

const (
        screenWidth  = 640
        screenHeight = 640
        maxIt        = 128
)

var (
        offscreen    *ebiten.Image
        offscreenPix []byte
        palette      [maxIt]byte
)

func init() {
        offscreen, _ = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterDefault)
        offscreenPix = make([]byte, screenWidth*screenHeight*4)
        for i := range palette {
                palette[i] = byte(math.Sqrt(float64(i)/float64(len(palette))) * 0x80)
        }
}

func color(it int) (r, g, b byte) {
        if it == maxIt {
                return 0xff, 0xff, 0xff
        }
        c := palette[it]
        return c, c, c
}

func updateOffscreen(centerX, centerY, size float64) {
        for j := 0; j &lt; screenHeight; j&#43;&#43; {
                for i := 0; i &lt; screenHeight; i&#43;&#43; {
                        x := float64(i)*size/screenWidth - size/2 &#43; centerX
                        y := (screenHeight-float64(j))*size/screenHeight - size/2 &#43; centerY
                        c := complex(x, y)
                        z := complex(0, 0)
                        it := 0
                        for ; it &lt; maxIt; it&#43;&#43; {
                                z = z*z &#43; c
                                if real(z)*real(z)&#43;imag(z)*imag(z) &gt; 4 {
                                        break
                                }
                        }
                        r, g, b := color(it)
                        p := 4 * (i &#43; j*screenWidth)
                        offscreenPix[p] = r
                        offscreenPix[p&#43;1] = g
                        offscreenPix[p&#43;2] = b
                        offscreenPix[p&#43;3] = 0xff
                }
        }
        offscreen.ReplacePixels(offscreenPix)
}

func init() {
        // Now it is not feasible to call updateOffscreen every frame due to performance.
        updateOffscreen(-0.75, 0.25, 2)
}

func update(screen *ebiten.Image) error {
        if ebiten.IsRunningSlowly() {
                return nil
        }

        screen.DrawImage(offscreen, nil)
        return nil
}

func main() {
        if err := ebiten.Run(update, screenWidth, screenHeight, 1, &#34;Mandelbrot (Ebiten Demo)&#34;); err != nil {
                log.Fatal(err)
        }
}
</code></pre></div>

</div></main>

<footer><div class="container">
  <p>© 2013 Hajime Hoshi</p>
  <p>Code is licensed under <a href="https://github.com/hajimehoshi/ebiten/blob/master/LICENSE">the Apache License 2.0</a>.</p>
  <p>The content of this page is licensed under <a href="https://creativecommons.org/licenses/by/4.0/">the Creative Commons Attribution 4.0 License</a>.</p>
</div></footer>

<script src="../scripts/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>