"Issue with scaling on Responsive Canvas leading to blurriness

I am working on a Canvas project and I want to make sure it is easily navigated on any device, whether it's a desktop or mobile. To achieve this, I decided to make the canvas responsive. Originally, the canvas was set at 800x800 which looked fine but wasn't responsive. The canvas contains lines and image files, and I'm using Bootstrap in my project.

I removed the width/height definitions and started testing its responsiveness. While it did respond to different screen sizes, everything appeared enlarged and blurry, including the images and lines. Even when I reduced the screen size, parts of the canvas were off-screen.

I understand that being responsive means scaling, but why does it look blurry even when the screen is well below 800x800? How can I fix this issue?

Here is an excerpt from 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 code is as follows:

body{ background-color: ivory; }

#border{
  border: solid 1px black;
}

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

Full script.js file can be found here.

You can view the images used in the canvas here: https://i.sstatic.net/wf758.png, https://i.sstatic.net/RyGwV.png.

I have researched similar questions like:

  • HTML Blurry Canvas Images (not relevant as it's about non-responsive canvases with fixed sizes)
  • Canvas drawings, like lines, are blurry (also not applicable as it's not related to responsiveness)
  • Canvas circle looks blurry (this was specific to phones, whereas my issue is across all devices)

[EDIT] The first blurry image represents the entire canvas, while the second image shows only the top left corner before making it responsive.

Answer №1

Visualize your canvas as a picture ...they operate in a similar manner. Envision an image sized 300px x 150px adjusting to fill 100% of the container (screen)

  • If the container is larger -> image will inevitably become blurry when upscaled
  • If the container is smaller than the image -> image will scale down smoothly
  • Optimal outcome - image matches the container's size perfectly (1:1 pixel ratio)

Your canvas currently has no specified dimensions, so the browser defaults to creating a canvas with dimensions of 300 x 150px - leading to blurriness due to upscaling; To adjust this size, you can add the following code at the start of your script:

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

Additionally, in your CSS, setting the canvas to fixed position like this:

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

...will result in a full-window-size canvas displayed with a 1:1 pixel ratio. You can find a working example here: https://jsfiddle.net/a2hefz0c/ (ensure to re-run the code after resizing the screen)

The presence of blur and utilizing the entire window area are addressed with this approach, adapting to various responsiveness requirements. If you prefer a scaled-down canvas within your container, retain your current CSS settings and assign a specific size to your canvas such as 1200x800px ...resulting in this output: https://jsfiddle.net/6svk02ph/4/

Cropping occurs based on the canvas size - content drawn outside the canvas boundaries won't be visible ;)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Displaying various charts in a single view without the need for scrolling in HTML

How can I display the first chart larger and all subsequent charts together in one window without requiring scrolling? This will eventually be viewed on a larger screen where everything will fit perfectly. Any recommendations on how to achieve this? Here ...

Another option could be to either find a different solution or to pause the loop until the

Is it possible to delay the execution of a "for" loop until a specific condition is met? I have a popup (Alert) that appears within the loop, prompting the user for confirmation with options to Agree or Cancel. However, the loop does not pause for the co ...

Mastering the art of incorporating live JavaScript search functionality that updates in real-time as the user continues typing on the same

Trying to implement a JavaScript search feature to find a div within the same page. The current code being used for the search query display is: $('.form-search').on('submit',function(){return false;}); $('.form-search .btn') ...

Making the Bootstrap 4 card-footer expand to occupy the remaining height of the column

I'm currently developing a project that incorporates bootstrap cards. There are 3 cards, each for a different column, and I need them to have equal heights. Here is how they currently look: https://i.sstatic.net/Nau98.png The B and C cards should oc ...

A comprehensive tool for testing JavaScript, CSS, HTML, and jQuery code

Currently, I am working on an extension for Google Chrome that consists of multiple .js, .css, and .html files. My coding process involves using Notepad++ as my primary tool. However, I find myself needing to conduct testing at various stages of developm ...

Creating a grid in JavaScript using a formula

I'm looking to create a grid of objects using JavaScript. Unfortunately, I can't remember the formula and I haven't been able to find it online: var width = 113; var height = 113; var col = 10; ...

Get a single object from an array with .single method provided by @ngrx

Seeking to retrieve an Observable containing a single object from an array of objects in my store. I aim to utilize the .single operator, which should throw an exception if there is not exactly 1 object present. However, I'm struggling with this as my ...

if and elsif JSON with Angular

Currently, I am working on completing a task within our project. To provide more context, I have a field with items that I must select. Once I choose an item, another field appears in which I must select additional elements. These elements need to be sen ...

Combining Json attributes in Jquery Grouping

My task is to group the displays by sectors, but I couldn't find a method in JSON to achieve this. Below is the code snippet: $(function displays(){ var url = '{% url get_displays %}'; $.getJSON(url, function(data) { var sidebar = ...

Horizontal Scrolling Menu for Dynamic Page Navigation

My goal was to create a smooth-scrolling one-page website that scrolls horizontally. One feature I wanted was a menu that smoothly slides into view whenever a new page is in focus along with the horizontal scrolling. I found an example of a scrolling scr ...

Can a .p12 or .pfx file be imported into Selenium Webdriver using only code?

Hey there everyone! I ran into an issue while trying to write one of my end-to-end tests. I want to log in to my application using a certificate, but I'm not sure if it's possible to do this purely through code. I've spent several days sea ...

deleting multiple items with checkbox selection in Angular 2

Can anyone provide the code for both the component and service to implement multiple deletion using checkboxes in Angular 2? I have only posted the HTML code, but I need assistance with the components and services as well. The current code allows single ...

Exploring all the tags of a webpage using Python and Beautifulsoup: A step-by-step guide

I am a beginner in this industry. The website I am looking to crawl is "http://py4e-data.dr-chuck.net/comments_1430669.html" and here is its source code "view-source:http://py4e-data.dr-chuck.net/comments_1430669.html" It is a simple website for practice. ...

Are there any JQuery events that can detect alterations in the list of children elements within an element?

Is there a JQuery event available that can detect changes in the size of an element collection, such as growth or reduction resulting from adding or removing child elements? ...

Dynamically setting CSS values with jQuery to center an image within a div

I'm trying to center an image in a div according to the width of the image after I scale it to have a height of 250px. For example, if I had an image with dimensions 625x450 and, in my CSS file, set the height of the image to be 250px, then the width ...

Creating an HTML table that adjusts to the screen height with square-shaped cells

var grid = document.getElementById("grid"); var size = 50; for (var y = 0; y < size; y++) { var currentRow = grid.insertRow(0); for (var x = 0; x < size; x++) { var currentCell = currentRow.insertCell(-1); currentCell.style.height = currentCell.styl ...

Postman's ability to capture elements that meet specific criteria set by another element

Here is the output of my response code: [ { "id": 364, "siteName": "FX21 - PortA", }, { "id": 364, "siteName": "FX21 - PortB", }, { "id": 370, "siteName": "FX05 - ER", }, I'm tr ...

Rotating an input 90 degrees within a div for unique positioning

I used JavaScript to make an input range vertical, like so: var range_pitch = document.createElement("input"); range_pitch.setAttribute("type", "range"); range_pitch.style.webkitTransform = "rotate(90deg)"; range_pitch.style.mozTransform = "rotate(90deg)" ...

What is the reason why Prettier does not automatically format code in Visual Studio Code?

After installing and enabling ESLint and Prettier in my Nuxt application, I made the switch to Visual Studio Code. However, when I open a .vue file and use CMD+ Shift + P to select Format Document, my file remains unformatted. I even have the Prettier ex ...

Creating a circular frame for your image to use as a Facebook profile picture

In the process of developing an input feature that allows users to upload file images for avatar changes, similar to Facebook's functionality. However, I am aware that Facebook utilizes a circular area for avatars and enables users to adjust the image ...