Combine various routes within CanvasRenderingContext2D to simultaneously apply fill and stroke operations

I've been experimenting with drawing a series of intersecting shapes using paths in CanvasRenderingContext2D. While it's going smoothly, I'm now looking to merge all these paths so that when I fill them with a semi-transparent color, the overlaps are hidden. Additionally, I want to outline the merged shapes. Any ideas on how I can make this happen?

Answer №1

To prevent overlapping semi-transparent pixels from interacting, you can convert the semi-transparent pixels (in RGBA format) to equivalent opaque pixels (in RGB format).

function RGBAtoRGB(r, g, b, a, backgroundR,backgroundG,backgroundB){
    var r3 = Math.round(((1 - a) * backgroundR) + (a * r))
    var g3 = Math.round(((1 - a) * backgroundG) + (a * g))
    var b3 = Math.round(((1 - a) * backgroundB) + (a * b))
    return "rgb("+r3+","+g3+","+b3+")";
}

For each sub-shape, it is important to use a separate beginPath. This ensures that each shape has its own styling (fillStyle) per beginpath.

Depending on your specific design needs, you may need to adjust the drawing order of your sub-shapes so that the desired opaque shapes are displayed on top.

Answer №2

Mark, I appreciate your advice but I have discovered a more efficient method for accomplishing this task.

My technique involves drawing shapes on a white background bitmapdata and utilizing it as an alpha mask...

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

Restrict the movement of an object in Three.js to stay on the surface of another

My goal is to be able to drag an object and have it snap to the surface of another. Whether it snaps upon release or updates live doesn't matter to me. The ultimate objective is to create a smooth shape in Blender, import it, and enable snapping whil ...

Is it possible to utilize React hooks within a conditional statement?

My current situation involves an object serving as a state, complete with various properties. Now, I am looking to update this state within a specific condition using a hook. However, this update seems to trigger an infinite loop. The question at hand is ...

Assign a variable to set the property of a class

Could something similar to this scenario be achievable? const dynamicPropName = "x"; class A { static propName = 1 // equivalent to static x = 1 } A[dynamicPropName] // will result in 1 or would it need to be accessed as (typeof A)[dynamicPropN ...

Having trouble clearing a div with Jquery

I'm having trouble emptying the am-container with my jQuery code. Here is what I currently have: jQuery(document).find("div[id='am-container']").html(''); Below is the HTML structure: <div class="mid-part-right"> <div ...

Tips and tricks for activating javax.script in Websphere liberty profile on Bluemix

I am looking to incorporate JavaScript into one of my Java applications. In order to test this, I executed the following code: javax.script.ScriptEngineManager manager = new ScriptEngineManager(); javax.script.ScriptEngine engine = manager.getEngineByName ...

Issues within the team relating to the assortment of items in the shopping cart

Working on an angular online shop and facing a challenge here. We have a list of products, but I need to single out one specific product from the list when the "add to cart" button is clicked. Currently exploring this in the product.component.ts file with ...

Unable to connect HTML data to the view section using angular.js

I am trying to incorporate some HTML content into my view using angular.js. time.html: <table class="table table-bordered table-striped table-hover" id="dataTable"> <tr> <td width="100" align="center">Time <i class="fa fa-long- ...

Unable to activate parameter function until receiving "yes" confirmation from a confirmation service utilizing a Subject observable

Currently, I am working on unit tests for an Angular application using Jasmine and Karma. One of the unit tests involves opening a modal and removing an item from a tree node. Everything goes smoothly until the removeItem() function is called. This functi ...

Python tutorial: Scrape a website with a dropdown menu of ul-li items

Recently, I came across a query on how to scrape a particular website using Python that has a search box and javascripts. Intrigued by this question, I decided to try scraping company ratings from the website . The main goal was to input a company name in ...

Finding the Number of Days Between Two Dates in AngularJS Using JavaScript

When I receive two dates from a web service in the format below: var dateone = "2016-08-21T07:00:00.000Z"; var datetwo = "2016-08-28T07:00:00.000Z"; var datediff = datetwo - dateone; var numdays = Math.round(datediff); console.log("Number of Days is " + n ...

Methods for retrieving HTML tag attribute values with JavaScript Regular Expressions

Imagine I have the following HTML stored in a string: <meta http-equiv="Set-Cookie" content="COOKIE1_VALUE_HERE"> <meta http-equiv="Set-Cookie" content="COOKIE2_VALUE_HERE"> <meta http-equiv="Set-Cookie" content="COOKIE3_VALUE_HERE"> Ad ...

Action type is not recognized: modification

After browsing through multiple forums, I haven't found a solution that works for my particular issue with my VueJS app. The problem arises when I try to input something in my first component. Below is the code snippet: main.js import Vue from &apos ...

When trying to add a Google font to my blog on Blogger, I encountered an error while editing the Header Section

Can you assist me in resolving the issue shown in the accompanying image? The error message reads as follows: Error encountered during XML parsing, specifically on line 5, column 71: The "display" entity reference must conclude with the ';' del ...

Ways to automatically close browser tab upon successful submission of a form

I have an old file that has been updated. One of the requirements for this file/project modification is to have the current browser window close when the user clicks OK to submit the form. I am curious if this can be done using plain/vanilla JavaScript? ...

Effortless Inertia Scrolling Implemented in Next.js

My search for achieving smooth inertia scrolling in next.js has left me unimpressed with the available solutions, as they either lack performance or come with some drawback. This medium article and sandbox demo provided a choppy experience, While this jav ...

The button event is currently only targeting the initial class. (Jquery)

I am having an issue where only the first instance of the saveBtn class is being saved into local storage when clicked. Can anyone provide some assistance with this? Here is the HTML: <div class="hour-container" id="8am"> & ...

Looking to refresh a model within an AngularJS directive by utilizing a touchscreen interface

Utilizing ng-repeat allows me to generate a series of input fields within a form: <div class="col-sm-6 input" data-ng-repeat="v in values"> <div data-prefix-numeric-input data-prefix-name="{{ v.name }}"> <input type="number" id= ...

Exploring the World of JSON Mapping Libraries

In my project, I am dealing with objects (like a ball) that have specific data members and functions. These objects are being retrieved from various servers, each with its own hierarchy. For example: class Ball { int size; string color; } An instance ...

Fixing the 'window is not defined' Error in Node JS through Command Line

Trying to incorporate Angular JS using 'require' in my code snippet: var ang = require('angular'); However, encountering an error: ReferenceError: window is not defined I am aware that the window object is not defined in the Node co ...

Filtering with Angular to eliminate items based on their unique identifiers

What is the best way to filter values in Angular or remove values based on their IDs? I need to delete data or objects from an array of objects where the ID is 5, 12, and 9. In addition, we want to filter using multiple IDs, such as when the ID is 5, 12, ...