egui/docs/index.html

189 lines
6.1 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
2019-01-17 23:34:01 +00:00
<!-- Disable zooming: -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<!-- TODO: read https://www.html5rocks.com/en/mobile/mobifying/#toc-meta-viewport -->
<head>
2019-01-12 22:07:30 +00:00
<title>Emigui A experiment in an Immediate Mode GUI written in Rust</title>
<style>
html {
/* Remove touch delay: */
touch-action: manipulation;
}
body {
2018-12-23 23:15:18 +00:00
background: #000000;
}
html,
body {
overflow: hidden;
}
</style>
</head>
<body>
<script>
// The `--no-modules`-generated JS from `wasm-bindgen` attempts to use
// `WebAssembly.instantiateStreaming` to instantiate the wasm module,
// but this doesn't work with `file://` urls. This example is frequently
// viewed by simply opening `index.html` in a browser (with a `file://`
// url), so it would fail if we were to call this function!
//
// Work around this for now by deleting the function to ensure that the
// `no_modules.js` script doesn't have access to it. You won't need this
// hack when deploying over HTTP.
delete WebAssembly.instantiateStreaming;
</script>
2019-02-09 22:00:07 +00:00
<!-- this is the JS generated by the `wasm-bindgen` CLI tool -->
2019-03-16 11:55:42 +00:00
<script src="example_wasm.js"></script>
2019-02-09 22:00:07 +00:00
<script>
// we'll defer our execution until the wasm is ready to go
// here we tell bindgen the path to the wasm file so it can start
// initialization and return to us a promise when it's done
2019-03-16 11:55:42 +00:00
wasm_bindgen("./example_wasm_bg.wasm")
2019-02-09 22:00:07 +00:00
.then(on_wasm_loaded)["catch"](console.error);
// ----------------------------------------------------------------------------
2019-02-09 22:00:07 +00:00
var g_wasm_app = null;
function paint_gui(canvas, input) {
2019-02-09 22:00:07 +00:00
if (g_wasm_app === null) {
g_wasm_app = wasm_bindgen.new_webgl_gui("canvas", pixels_per_point());
}
2019-02-09 22:00:07 +00:00
wasm_bindgen.run_gui(g_wasm_app, JSON.stringify(input));
}
// ----------------------------------------------------------------------------
var g_mouse_pos = null;
var g_mouse_down = false;
2019-03-16 13:12:40 +00:00
var g_is_touch = false;
2019-01-19 16:10:28 +00:00
function pixels_per_point() {
// return 1.0;
return window.devicePixelRatio || 1.0;
}
function auto_resize_canvas(canvas) {
2019-01-19 16:10:28 +00:00
canvas.style.width = window.innerWidth + "px";
canvas.style.height = window.innerHeight + "px";
canvas.width = window.innerWidth * pixels_per_point();
canvas.height = window.innerHeight * pixels_per_point();
}
function get_input(canvas) {
return {
mouse_down: g_mouse_down,
mouse_pos: g_mouse_pos,
2019-01-19 16:10:28 +00:00
screen_size: { x: window.innerWidth, y: window.innerHeight },
pixels_per_point: pixels_per_point(),
};
}
function mouse_pos_from_event(canvas, event) {
var rect = canvas.getBoundingClientRect();
return {
x: event.clientX - rect.left,
y: event.clientY - rect.top
};
}
const ANIMATION_FRAME = true;
function paint() {
var canvas = document.getElementById("canvas");
auto_resize_canvas(canvas);
paint_gui(canvas, get_input(canvas));
2019-03-16 14:49:41 +00:00
}
2019-03-16 14:49:41 +00:00
function paint_and_schedule() {
paint();
if (ANIMATION_FRAME) {
2019-03-16 14:49:41 +00:00
window.requestAnimationFrame(paint_and_schedule);
}
}
2019-02-09 22:00:07 +00:00
function on_wasm_loaded() {
var canvas = document.getElementById("canvas");
2019-03-16 14:49:41 +00:00
var invalidate = function() {
if (!ANIMATION_FRAME) {
paint();
}
};
2019-01-17 23:34:01 +00:00
canvas.addEventListener("mousedown", function(event) {
2019-03-16 13:12:40 +00:00
if (g_is_touch) { return; }
2019-01-17 23:34:01 +00:00
g_mouse_pos = mouse_pos_from_event(canvas, event);
g_mouse_down = true;
2019-03-16 14:49:41 +00:00
invalidate();
2019-01-17 23:34:01 +00:00
event.stopPropagation();
event.preventDefault();
});
canvas.addEventListener("mousemove", function(event) {
2019-03-16 13:12:40 +00:00
if (g_is_touch) { return; }
g_mouse_pos = mouse_pos_from_event(canvas, event);
2019-03-16 14:49:41 +00:00
invalidate();
event.stopPropagation();
event.preventDefault();
});
2019-01-17 23:34:01 +00:00
canvas.addEventListener("mouseup", function(event) {
2019-03-16 13:12:40 +00:00
if (g_is_touch) { return; }
2019-01-17 23:34:01 +00:00
g_mouse_pos = mouse_pos_from_event(canvas, event);
g_mouse_down = false;
2019-03-16 14:49:41 +00:00
invalidate();
2019-01-17 23:34:01 +00:00
event.stopPropagation();
event.preventDefault();
});
canvas.addEventListener("mouseleave", function(event) {
2019-03-16 13:12:40 +00:00
if (g_is_touch) { return; }
g_mouse_pos = null;
2019-03-16 14:49:41 +00:00
invalidate();
event.stopPropagation();
event.preventDefault();
});
2019-01-17 23:34:01 +00:00
canvas.addEventListener("touchstart", function(event) {
2019-03-16 13:12:40 +00:00
g_is_touch = true;
2019-01-17 23:34:01 +00:00
g_mouse_pos = { x: event.touches[0].pageX, y: event.touches[0].pageY };
g_mouse_down = true;
2019-03-16 14:49:41 +00:00
invalidate();
event.stopPropagation();
event.preventDefault();
});
2019-01-17 23:34:01 +00:00
canvas.addEventListener("touchmove", function(event) {
2019-03-16 13:12:40 +00:00
g_is_touch = true;
2019-01-17 23:34:01 +00:00
g_mouse_pos = { x: event.touches[0].pageX, y: event.touches[0].pageY };
2019-03-16 14:49:41 +00:00
invalidate();
2019-01-17 23:34:01 +00:00
event.stopPropagation();
event.preventDefault();
});
canvas.addEventListener("touchend", function(event) {
2019-03-16 13:12:40 +00:00
g_is_touch = true;
g_mouse_down = false; // First release mouse to click...
2019-03-16 14:49:41 +00:00
update_gui()();
2019-03-16 13:12:40 +00:00
g_mouse_pos = null; // ...remove hover effect
2019-03-16 14:49:41 +00:00
update_gui()();
event.stopPropagation();
event.preventDefault();
});
2019-01-17 23:34:01 +00:00
2019-03-16 13:12:40 +00:00
if (!ANIMATION_FRAME) {
2019-03-16 14:49:41 +00:00
window.addEventListener("load", invalidate);
window.addEventListener("pagehide", invalidate);
window.addEventListener("pageshow", invalidate);
window.addEventListener("resize", invalidate);
2019-03-16 13:12:40 +00:00
}
2019-03-16 14:49:41 +00:00
paint_and_schedule();
}
</script>
<!-- TODO: make this cover the entire screen, with resize and all -->
2018-12-27 23:51:40 +00:00
<canvas id="canvas" width="1024" height="1024"></canvas>
</body>
</html>