Socket
Socket
Sign inDemoInstall

github.com/Gaunsessa/webgl

Package Overview
Dependencies
0
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    github.com/Gaunsessa/webgl


Version published

Readme

Source

webgl + tinygo + wasm

GoDoc Go Report Card

WASM target for TinyGo bindings for WebGL 1.0 context.

Example

A full example can be found in in the examples/ directory.

Screenshot

webgl_example.go:

package main

import (
	"syscall/js"

	"github.com/justinclift/webgl"
)

func main() {
	// Get the canvas element
	doc := js.Global().Get("document")
	canvas := doc.Call("getElementById", "mycanvas")
	width := canvas.Get("clientWidth").Int()
	height := canvas.Get("clientHeight").Int()
	canvas.Call("setAttribute", "width", width)
	canvas.Call("setAttribute", "height", height)

	// Set the desired WebGL context attributes
	attrs := webgl.DefaultAttributes()
	attrs.Alpha = false

	// Create the WebGL context
	gl, err := webgl.NewContext(&canvas, attrs)
	if err != nil {
		js.Global().Call("alert", "Error: "+err.Error())
		return
	}

	// Do something with the WebGL context
	gl.ClearColor(0.8, 0.3, 0.01, 1)
	gl.Clear(webgl.COLOR_BUFFER_BIT)
}

index.html:

<!DOCTYPE html>

<html>
<head>
	<meta charset="utf-8" />
	<title>TinyGo wasm WebGL Fundamentals</title>
	<meta name="viewport" content="width=device-width, initial-scale=1" />
	<script src="wasm_exec.js" defer></script>
	<script src="wasm.js" defer></script>
	<style>
		body,pre { margin:0;padding:0; }
		#mycanvas {
			position:fixed;
			opacity:0.9;
			width: 100%;
			height:100%;
			top:0;right:0;bottom:0;left:0;
		}
	</style>
</head>
<body>
	<canvas id="mycanvas">Your browser doesn't appear to support the canvas tag.</canvas>
</body>
</html>

wasm.js:

'use strict';

const WASM_URL = 'wasm.wasm';
var wasm;

// Load and run the wasm
function init() {
  const go = new Go();
  if ('instantiateStreaming' in WebAssembly) {
    WebAssembly.instantiateStreaming(fetch(WASM_URL), go.importObject).then(function (obj) {
      wasm = obj.instance;
      go.run(wasm); // Initial setup
    })
  } else {
    fetch(WASM_URL).then(resp =>
      resp.arrayBuffer()
    ).then(bytes =>
      WebAssembly.instantiate(bytes, go.importObject).then(function (obj) {
        wasm = obj.instance;
        go.run(wasm);
      })
    )
  }
}

init();

wasm_exec.js should be obtained from the TinyGo source repository.

Sources used

FAQs

Last updated on 11 Mar 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc