Categories
JavaScript Answers

How to fill rectangle with pattern with JavaScript?

Sometimes, we want to fill rectangle with pattern with JavaScript.

In this article, we’ll look at how to fill rectangle with pattern with JavaScript.

How to fill rectangle with pattern with JavaScript?

To fill rectangle with pattern with JavaScript, we can use d3.

For instance, we write:

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.3.0/d3.min.js"></script>

to add the d3 script.

Then we write:

const svg = d3.select("body").append("svg");

svg
  .append('defs')
  .append('pattern')
  .attr('id', 'diagonalHatch')
  .attr('patternUnits', 'userSpaceOnUse')
  .attr('width', 4)
  .attr('height', 4)
  .append('path')
  .attr('d', 'M-1,1 l2,-2 M0,4 l4,-4 M3,5 l2,-2')
  .attr('stroke', '#000000')
  .attr('stroke-width', 1);

svg.append("rect")
  .attr("x", 0)
  .attr("width", 100)
  .attr("height", 100)
  .style("fill", 'orange');

svg.append("rect")
  .attr("x", 0)
  .attr("width", 100)
  .attr("height", 100)
  .attr('fill', 'url(#diagonalHatch)');

to create an svg element and append it to the body as its child with

const svg = d3.select("body").append("svg");

Then we write

svg
  .append('defs')
  .append('pattern')
  .attr('id', 'diagonalHatch')
  .attr('patternUnits', 'userSpaceOnUse')
  .attr('width', 4)
  .attr('height', 4)
  .append('path')
  .attr('d', 'M-1,1 l2,-2 M0,4 l4,-4 M3,5 l2,-2')
  .attr('stroke', '#000000')
  .attr('stroke-width', 1);

to add the diagonal hatch pattern.

We add the width, height stroke, stroke-width, etc with attr.

We set the 'id' of the pattern so we can apply it later by referencing its ID.

Next, we add the rectangle with

svg.append("rect")
  .attr("x", 0)
  .attr("width", 100)
  .attr("height", 100)
  .style("fill", 'orange');

And we set width, height and fill of the rectangle with attr.

Finally, we append the diagonal hatch pattern we created with

svg.append("rect")
  .attr("x", 0)
  .attr("width", 100)
  .attr("height", 100)
  .attr('fill', 'url(#diagonalHatch)');

We call attr with 'fill' and 'url(#diagonalHatch)' to apply the diagonalHatch pattern we created earlier.

Conclusion

To fill rectangle with pattern with JavaScript, we can use d3.

Categories
JavaScript Answers

How to detect if microphone permissions have been granted in Chrome with JavaScript?

Sometimes, we want to detect if microphone permissions have been granted in Chrome with JavaScript.

In this article, we’ll look at how to detect if microphone permissions have been granted in Chrome with JavaScript.

How to detect if microphone permissions have been granted in Chrome with JavaScript?

To detect if microphone permissions have been granted in Chrome with JavaScript, we can use the navigation.permissions.query method.

For instance, we write:

const checkMic = async () => {
  const permissionStatus = await navigator.permissions.query({
    name: 'microphone'
  })
  console.log(permissionStatus)
}
checkMic()

to define the checkMic function that calls navigation.permissions.query with { name: 'microphone' } to return a promise with the microphone permission data.

As a result, we get something like

{name: 'audio_capture', state: 'denied', onchange: null}

logged.

Conclusion

To detect if microphone permissions have been granted in Chrome with JavaScript, we can use the navigation.permissions.query method.

Categories
JavaScript Answers

How to set specific property value of all objects in a JavaScript object array?

To set specific property value of all objects in a JavaScript object array, we can use the array map method.

For instance, we write:

const arr = [{
    id: "a1",
    guid: "123",
    value: "abc",
  },
  {
    id: "a2",
    guid: "123",
    value: "def",
  },
  {
    id: "a2",
    guid: "123",
    value: "def",
  },

]
const newArr = arr.map(e => ({
  ...e,
  status: "active"
}));
console.log(newArr)

to call arr.map with a function that returns an object with the entry e spread into a new object.

And then we add the status property to the same object.

Therefore, newArr is

[{
  guid: "123",
  id: "a1",
  status: "active",
  value: "abc"
}, {
  guid: "123",
  id: "a2",
  status: "active",
  value: "def"
}, {
  guid: "123",
  id: "a2",
  status: "active",
  value: "def"
}]
Categories
JavaScript Answers

How to stop label from toggling the input checkbox with JavaScript?

Sometimes, we want to stop label from toggling the input checkbox with JavaScript.

In this article, we’ll look at how to stop label from toggling the input checkbox with JavaScript.

How to stop label from toggling the input checkbox with JavaScript?

To stop label from toggling the input checkbox with JavaScript, we can call preventDefault in the label’s click handler.

For instance, we write:

<label for="checked">checked</label>
<input type="checkbox" id="checked" name="checked">

to add a label and an input.

Then we write:

const label = document.querySelector('label')
label.onclick = (e) => {
  e.preventDefault()
}

to select the label with querySelector.

Then we set label.onclick to a function that calls e.preventDefault to stop the label from toggling the checkbox.

Conclusion

To stop label from toggling the input checkbox with JavaScript, we can call preventDefault in the label’s click handler.

Categories
JavaScript Answers

How to toggle class in the nested component in React and JavaScript?

Sometimes, we want to toggle class in the nested component in React and JavaScript.

In this article, we’ll look at how to toggle class in the nested component in React and JavaScript.

How to toggle class in the nested component in React and JavaScript?

To toggle class in the nested component in React and JavaScript, we can use the classnames library.

To install it, we run npm i classnames.

Then we use it by writing:

import React from "react";
import classNames from "classnames";

export default function App() {
  const [active, setActive] = React.useState(false);
  const cx = classNames({ green: active });

  return (
    <>
      <style>{`.green { background-color: green }`}</style>
      <button onClick={() => setActive((a) => !a)}>toggle</button>
      <section className={cx}>hello world</section>
    </>
  );
}

to create the cx variable by calling classNames with an object that has the class name as the key and the condition of which it’s applied as the value.

When active is true, the green class is applied.

And we add a button to toggle the green class by setting onClick to a function that calls setActive to toggle the value of active.

Conclusion

To toggle class in the nested component in React and JavaScript, we can use the classnames library.