-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
47 lines (37 loc) · 1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const triangle = require('a-big-triangle')
const context = require('gl-context')
const fit = require('canvas-fit')
const Shader = require('gl-shader')
module.exports = toy
const vert = `
precision mediump float;
attribute vec2 position;
void main() {
gl_Position = vec4(position, 1, 1);
}
`.trim()
// String -> WebGLRenderingContext, Shader
function toy(frag, cb) {
const canvas = document.body.appendChild(document.createElement('canvas'))
const gl = context(canvas, render)
const shader = Shader(gl, vert, frag)
const fitter = fit(canvas)
render.update = update
render.resize = fitter
render.shader = shader
render.canvas = canvas
render.gl = gl
window.addEventListener('resize', fitter, false)
return render
function render() {
const width = gl.drawingBufferWidth
const height = gl.drawingBufferHeight
gl.viewport(0, 0, width, height)
shader.bind()
cb(gl, shader)
triangle(gl)
}
function update(frag) {
shader.update(vert, frag)
}
}