Categories
JavaScript Answers React Native Answers

How to insert a line break into a Text component in React Native?

Sometimes, we want to insert a line break into a Text component in React Native.

In this article, we’ll look at how to insert a line break into a Text component in React Native.

How to insert a line break into a Text component in React Native?

To insert a line break into a Text component in React Native, we can add the '\n' character string.

For instance, we write:

import * as React from 'react';
import { View, StyleSheet } from 'react-native';
import { Text } from 'react-native-paper';

export default class MyComponent extends React.Component {
  render() {
    return (
      <View>
        <Text>
          Hi{'\n'}
          This is a test message.
        </Text>
      </View>
    );
  }
}

to add {'\n'} into the content of Text.

Then we see:

Hi
This is a test message.

rendered.

Conclusion

To insert a line break into a Text component in React Native, we can add the '\n' character string.

Categories
JavaScript Answers

How to get the filename from a file input with JavaScript?

Sometimes, we want to get the filename from a file input with JavaScript.

In this article, we’ll look at how to get the filename from a file input with JavaScript.

How to get the filename from a file input with JavaScript?

To get the filename from a file input with JavaScript, we can can it from the files property.

For instance, we write:

<input type='file'>

to add a file input.

Then we write:

const input = document.querySelector("input")
input.onchange = (e) => {
  const [file] = e.target.files
  console.log(file.name)
}

to select the input with querySelector.

Then we set input.onchange to a function that gets the selected files from e.target.files.

And then we get the file from the files object and get the file name from file.name.

Conclusion

To get the filename from a file input with JavaScript, we can can it from the files property.

Categories
JavaScript Answers

How to draw a line over an image background with HTML5 canvas and JavaScript?

Sometimes, we want to draw a line over an image background with HTML5 canvas and JavaScript.

In this article, we’ll look at how to draw a line over an image background with HTML5 canvas and JavaScript.

How to draw a line over an image background with HTML5 canvas and JavaScript?

To draw a line over an image background with HTML5 canvas and JavaScript, we can call some canvas methods.

For instance, we write:

<canvas width='200' height='300'></canvas>

to add a canvas.

Then we write:

const canvas = document.querySelector("canvas")
const context = canvas.getContext("2d")
const image = new Image()
image.onload = () => {
  context.drawImage(image, 100, 20, 500, 500);
  context.beginPath();
  context.moveTo(188, 130);
  context.bezierCurveTo(140, 10, 388, 10, 388, 170);
  context.strokeStyle = "black";
  context.stroke();
}
image.src = 'https://picsum.photos/200/300'

to select the canvas with querySelector.

Then we create an img element with Image.

We set image.onload to a function that draws image onto the canvas with drawImage.

Then we use beginPath to start drawing.

moveTo moves to the location we want to draw.

besizeCurveTo draws a bezier curve. The first 4 arguments are the x and y coordinates of the first 2 control points.

And the last 2 arguments are the x and y coordinates of the end point.

strokeStyle sets the stroke.

And stroke draws the line.

Conclusion

To draw a line over an image background with HTML5 canvas and JavaScript, we can call some canvas methods.

Categories
JavaScript Answers

How to edit pixels and remove white background in a canvas image in HTML and JavaScript?

Sometimes, we want to edit pixels and remove white background in a canvas image in HTML and JavaScript.

In this article, we’ll look at how to edit pixels and remove white background in a canvas image in HTML and JavaScript.

How to edit pixels and remove white background in a canvas image in HTML and JavaScript?

To edit pixels and remove white background in a canvas image in HTML and JavaScript, we can loop through the canvas pixels and change their color.

For instance, we write:

<canvas width='200' height='300'></canvas>

to add a canvas.

Then we write

const canvas = document.querySelector("canvas")
const ctx = canvas.getContext("2d")
const image = new Image()
image.onload = () => {
  canvas.height = canvas.width = 135;
  ctx.drawImage(image, 0, 0);
  const imgd = ctx.getImageData(0, 0, 135, 135),
    pix = imgd.data,
    newColor = {
      r: 0,
      g: 0,
      b: 0,
      a: 0
    };

  for (let i = 0, n = pix.length; i < n; i += 4) {
    const r = pix[i],
      g = pix[i + 1],
      b = pix[i + 2];
    if (r === 255 && g === 255 && b == 255) {
      pix[i] = newColor.r;
      pix[i + 1] = newColor.g;
      pix[i + 2] = newColor.b;
      pix[i + 3] = newColor.a;
    }
  }
  ctx.putImageData(imgd, 0, 0);
}
image.src = 'https://picsum.photos/200/300'

to select the canvas with querySelector.

In the image.onload method, we loop through the canvas image data we got from getImageData.

And we check for white with r === 255 && g === 255 && b == 255.

If that’s true, then we set the pixels to new rgba values.

We then call putImageData to draw the new pixels.

image.onload runs after image.src is set.

Conclusion

To edit pixels and remove white background in a canvas image in HTML and JavaScript, we can loop through the canvas pixels and change their color.

Categories
JavaScript Answers

How to get only first class of an HTML element with JavaScript?

Sometimes, we want to get only first class of an HTML element with JavaScript.

In this article, we’ll look at how to get only first class of an HTML element with JavaScript.

How to get only first class of an HTML element with JavaScript?

To get only first class of an HTML element with JavaScript, we can use the classList property.

For instance, we write:

<div class='foo bar baz'>

</div>

to add a div with multiple classes.

Then we write:

const div = document.querySelector('div')
const [firstClass] = div.classList
console.log(firstClass)

to select the div with querySelector.

And then we get the first class from div.classList with destructuring.

Therefore firstClass is 'foo'.

Conclusion

To get only first class of an HTML element with JavaScript, we can use the classList property.