How come certain rectangles vanish when one rectangle completely fills the space?

Currently, I am encountering an issue with CSS paint worklet and I am trying to determine if it's a browser bug or an error on my end. In the worklet, I am drawing multiple rectangles. Strangely, when one rectangle covers the entire area, the others start disappearing as I adjust the zoom level. However, if I remove the

context.fillRect(0, 0, width, height)
, everything functions perfectly.

To provide a clear visual of the problem, I have created a sandbox code: https://codesandbox.io/s/magical-villani-py8x2

https://i.stack.imgur.com/wWZhM.gif

https://i.stack.imgur.com/y2Rem.gif

Answer №1

It appears there is a bug in the Chrome "experimental" implementation that you may want to report to their issue tracker for resolution.

While your use of the non-standard zoom property isn't necessarily problematic, switching to the transform property instead can prevent the occurrence of this bug:

(() => {

  if (!CSS.paintWorklet) {
    return console.error('CSS Paint API is not supported in this browser, you may have to enable it from chrome://flags/#enable-experimental-web-platform-features');
  }
  const worklet_script = document.querySelector('[type="paint-worklet"]').textContent;
  const worklet_blob = new Blob([worklet_script], { type: 'text/javascript' });
  CSS.paintWorklet.addModule(URL.createObjectURL(worklet_blob));

  window.addEventListener("DOMContentLoaded", () => {
    const slider = document.getElementById("slider");
    slider.addEventListener("input", () => {
      const el = document.querySelector(".content");
      el.style.transform = `scale(${slider.value},${slider.value})`;
    });
  });

})();
.content {
  background: paint(sandbox);
  border: 1px solid black;
  height: 200px;
  width: 200px;
  transform-origin: top left;
}
<input type="range" id="slider" min="0.5" max="4" value="1" step="0.1" />
<div class="content"></div>
<script type="paint-worklet">
class SandboxPaintWorklet {
  paint(context, geometry, properties) {
    const { width, height } = geometry;

    // background
    context.fillStyle = "#8866aa";
    context.fillRect(0, 0, width, height);

    context.fillStyle = "#000000";
    context.beginPath();
    // vertical line
    context.fillRect((width * 3) / 4, 0, 1, height);

    // horizontal lines
    const distance = Math.ceil(height / 20);

    for (let i = 0; i < 20; ++i) {
      context.fillRect(0, i * distance, width / 2, 1);
    }
  }
}

registerPaint("sandbox", SandboxPaintWorklet);
</script>

Even with the zoom property, using rect() to fill a single sub-path rather than multiple fillRect calls can also resolve the issue.

(() => {

  if (!CSS.paintWorklet) {
    return console.error('CSS Paint API is not supported in this browser, you may have to enable it from chrome://flags/#enable-experimental-web-platform-features');
  }
  const worklet_script = document.querySelector('[type="paint-worklet"]').textContent;
  const worklet_blob = new Blob([worklet_script], { type: 'text/javascript' });
  CSS.paintWorklet.addModule(URL.createObjectURL(worklet_blob));

  window.addEventListener("DOMContentLoaded", () => {
    const slider = document.getElementById("slider");
    slider.addEventListener("input", () => {
      const el = document.querySelector(".content");
      el.style.zoom = slider.value;
    });
  });

})();
.content {
  background: paint(sandbox);
  border: 1px solid black;
  height: 200px;
  width: 200px;
}
<input type="range" id="slider" min="0.5" max="4" value="1" step="0.1" />
<div class="content"></div>
<script type="paint-worklet">
class SandboxPaintWorklet {
  paint(context, geometry, properties) {
    const { width, height } = geometry;

    // background
    context.fillStyle = "#8866aa";
    context.fillRect(0, 0, width, height);

    context.fillStyle = "#000000";
    context.beginPath();
    // vertical line
    context.rect((width * 3) / 4, 0, 1, height);

    // horizontal lines
    const distance = Math.ceil(height / 20);

    for (let i = 0; i < 20; ++i) {
      context.rect(0, i * distance, width / 2, 1);
    }
    context.fill();
  }
}

registerPaint("sandbox", SandboxPaintWorklet);
</script>

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

fluctuating random percentage in JavaScript/jQuery

I am currently faced with the challenge of selecting a random number based on a given percentage ranging from 0 to 5. 0 - 25% (25/100) 1 - 25% (25/100) 2 - 20% (20/100) 3 - 15% (15/100) 4 - 10% (10/100) 5 - 5% (5/100) However, there are instances where ...

Choose the radio button upon clicking away from the input field

Currently, I have a standard Bootstrap 4 accordion that contains a radio button. Here is the code snippet: <div class="card"> <div class="card-header" id="headingOne"> <h2 class="mb-0"> ...

Understanding the implementation of setters in JavaScript: How are they utilized in Angular controllers?

