Categories
JavaScript Answers

How to Draw a Flipped or Mirrored Image with the HTML Canvas and JavaScript?

Sometimes, we want to draw a flipped or mirrored image with the HTML canvas and JavaScript.

In this article, we’ll look at how to draw a flipped or mirrored image with the HTML canvas and JavaScript.

Draw a Flipped or Mirrored Image with the HTML Canvas and JavaScript

To draw a flipped or mirrored image with the HTML canvas and JavaScript, we can call drawImage with the width multiplied by -1 and the scale method.

For instance, we write:

<canvas></canvas>

to add the canvas element.

Then we write:

const canvas = document.querySelector('canvas')
const context = canvas.getContext('2d')
const img = new Image()
img.src = 'https://i.picsum.photos/id/943/200/200.jpg?hmac=cFvTm_QTKVRRCLsOsMx1m2RmksDL0_U5AfcmnQ7TVds'
img.onload = () => {
  context.save();
  context.scale(-1, 1);
  context.drawImage(img, 0, 0, img.width * -1, img.height);
  context.restore();
}

to select the canvas with document.querySelector.

Then we call getContext to get the context.

Next, we use the Image constructor to create an img element.

And then we set the src property of the img element to the URL of the image.

Then we set img.onload to a function that draws the image when the image is loaded.

We call scale with -1 and 1 to flip the image.

Then we call drawImage with the img image, the x and y coordinates of the top left corner, and the width and height of the image respectively.

Now we see the flipped image drawn on the screen.

Conclusion

To draw a flipped or mirrored image with the HTML canvas and JavaScript, we can call drawImage with the width multiplied by -1 and the scale method.

Categories
JavaScript Answers jQuery

How to Set the Src Attribute of an Element with jQuery?

Sometimes, we want to set the src sttribute of an element with jQuery.

In this article, we’ll look at how to set the src sttribute of an element with jQuery.

Set the Src Attribute of an Element with jQuery

To set the src sttribute of an element with jQuery, we can call the attr method with the attribute name and value.

For instance, we write:

<img>

to add the img element.

Then we write:

$("img").attr("src", "https://picsum.photos/200");

to select the img element with $.

Then we call attr on the returned element with attribute name and value strings.

Now we should see an image displayed.

Conclusion

To set the src sttribute of an element with jQuery, we can call the attr method with the attribute name and value.

Categories
JavaScript Answers

How to Multiply Each Member of an Array by a scalar in JavaScript?

Sometimes, we want to multiply each member of an array by a scalar in JavaScript.

In this article, we’ll look at how to multiply each member of an array by a scalar in JavaScript.

Multiply Each Member of an Array by a scalar in JavaScript

To multiply each member of an array by a scalar in JavaScript, we can use the JavaScript array’s map method.

For instance, we write:

const a = [1, 2, 3];
const b = a.map(x => x * 5);
console.log(b)

to call map to multiply each member of the a array by 5 and assign the returned array to b.

Therefore, b is [5, 10, 15] according to the console log.

Conclusion

To multiply each member of an array by a scalar in JavaScript, we can use the JavaScript array’s map method.

Categories
JavaScript Answers

How to Get AM or PM from a Date with JavaScript?

Sometimes, we want to get AM or PM from a date with JavaScript.

In this article, we’ll look at how to get AM or PM from a date with JavaScript.

Get AM or PM from a Date with JavaScript

To get AM or PM from a date with JavaScript, we can use the toLocaleTimeString method.

For instance, we write:

const now = new Date()
  .toLocaleTimeString([], {
    hour: '2-digit',
    minute: '2-digit',
    hour12: true
  })
  .toLowerCase();
console.log(now)

to call toLocateTimeString on a date.

The 2nd argument is an object with the hour and minute set to the hour and minute format of our choice.

hour12 is set to true so that we see am and pm returned.

Therefore, now should be a time like '11:16 am'.

Conclusion

To get AM or PM from a date with JavaScript, we can use the toLocaleTimeString method.

Categories
JavaScript Answers

How to Convert Milliseconds into a Readable Date with JavaScript?

Sometimes, we want to convert milliseconds into a readable date with JavaScript.

In this article, we’ll look at how to convert milliseconds into a readable date with JavaScript.

Convert Milliseconds into a Readable Date with JavaScript

To convert milliseconds into a readable date with JavaScript, we can call the toLocaleString string method on the date.

For instance, we write:

const date = new Date(1324339200000);
const d = date.toLocaleString();
console.log(d)

We create a Date instance with the millisecond timestamp.

Then we call toLocaleString on the returned date object to return a string with the human readable version of the date-time assign it to d.

Therefore, d is '12/19/2011, 4:00:00 PM'.

Conclusion

To convert milliseconds into a readable date with JavaScript, we can call the toLocaleString string method on the date.