github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/wasmcanvas/main.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  	"time"
     6  
     7  	"honnef.co/go/js/dom/v2"
     8  )
     9  
    10  func main() {
    11  	viewElem := dom.GetWindow().Document().QuerySelector("#view")
    12  
    13  	view, ok := viewElem.(*dom.HTMLCanvasElement)
    14  	if !ok {
    15  		log.Printf("not a canvas, was %T", viewElem)
    16  		return
    17  	}
    18  
    19  	imageElem := dom.GetWindow().Document().QuerySelector("#image")
    20  	image, ok := imageElem.(*dom.HTMLImageElement)
    21  	if !ok {
    22  		log.Printf("not an image, was %T", imageElem)
    23  		return
    24  	}
    25  
    26  	context := view.GetContext2d()
    27  	start := time.Now()
    28  	for y := 0; y < 25; y++ {
    29  		for x := 0; x < 25; x++ {
    30  			context.DrawImage(image, float64(x)*16, float64(y)*16)
    31  		}
    32  	}
    33  	delta := time.Since(start)
    34  	context.FillText(delta.String(), 10, 10, 10000000)
    35  }