What could be causing these transformed canvases to not display fully in Chrome at specific resolutions?

fiddle: https://jsfiddle.net/f8hscrd0/66/

html:

<body>
<div id="canvas_div">
</div>
</body>

js:

let colors = [
    ['#000','#00F','#0F0'],
    ['#0FF','#F00','#F0F'],
    ['#FF0','#FFF','#000']
]

let canvas_div = document.getElementById('canvas_div');
canvas_div.style.position = "fixed";
canvas_div.style.top = "50%";
canvas_div.style.left = "50%";
canvas_div.style.width = "0";
canvas_div.style.height = "0%";
canvas_div.style.transform = "scale(10) translate(-20px,-20px)";

for (i = 0; i < 3; i++){
    for (j = 0; j < 3; j++){
    let canvas = document.createElement('canvas');
    canvas.width = 100;
    canvas.height = 100;
    canvas.style.position = "fixed";
    canvas.style.top = `${i * 10}px`;
    canvas.style.left = `${j * 10}px`;
    canvas.style.width = "10px"
    canvas.style.height = "10px"
    let context = canvas.getContext("2d");
    context.fillStyle = colors[i][j];
    context.fillRect(-0,0,100,100);
        canvas_div.appendChild(canvas);
    }
}

Why do the canvases not render over the entire respective canvas element in chrome?

I'm uncertain on how to phrase that question differently. This issue arises when the viewport is an even number of pixels wide and high. It works flawlessly in Firefox or with an odd-sized window. If you don't see the problem, resize the fiddle or your browser window (or open developer tools).

What I would expect to see (and what I see in FF or with odd sized window):

https://i.stack.imgur.com/yC1Ma.png

What I see with an even width document:

https://i.stack.imgur.com/THwZi.png

Upon inspection, it appears that the canvas element itself is the correct size:

https://i.stack.imgur.com/TLkHp.png

Could this be a bug in Chrome, or have I overlooked/misinterpreted something?

Answer №1

Firstly, there are a couple of mistakes in the provided fiddle such as using appendChild instead of appendChile and not utilizing canvas.getContext('2d') to draw on the screen. Regarding your query, it seems that the white border you are observing is Chrome's default margin/padding for DOM elements. This can be eliminated by including the following code in your linked CSS file.

div, canvas {
  margin: 0px;
  padding: 0px;
}

For more details, refer to: CSS Box Model.


Further investigation reveals that the issue may stem from the scale(10) applied to the canvas div. Although no existing solution was found, I suggest removing the scaling and adjusting the script to position the canvases correctly.

...

canvas_div.style.transform = "translate(-20px,-20px)"; // Scale removed

...