After learning about getters and setters, I came across an example that clarified things for me: var person = { firstName: 'Jimmy', lastName: 'Smith' }; Object.defineProperty(person, 'fullName', { get: function() ...

Utilizing JQuery to detect a combination of ctrlKey and mouse click event

Looking for assistance with JQuery as I am new to it and still learning the logic. My goal is to determine which index of a textarea was clicked while holding down the CtrlKey. How can I combine an onclick event with a keyboard event and assign a function? ...

An image on the left side of a perfectly aligned text

I am currently working on aligning an image and text for a logo. However, I am having trouble justifying the second line to the width of the block. Here is the desired result: This is what I have tried: @import url(http://fonts.googleapis.com/css?famil ...

The top margin is experiencing issues and is not functioning correctly

I'm struggling to identify the problem with this script: http://jsfiddle.net/AKB3d/ #second { margin-top:50px; ... } My goal is to have the yellow box positioned 50px below the top border of the right box. However, every time I add margin-top to ...

Encountered a problem while attempting to create two separate div elements. The template root is designed to have only one element. This issue

<template> <div class="footerpart-container"> <div class="general-container"> dadada </div> <div class="legal-container"> dadad </div> <div class="helpsupport-container"> dada ...

Guide to setting up a Cordova and TypeScript project using the command line interface

For my mobile application development, I rely on Cordova and execute cordova create MyApp in the command-line to initiate a new project. I am familiar with JavaScript but now require TypeScript for my project. Please assist me in setting up a Cordova pro ...

The class functions perfectly under regular circumstances but ceases to operate once it is initialized

I'm currently developing a bluetooth remote in React Native. The issue I am facing is that my BLE class works perfectly on its own, but certain sections of code seem to malfunction when implemented within another class. import BLE from './Core/BL ...

Is it possible to utilize JavaScript on a mobile website for item counting purposes?

I have a task at hand that I need help with, and I'm unsure of the best approach to take. The goal is to create a mobile web page that can count items during a specific session. There will be four different items that need to be counted: chicken, cow, ...

Guide to retrieving fresh information from an API using a desktop application built on node and electron

Looking for advice on efficiently retrieving new order data from the Shopify API for a private desktop application I'm working on. Should I query the API at regular intervals while the application is active, or is there a way to implement webhooks in ...

Utilizing Font-face and background images in asset-pipeline plugin: A comprehensive guide

I am currently using the asset-pipeline from CodeSleeve to handle my style sheets in my Laravel project. I have successfully downloaded the application.css file, but I have a question: how can I include images and use font-face in this setup? Here is a sn ...

Adding individual names for elements in a list using Jackson

Is there a way to assign a unique name to each element in a JSON list using JACKSON? For instance, consider the following JSON data: {"result": [ { "name": "ABC", "age": "20" },{ "name": "DEF", "age": "12" } ]} Howeve ...

Is there a way to use setTimeout in JavaScript to temporarily stop a map or loop over an array?

data.forEach((d, i) => { setTimeout(() => { drawCanvas(canvasRef, d); }, 1000 * i); }); I have implemented a loop on an array using forEach with a one-second delay. Now I am looking to incorporate a pause and resume f ...

The overflow:hidden feature is ineffective in Firefox, yet functions properly in both IE and Chrome browsers

I'm in the process of creating a webshop and facing an issue with my sidebar due to varying product counts on different pages. For now, I have implemented the following workaround: padding-bottom: 5000px; margin-bottom: -5000px; overflow: ...

"Implementing Scrollify.js to dynamically add a class to the current section

I'm attempting to dynamically add an 'active' class to the current section on a page using scrollify.js. While I can retrieve the index value, I am struggling to get the section id or class. How can I obtain the id or class of the current s ...

Validating forms using Ajax in the Model-View-Controller

I am facing an issue with my Ajax form, where I need to trigger a JavaScript function on failure. The code snippet looks like this: using (Ajax.BeginForm("UpdateStages", new AjaxOptions { HttpMethod = "POST", OnSuccess = "refreshSearchResults(&apo ...

Can the Twitter Bootstrap typeahead feature be used with an external data source?

Can the typeahead feature in version 2.0.3 of twitter-bootstrap work with a remote data source? You can check out the link for more information on the typeahead functionality: ...

Creating a standalone executable for Node.js without including the entire Node.js framework

Trying to pack a simple hello world script into an executable has led to some challenges. Both pkg and nexe seem to include the entirety of Node.js in the output file, resulting in quite larger files than necessary (around 30 MB). Although EncloseJS was fo ...

Use jQuery to change the background color when clicked

Below is the HTML code with UL and LI elements: <UL> <LI><span id='select1'>Text</span></LI> <LI><span id='select2'>Text</span></LI> <LI><span id='select3'>Tex ...