Full screen canvas

This commit is contained in:
Emil Ernerfeldt 2018-12-28 00:51:40 +01:00
parent 1c6fd220b2
commit 702ec23372
4 changed files with 93 additions and 44 deletions

View file

@ -111,38 +111,59 @@ function paint_gui(canvas, input) {
var g_mouse_pos = { x: -1000.0, y: -1000.0 }; var g_mouse_pos = { x: -1000.0, y: -1000.0 };
var g_mouse_down = false; var g_mouse_down = false;
function get_input(canvas) { function get_input(canvas) {
var pixels_per_point = window.devicePixelRatio || 1;
var ctx = canvas.getContext("2d");
ctx.scale(pixels_per_point, pixels_per_point);
// Resize based on screen size:
canvas.setAttribute("width", window.innerWidth * pixels_per_point);
canvas.setAttribute("height", window.innerHeight * pixels_per_point);
return { return {
mouse_down: g_mouse_down, mouse_down: g_mouse_down,
mouse_pos: g_mouse_pos, mouse_pos: g_mouse_pos,
screen_size: { x: canvas.width, y: canvas.height } screen_size: { x: canvas.width, y: canvas.height }
}; };
} }
function mouse_pos_from_event(canvas, evt) { function mouse_pos_from_event(canvas, event) {
var rect = canvas.getBoundingClientRect(); var rect = canvas.getBoundingClientRect();
return { return {
x: evt.clientX - rect.left, x: event.clientX - rect.left,
y: evt.clientY - rect.top y: event.clientY - rect.top
}; };
} }
function initialize() { function initialize() {
console.log("window.devicePixelRatio: " + window.devicePixelRatio);
var canvas = document.getElementById("canvas"); var canvas = document.getElementById("canvas");
canvas.addEventListener("mousemove", function (evt) { var repaint = function () { return paint_gui(canvas, get_input(canvas)); };
g_mouse_pos = mouse_pos_from_event(canvas, evt); canvas.addEventListener("mousemove", function (event) {
paint_gui(canvas, get_input(canvas)); g_mouse_pos = mouse_pos_from_event(canvas, event);
}, false); repaint();
canvas.addEventListener("mouseleave", function (evt) { event.stopPropagation();
event.preventDefault();
});
canvas.addEventListener("mouseleave", function (event) {
g_mouse_pos = { x: -1000.0, y: -1000.0 }; g_mouse_pos = { x: -1000.0, y: -1000.0 };
paint_gui(canvas, get_input(canvas)); repaint();
}, false); event.stopPropagation();
canvas.addEventListener("mousedown", function (evt) { event.preventDefault();
g_mouse_pos = mouse_pos_from_event(canvas, evt); });
canvas.addEventListener("mousedown", function (event) {
g_mouse_pos = mouse_pos_from_event(canvas, event);
g_mouse_down = true; g_mouse_down = true;
paint_gui(canvas, get_input(canvas)); repaint();
}, false); event.stopPropagation();
canvas.addEventListener("mouseup", function (evt) { event.preventDefault();
g_mouse_pos = mouse_pos_from_event(canvas, evt); });
canvas.addEventListener("mouseup", function (event) {
g_mouse_pos = mouse_pos_from_event(canvas, event);
g_mouse_down = false; g_mouse_down = false;
paint_gui(canvas, get_input(canvas)); repaint();
}, false); event.stopPropagation();
paint_gui(canvas, get_input(canvas)); event.preventDefault();
});
window.addEventListener("load", repaint);
window.addEventListener("pagehide", repaint);
window.addEventListener("pageshow", repaint);
window.addEventListener("resize", repaint);
// setInterval(repaint, 16);
repaint();
} }

View file

@ -52,7 +52,7 @@ interface Text {
kind: "text"; kind: "text";
fill_color: Color | null; fill_color: Color | null;
font_name: string; // e.g. "Palatino" font_name: string; // e.g. "Palatino"
font_size: number, // Height in pixels, e.g. 12 font_size: number; // Height in pixels, e.g. 12
pos: Vec2; pos: Vec2;
stroke_color: Color | null; stroke_color: Color | null;
text: string; text: string;
@ -211,6 +211,15 @@ let g_mouse_pos = { x: -1000.0, y: -1000.0 };
let g_mouse_down = false; let g_mouse_down = false;
function get_input(canvas): RawInput { function get_input(canvas): RawInput {
const pixels_per_point = window.devicePixelRatio || 1;
const ctx = canvas.getContext("2d");
ctx.scale(pixels_per_point, pixels_per_point);
// Resize based on screen size:
canvas.setAttribute("width", window.innerWidth * pixels_per_point);
canvas.setAttribute("height", window.innerHeight * pixels_per_point);
return { return {
mouse_down: g_mouse_down, mouse_down: g_mouse_down,
mouse_pos: g_mouse_pos, mouse_pos: g_mouse_pos,
@ -218,54 +227,68 @@ function get_input(canvas): RawInput {
}; };
} }
function mouse_pos_from_event(canvas, evt): Vec2 { function mouse_pos_from_event(canvas, event): Vec2 {
const rect = canvas.getBoundingClientRect(); const rect = canvas.getBoundingClientRect();
return { return {
x: evt.clientX - rect.left, x: event.clientX - rect.left,
y: evt.clientY - rect.top, y: event.clientY - rect.top,
}; };
} }
function initialize() { function initialize() {
console.log(`window.devicePixelRatio: ${window.devicePixelRatio}`);
const canvas = document.getElementById("canvas"); const canvas = document.getElementById("canvas");
const repaint = () => paint_gui(canvas, get_input(canvas));
canvas.addEventListener( canvas.addEventListener(
"mousemove", "mousemove",
(evt) => { (event) => {
g_mouse_pos = mouse_pos_from_event(canvas, evt); g_mouse_pos = mouse_pos_from_event(canvas, event);
paint_gui(canvas, get_input(canvas)); repaint();
event.stopPropagation();
event.preventDefault();
}, },
false,
); );
canvas.addEventListener( canvas.addEventListener(
"mouseleave", "mouseleave",
(evt) => { (event) => {
g_mouse_pos = { x: -1000.0, y: -1000.0 }; g_mouse_pos = { x: -1000.0, y: -1000.0 };
paint_gui(canvas, get_input(canvas)); repaint();
event.stopPropagation();
event.preventDefault();
}, },
false,
); );
canvas.addEventListener( canvas.addEventListener(
"mousedown", "mousedown",
(evt) => { (event) => {
g_mouse_pos = mouse_pos_from_event(canvas, evt); g_mouse_pos = mouse_pos_from_event(canvas, event);
g_mouse_down = true; g_mouse_down = true;
paint_gui(canvas, get_input(canvas)); repaint();
event.stopPropagation();
event.preventDefault();
}, },
false,
); );
canvas.addEventListener( canvas.addEventListener(
"mouseup", "mouseup",
(evt) => { (event) => {
g_mouse_pos = mouse_pos_from_event(canvas, evt); g_mouse_pos = mouse_pos_from_event(canvas, event);
g_mouse_down = false; g_mouse_down = false;
paint_gui(canvas, get_input(canvas)); repaint();
event.stopPropagation();
event.preventDefault();
}, },
false,
); );
paint_gui(canvas, get_input(canvas)); window.addEventListener("load", repaint);
window.addEventListener("pagehide", repaint);
window.addEventListener("pageshow", repaint);
window.addEventListener("resize", repaint);
// setInterval(repaint, 16);
repaint();
} }

View file

@ -9,12 +9,11 @@
/* Remove touch delay: */ /* Remove touch delay: */
touch-action: manipulation; touch-action: manipulation;
} }
body { body {
background: #000000; background: #000000;
color: #bbbbbb; }
max-width: 1024px; html, body {
margin: auto; overflow: hidden;
} }
</style> </style>
</head> </head>
@ -38,6 +37,6 @@
<script src="frontend.js" type="module"></script> <script src="frontend.js" type="module"></script>
<!-- TODO: make this cover the entire screen, with resize and all --> <!-- TODO: make this cover the entire screen, with resize and all -->
<canvas id="canvas" width="1024" height="768"></canvas> <canvas id="canvas" width="1024" height="1024"></canvas>
</body> </body>
</html> </html>

View file

@ -31,6 +31,12 @@ impl Default for App {
impl GuiSettings for App { impl GuiSettings for App {
fn show_gui(&mut self, gui: &mut Layout) { fn show_gui(&mut self, gui: &mut Layout) {
gui.label(format!(
"Screen size: {} x {}",
gui.input().screen_size.x,
gui.input().screen_size.y,
));
gui.checkbox("checkbox", &mut self.checked); gui.checkbox("checkbox", &mut self.checked);
if gui if gui