for (i = 0; i < 3; i++){
    for (j = 0; j < 3; j++){
    let canvas = document.createElement('canvas');
    canvas.width = 100;
    canvas.height = 100;
    canvas.style.position = "fixed";
    canvas.style.top = `${i * 100}px`;
    canvas.style.left = `${j * 100}px`;
    canvas.style.width = "100px"
    canvas.style.height = "100px"

...

}

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

The "Key" function from Object.keys does not yield true when compared to an identical string

Object.keys(data).forEach((key) => { bannerData[key] = data[key][0]; console.log(key); if (key == "timestamp") { labels.push(data[key]); console.log("wtf"); console.log(labels); ...

Finding the value of a radio button dynamically created by jQuery

I am having an issue retrieving the value of a radio button that is generated using jQuery. I suspect there may be some problems with event handling. Below is my code: HTML <div id="divOption1"></div> The jQuery script to generate t ...

Updating jQuery event behaviors based on the value of .html( 'string' )

Visit this link for more information. Feel free to modify the heading if you believe it needs improvement. General I manage a multilingual WordPress website with dynamic menu and navigation controlled through the WordPress admin panel. The multilingual ...

Using jQuery and Modernizr for CSS3 and Javascript transitions support in Internet Explorer

Looking for a way to add support for button hover effects in IE 7-9 on this Codepen.io project. Can anyone help? For example, I'm trying: transition-duration: 0.5s; transition-property: all; transition-timing-function: ease; margin-top: 5px; I atte ...

Tips for invoking an Android function from an AngularJS directive?

I am facing an issue with my HTML that uses an AngularJS directive. This HTML file is being used in an Android WebView, and I want to be able to call an Android method from this directive (Learn how to call Android method from JS here). Below is the code ...

Convert JSON data into an HTML table using JavaScript without displaying the final result

I need help troubleshooting my JavaScript code that is supposed to populate an HTML table with data from a JSON file. However, the table remains empty and I can't figure out why. Sample JSON Data [{"User_Name":"John Doe","score":"10","team":"1"}, { ...

Encountering difficulty in retrieving the outcome of the initial HTTP request while utilizing the switchMap function in RxJS

My goal is to make 2 HTTP requests where the first call creates a record and then based on its result, I want to decide whether or not to execute the second call that updates another data. However, despite being able to handle errors in the catchError bl ...

The functionality of JSON.stringify does not take into account object properties

Check out the example on jsfiddle at http://jsfiddle.net/frigon/H6ssq/ I have encountered an issue where JSON.stringify is ignoring certain fields. Is there a way to make JSON.stringify include them in the parsing? In the provided jsfiddle code... <s ...

Is it possible to uncheck a checkbox from a list, even if some of them are already unchecked?

I am attempting to implement a feature where checking a checkbox selects all others in the list, and unchecking one deselects all. Currently, only selecting one checkbox allows me to check all the rest, but unchecking one doesn't trigger the reverse a ...

Update the HTML weather icon with data from JSON

I have a collection of weather icons stored on my computer. How can I customize the default weather icons with my own set of icons? In the JSON file, there is a variable like this: "icon":"partlycloudy" <html> <head> <script src="http://c ...

Having trouble getting NPM environment variables to function properly on a Windows system?

I have a confusion in my package.json file where I am attempting to compile less code using versioning. Here is an example of what I am trying to achieve: "scripts" { ... "build:css": "lessc --source-map css/index.less build/$npm_package_name.$npm_package ...

Is there a way to dynamically insert the value of an input field into a text field in real time using Javascript or ajax?

In my form, there is a text field with the id #image_tag_list_tokens. It looks like this: = f.text_area :tag_list_tokens, label: "Tags (optional) ->", data: {load: @image_tags }, label: "Tags" Additionally, I have an input field and a button: <i ...

Expanding and collapsing all divs with a single click using Jquery

I've gone through numerous resources but I'm still struggling to understand this concept. I have implemented a simple accordion based on this tutorial and now I want to include an "expand/collapse all" link. While I was able to expand all the ite ...

Combine several items to form a single entity

When I have two objects returned from the database, my goal is to combine them into one object. Here are the examples: { "id": 4, "first_name": "s", "last_name": "a", ...

extract the ng-model data and send it to an angular directive

In my current project, I have a specific situation where there are multiple text-boxes and when any of them is clicked, the corresponding ng-model should be displayed in the browser console. To achieve this functionality, I came up with the following Angul ...

Angular 6 - Outdated Functions

I'm having trouble updating the request options as they are now deprecated. I can't seem to locate the alternative option for this. Can anyone offer some assistance? import { Injectable } from '@angular/core'; import { HttpClient } fr ...

Determine if the specific subroute has a child using vue-router

After checking similar questions on stackoverflow without success, I am seeking a solution. I am attempting to determine if a subroute is a child of a specific route in order to display a container. Unfortunately, the following code snippet does not work: ...

Why does my Visual Studio Code always display "building" when I launch an extension?

https://code.visualstudio.com/api/get-started/your-first-extension I followed a tutorial to create a hello world extension. Why does my VSCode always display 'building' when I run the extension? Executing task: npm run watch < [email p ...

Set onfocus for DOM elements dynamically

I have a question regarding assigning onfocus or similar functions to DOM elements during runtime. Here is the scenario: <input type="text" id="box" onfocus="inputFocused()"/> Now, in a runtime assignment case: <input type="text" id="box"/> ...

Adding margin padding to a Material UI Navbar: Step-by-step guide

Hey there, I've added buttons to my Navbar from Mui. However, I'm running into an issue where the margin and padding won't change no matter what I do. I'm trying to create some space between them. Please see the code below: import { use ...