Skip to content

Instantly share code, notes, and snippets.

@SolidTux

SolidTux/none.rs Secret

Created October 18, 2018 06:50
Show Gist options
  • Save SolidTux/d5219bbd0586ec3115fc71e1d165c9e7 to your computer and use it in GitHub Desktop.
Save SolidTux/d5219bbd0586ec3115fc71e1d165c9e7 to your computer and use it in GitHub Desktop.
Generated code for askama template with escape=none.
impl ::askama::Template for WebGlDemo {
fn render_into(&self, writer: &mut ::std::fmt::Write) -> ::askama::Result<()> {
writer.write_str(
"<!DOCTYPE html>\n<html>\n<head>\n <meta charset=\"utf-8\" />\n <title>",
)?;
write!(writer, "{}", &{ self.name })?;
writer.write_str("</title>\n</head>\n<body>\n<canvas id=\"webgl\" width=\"640\" height=\"480\"></canvas>\n<script id=\"vertexShader\" type=\"x-shader/x-vertex\">\nattribute vec3 pos;\nvoid main() {\n gl_Position = vec4(pos, 1.0);\n}\n</script>\n<script id=\"fragmentShader\" type=\"x-shader/x-fragment\">\nprecision mediump float;")?;
writer.write_str("\n")?;
write!(writer, "{}", &{ self.shader })?;
writer.write_str("\n")?;
writer.write_str("</script>\n<script type=\"text/javascript\">\nvar start = new Date();\nvar canvas = document.getElementById(\"webgl\");\nvar gl = canvas.getContext(\"webgl\");\ngl.clearColor(0.8, 0.8, 0.8, 1);\ngl.clear(gl.COLOR_BUFFER_BIT);\nvar prog = shaderProgram(gl,\n document.getElementById(\'vertexShader\').textContent,\n document.getElementById(\'fragmentShader\').textContent,\n);\ngl.useProgram(prog);\nattributeSetFloats(gl, prog, \"pos\", 3, [\n -1, -1, 0,\n -1, 1, 0,\n 1, -1, 0,\n 1, 1, 0\n]);\nuniform_time = gl.getUniformLocation(prog, \"iTime\");\nuniform_res = gl.getUniformLocation(prog, \"iResolution\");\nmain();\nfunction shaderProgram(gl, vs, fs) {\n var prog = gl.createProgram();\n var addshader = function(type, source) {\n var s = gl.createShader((type == \'vertex\') ?\n gl.VERTEX_SHADER : gl.FRAGMENT_SHADER);\n gl.shaderSource(s, source);\n gl.compileShader(s);\n if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) {\n throw \"Could not compile \"+type+\n \" shader:\\n\\n\"+gl.getShaderInfoLog(s);\n }\n gl.attachShader(prog, s);\n };\n addshader(\'vertex\', vs);\n addshader(\'fragment\', fs);\n gl.linkProgram(prog);\n if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) {\n throw \"Could not link the shader program!\";\n }\n return prog;\n}\nfunction attributeSetFloats(gl, prog, attr_name, rsize, arr) {\n gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());\n gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(arr),\n gl.STATIC_DRAW);\n var attr = gl.getAttribLocation(prog, attr_name);\n gl.enableVertexAttribArray(attr);\n gl.vertexAttribPointer(attr, rsize, gl.FLOAT, false, 0, 0);\n}\nfunction draw() {\n var now = new Date();\n gl.uniform1f(uniform_time, (now - start)/1000.);\n gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);\n window.requestAnimationFrame(draw);\n}\nfunction fullscreen() {\n if (canvas.requestFullscreen) {\n canvas.requestFullscreen();\n } else if (canvas.mozRequestFullScreen) {\n canvas.mozRequestFullScreen();\n } else if (canvas.webkitRequestFullscreen) {\n canvas.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);\n }\n}\nfunction fullscreenEvent() {\n canvas.width = canvas.clientWidth;\n canvas.height = canvas.clientHeight;\n gl.uniform2f(uniform_res, canvas.width, canvas.height);\n gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);\n draw();\n}\nfunction main() {\n document.addEventListener(\"click\", fullscreen);\n document.addEventListener(\"fullscreenchange\", fullscreenEvent);\n document.addEventListener(\"mozfullscreenchange\", fullscreenEvent);\n document.addEventListener(\"webkitfullscreenchange\", fullscreenEvent);\n}\n</script>\n</body>\n</html>")?;
Ok(())
}
fn extension(&self) -> Option<&str> {
Some("html")
}
}
impl ::std::fmt::Display for WebGlDemo {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
self.render_into(f).map_err(|_| ::std::fmt::Error {})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment