Draw in Canvas

131 views
Skip to first unread message

Александр Синотов

unread,
May 26, 2016, 7:44:39 PM5/26/16
to GopherJS
Hello there!

I have a problem with drawing in canvas from GopherJS.
Without much preamble:
[q.go]
package main

import (
    "github.com/gopherjs/gopherjs/js"
)

func Test() {
    document := js.Global.Get("document")
    cnvs := document.Call("getElementById", "cnvs")
    ctx := cnvs.Call("getContext", "2d")

    imgData := ctx.Call("createImageData", 128, 128)
   
    for  i := 0; i < 128 * 128; i++  {
        data := imgData.Call("data")
        pD := i * 4
        data.SetIndex(pD + 0, 0xff)
        data.SetIndex(pD + 1, 0)
        data.SetIndex(pD + 2, 0)
        data.SetIndex(pD + 3, 0xff)
    }
}

func main() {
    js.Global.Set("miejs", map[string]interface{}{
        "Test":    Test,
    })
}
[/q.go]

[q.html]
<html>
    <head>
        <meta charset="utf-8">
        <title>MieJS</title>
        <script src="q.js" type="text/javascript"></script>
    </head>
    <body>
        <canvas id="cnvs" height="128" width="128"></canvas>
    </body>
</html>
[/q.html]

And nothing.

Rusco

unread,
May 27, 2016, 4:40:21 AM5/27/16
to GopherJS

Have a look (I renamed your file to canvas.go):

Александр Синотов

unread,
May 27, 2016, 10:39:47 AM5/27/16
to GopherJS
Thanks a lot!
Reply all
Reply to author
Forward
0 new messages