0

I am creating a Canvas that I want to ensure it is easily navigated on any platform (desktop, mobile, etc). I decided on using a responsive canvas to accomplish this. Before I had the canvas at 800x800 and it looked fine (aside from not being responsive). The canvas includes lines and image files. I'm also using Bootstrap.

I took out the width/height definitions and began testing how it look when responsive. It did act responsive. However, everything looks enlarged and blurry, including the images and the lines. Even if I size down my screen. Part of the canvas is also off the screen.

I understand responsive means it'll scale. But why when the screen is even far below 800x800 it looks blurry, how can I correct that?

Relevant part of the index file:

...HTML above...
<div class="row">
  <div class="col-xs-1 col-sm-1" id="border">Menu</div>
  <div class="col-xs-11 col-sm-11" id="border">
  
<canvas id="Canvas"></canvas>
  </div>

</div>
<script src="js/script.js" charset="utf-8"></script>
<script>
<?php
 ...some PHP code here inserts images...
}
?>
</script>

The CSS is simply:

body{ background-color: ivory; }

#border{
  border: solid 1px black;
}

#Canvas{
  border: solid 1px red;  
  width: 100%;
}

Full script.js: https://pastebin.com/u63h6VZ8

Canvas preview of full page

Before change to responsive, top left corner of canvas and it looks fine

I did review similar questions such as:

[EDIT] The above blurry image is the full canvas. The second is the top left corner only of canvas before I made it responsive.

Zeno
  • 1,769
  • 7
  • 33
  • 61

1 Answers1

1

Imagine your canvas as an image ...they actually act the same. Imagine image 300px x 150px displayed to fit 100% of the container (screen)

  • when container is bigger -> image will definetly blur when scaling up
  • when container is smaller than image -> image will nicely scale down
  • best result - image of the exact size of the container (1:1 pixel ratio)

Your canvas actually has no size given so the browser by default creates a canvas sized 300 x 150px - that is why your canvas is getting blured - because it is being scaled up; To change that size you can write at the beginning of your code smth like .:

// Init the canvas
var canvas=document.getElementById("Canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

and in your CSS changing canvas to fixed position like this:

#Canvas{
  position: fixed;
  left:0;
  top:0;
  width:100%;
  height:100%;
}

...will give you the full-window-size canvas displayed with 1:1 pixel ratio. The working draft is here: https://jsfiddle.net/a2hefz0c/ (just re-run the code every time you change the size of the screen)

I have no idea what kind of responsivness you trying to reach - but this is the way to eliminate the blur and to take the whole area of the window. If you want to have canvas scaled-down and fitted into your container just leave your CSS as is and give your canvas hardcoded size like eg 1200x800px ...and this is what you're going to get: https://jsfiddle.net/6svk02ph/4/

The cropping of the content is also the result of the size of the canvas - when you draw something outside of the canvas (eg. 500px from left on a 300px canvas) it will not be on the canvas ;)

webdev-dan
  • 1,339
  • 7
  • 10
  • If my images are at an x/y position, wouldn't the first option (1:1 pixel ratio) change the location of my images whenever the canvas is resized? I'd have to avoid that as this is a compass chart. (I can try testing the second option) – Zeno Jan 23 '21 at 16:42
  • @Zeno I do not know how you calculate the position of your images - if you want them to be visible whatever the size of the canvas is - calculate the position relatively to the canvas size - it looks like the arrow-up x position is well calculated - always in the middle. Just do the same with the rest. PS. The second option - hardcoded size gives you easier (absolute) positioning/sizing for the cost of possible quality loss when resizing. – webdev-dan Jan 23 '21 at 19:17
  • Your second option seems to be what I was seeking! It seems to correct my issues, the only issue remaining is that the mouse drag feature `onMouseMove` loses track of where the image is after the window is resized and won't drag. Doing some debugging on that now. – Zeno Jan 24 '21 at 02:00
  • @Zeno You need to take the scale into account when calculating click position `var scale_ratio=canvas.width / $iCanvas.width();` and for example click event X position will be `var mx=parseInt(scale_ratio * e.clientX - $iCanvas.offset().left);` – webdev-dan Jan 24 '21 at 02:33
  • Marked as answered, thanks! Still have some work to do getting this working on mobile touch. – Zeno Jan 25 '21 at 22:19